class GrapeTokenAuth::TokenAuthorizer

Attributes

data[R]
resource[R]
resource_class[R]
user[R]

Public Class Methods

new(authorizer_data) click to toggle source
# File lib/grape_token_auth/token_authorizer.rb, line 6
def initialize(authorizer_data)
  @data = authorizer_data
end

Public Instance Methods

authenticate_from_token(scope) click to toggle source
# File lib/grape_token_auth/token_authorizer.rb, line 10
def authenticate_from_token(scope)
  initialize_resource_class(scope)
  return nil unless resource_class

  resource_from_existing_warden_user(scope)
  return resource if correct_resource_type_logged_in?

  find_resource(scope)
end
find_resource(scope) click to toggle source
# File lib/grape_token_auth/token_authorizer.rb, line 20
def find_resource(scope)
  initialize_resource_class(scope)
  return nil unless resource_class

  return nil unless data.token_prerequisites_present?

  load_user_from_uid
  return nil unless user_authenticated?
  data.authed_with_token = true
  user
end

Private Instance Methods

correct_resource_type_logged_in?() click to toggle source
# File lib/grape_token_auth/token_authorizer.rb, line 53
def correct_resource_type_logged_in?
  resource && resource.class == resource_class
end
initialize_resource_class(scope) click to toggle source
# File lib/grape_token_auth/token_authorizer.rb, line 36
def initialize_resource_class(scope)
  @resource_class =  GrapeTokenAuth.configuration.scope_to_class(scope)
end
load_user_from_uid() click to toggle source
# File lib/grape_token_auth/token_authorizer.rb, line 40
def load_user_from_uid
  @user = resource_class.find_by_uid(data.uid)
  # TODO: hacky solution to the fact that this statement can fail sporadically
  # with multiple requests. Nil returned from the statement. but
  # re-executing the request causes it to pass? Database lock?
rescue ::ActiveRecord::StatementInvalid
  @user = resource_class.find_by_uid(data.uid)
end
resource_from_existing_warden_user(scope) click to toggle source
# File lib/grape_token_auth/token_authorizer.rb, line 49
def resource_from_existing_warden_user(scope)
  @resource = data.exisiting_warden_user(scope)
end
user_authenticated?() click to toggle source
# File lib/grape_token_auth/token_authorizer.rb, line 57
def user_authenticated?
  user && user.valid_token?(data.token, data.client_id)
end