Class: SpreeCmCommissioner::UserAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/user_authenticator.rb

Class Method Summary collapse

Class Method Details

.auth_context(params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 31

def self.auth_context(params)
  oauth_application = find_oauth_application(params)
  tenant_id = oauth_application&.tenant_id

  case flow_type(params)
  when 'login_auth'
    options = { login: params[:username], password: params[:password], tenant_id: tenant_id }
    SpreeCmCommissioner::UserPasswordAuthenticator.call(options)
  when 'social_auth'
    options = { id_token: params[:id_token], tenant_id: tenant_id }
    SpreeCmCommissioner::UserIdTokenAuthenticator.call(options)
  when 'facebook_auth'
    options = { fb_access_token: params[:fb_access_token], tenant_id: tenant_id }
    SpreeCmCommissioner::UserFbTokenAuthenticator.call(options)
  when 'telegram_web_app_auth'
    options = { telegram_init_data: params[:telegram_init_data], telegram_bot_username: params[:tg_bot] }
    SpreeCmCommissioner::UserTelegramWebAppAuthenticator.call(options)
  when 'vattanac_bank_web_app_auth'
    options = { session_id: params[:session_id] }
    SpreeCmCommissioner::UserVattanacBankWebAppAuthenticator.call(options)
  end
end

.call!(params) ⇒ Object

:username, :password :id_token :fb_access_token :telegram_init_data, :tg_bot :session_id



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 10

def self.call!(params)
  context = auth_context(params)
  raise exception(context.message) unless context.success?

  user = context.user

  # Check if user.tenant_id is nil first to keep our old logic work as usual
  if user.tenant_id.nil? && params[:client_id].blank? && params[:client_secret].blank?
    return user
  elsif params[:client_id].present? && params[:client_secret].present?
    oauth_application = find_oauth_application(params)
    raise exception(I18n.t('authenticator.invalid_client_credentials')) unless oauth_application

    validate_tenant_match!(user, oauth_application)
  else
    raise exception(I18n.t('authenticator.invalid_or_missing_params'))
  end

  user
end

.exception(message) ⇒ Object



64
65
66
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 64

def self.exception(message)
  Doorkeeper::Errors::DoorkeeperError.new(message)
end

.find_oauth_application(params) ⇒ Object



74
75
76
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 74

def self.find_oauth_application(params)
  Spree::OauthApplication.find_by(uid: params[:client_id], secret: params[:client_secret])
end

.flow_type(params) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 54

def self.flow_type(params)
  return 'login_auth' if params.key?(:username) && params.key?(:password)
  return 'social_auth' if params.key?(:id_token)
  return 'facebook_auth' if params.key?(:fb_access_token)
  return 'telegram_web_app_auth' if params.key?(:telegram_init_data) && params.key?(:tg_bot)
  return 'vattanac_bank_web_app_auth' if params.key?(:session_id)

  raise exception(I18n.t('authenticator.invalid_or_missing_params'))
end

.validate_tenant_match!(user, oauth_application) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


68
69
70
71
72
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 68

def self.validate_tenant_match!(user, oauth_application)
  return if user.tenant_id == oauth_application.tenant_id

  raise ActiveRecord::RecordNotFound
end