Sha256: 0c8c87874727cc61aec88dddd203bd35f934b88887ce175b2b3412e4da55e9f1

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

module DoorkeeperHub
  module Helpers
    extend ActiveSupport::Concern

    included do
      before_action :authenticate_doorkeeper!, unless: -> { DoorkeeperHub.client_mode? }
    end

    def authenticate_doorkeeper!
      if warden.authenticated?
        return if DoorkeeperHub.client_mode?

        redirect_to root_path_with_token if params[DoorkeeperHub.token_param].nil?
      else
        # TODO add origin path to params
        redirect_to '/auth/saml'
      end
    end

    def default_url_options(options = {})
      return options if DoorkeeperHub.client_mode?

      options[DoorkeeperHub.token_param] = params[DoorkeeperHub.token_param]
      options
    end

    def after_sign_in_path
      request.env['omniauth.origin'] || (DoorkeeperHub.client_mode? ? root_path : root_path_with_token)
    end

    def root_path_with_token
      root_path DoorkeeperHub.token_param => params_or_default_token
    end

    def params_or_default_token
      params[DoorkeeperHub.token_param] ||
        companies.first.send(DoorkeeperHub.config.doorkeeper_token_name)
    end

    def company
      @company ||=
        companies.active.find_by(DoorkeeperHub.config.doorkeeper_token_name => params[DoorkeeperHub.token_param]) ||
          raise(ActionController::RoutingError.new('Not Found'))

    end

    def current_user
      warden.user
    end

    def companies
      @companies ||= DoorkeeperHub.config
                       .scope_model
                       .constantize
                       .where(DoorkeeperHub.config.doorkeeper_token_name => current_user.company_tokens)
    end

    def warden
      request.env['warden']
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
doorkeeper_hub-0.3.3 lib/doorkeeper_hub/helpers.rb
doorkeeper_hub-0.3.2 lib/doorkeeper_hub/helpers.rb
doorkeeper_hub-0.3.1 lib/doorkeeper_hub/helpers.rb
doorkeeper_hub-0.3.0 lib/doorkeeper_hub/helpers.rb