Module: Capcoauth::Rails::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/capcoauth/rails/helpers.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/capcoauth/rails/helpers.rb', line 6

def current_user

  # Don't return user for options requests
  return if request.method_symbol == :options

  # Bypass if already set/verified
  return @current_user if @_current_user_performed
  @_current_user_performed = true

  # Get the token object
  token, error = verify_token.first

  # Skip lookup if application credentials or token invalid
  return nil if token.blank? or error.present?

  # Resolve user ID using configuration resolver unless already found
  begin
    @current_user = Capcoauth.configuration.user_resolver.call(token.user_id) if token.user_id.present?
  rescue ActiveRecord::RecordNotFound => e
    Capcoauth.configuration.logger.info "[CapcOAuth] Error looking up user: #{e.message}"
  end

  @current_user
end

#verify_authorized!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/capcoauth/rails/helpers.rb', line 31

def verify_authorized!

  # Don't verify options requests
  return if request.method_symbol == :options

  # Run verification
  token, error, reason = verify_token

  # Re-raise exceptions with human-readable reason
  raise Capcoauth::AuthorizationError, reason if error == :unauthorized_error

  # Raise an error if token has an ID but the user wasn't found
  if Capcoauth.configuration.require_user and token.user_id.present? and current_user.blank?
    Capcoauth.configuration.logger.info "[CapcOAuth] Error looking up user: Token returned ID ##{token.user_id} but resolver didn't return user"
    raise Capcoauth::AuthorizationError, 'Your credentials were valid, but you aren\'t currently active in this system'
  end
end