Sha256: 2ec078a3eb38499383b0fc942667e01c67432cd4ef78258bf4fbbb90c88ef43e

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module NulogySSO

  # A mix-in that is intended to enhance a controller with NulogySSO authentication code.
  # It is recommended to `include NulogySSO::ControllerHelper` in your ApplicationController.
  module ControllerHelper
    extend ActiveSupport::Concern

    included do
      # Makes the commonly used @current_user variable available to controllers and views.
      # This emulates a code pattern popular in Rails apps using Devise.
      attr_reader :current_user
      helper_method :current_user
    end

    def authenticate_sso_user
      raw_token = cookies[NulogySSO.auth_cookie_key]
      return redirect_to nulogy_sso.login_path if raw_token.blank?

      @current_user = Authenticator.new.authenticated_user(raw_token)
      return redirect_to nulogy_sso.login_path if @current_user.blank?
      return render status: :forbidden, template: "sso_error" unless valid_user?(@current_user)
    end

    private

    def valid_user?(user)
      NulogySSO.validate_user.call(user)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nulogy_sso-0.2.0 lib/nulogy_sso/controller_helper.rb