class Warden::Cognito::AuthenticatableStrategy

Attributes

helper[R]
user_not_found_callback[R]

Public Class Methods

new(env, scope = nil) click to toggle source
Calls superclass method
# File lib/warden/cognito/authenticatable_strategy.rb, line 9
def initialize(env, scope = nil)
  super
  @user_not_found_callback = UserNotFoundCallback.new
  @helper = UserHelper.new
end

Public Instance Methods

authenticate!() click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 19
def authenticate!
  attempt = cognito_client.initiate_auth(email, password)

  return fail(:unknow_cognito_response) unless attempt

  user = local_user || trigger_callback(attempt.authentication_result)

  fail!(:unknown_user) unless user.present?
  success!(user)
rescue Aws::CognitoIdentityProvider::Errors::NotAuthorizedException
  fail!(:invalid_login)
rescue StandardError
  fail(:unknow_cognito_response)
end
valid?() click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 15
def valid?
  cognito_authenticable?
end

Private Instance Methods

auth_params() click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 65
def auth_params
  params[scope.to_s].symbolize_keys.slice(:password, :email, :pool_identifier)
end
cognito_authenticable?() click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 49
def cognito_authenticable?
  params[scope.to_s].present? && password.present?
end
cognito_client() click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 36
def cognito_client
  CognitoClient.scope pool_identifier
end
email() click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 53
def email
  auth_params[:email]
end
local_user() click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 45
def local_user
  helper.find_by_cognito_username(email, cognito_client.pool_identifier)
end
password() click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 57
def password
  auth_params[:password]
end
pool_identifier() click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 61
def pool_identifier
  auth_params[:pool_identifier]&.to_sym
end
trigger_callback(authentication_result) click to toggle source
# File lib/warden/cognito/authenticatable_strategy.rb, line 40
def trigger_callback(authentication_result)
  cognito_user = cognito_client.fetch(authentication_result.access_token)
  user_not_found_callback.call(cognito_user, cognito_client.pool_identifier)
end