Class: SpreeCmCommissioner::UserAuthenticator
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::UserAuthenticator
- Defined in:
- app/services/spree_cm_commissioner/user_authenticator.rb
Class Method Summary collapse
- .auth_context(params) ⇒ Object
-
.call!(params) ⇒ Object
:username, :password :id_token.
- .exception(message) ⇒ Object
- .find_oauth_application(params) ⇒ Object
- .flow_type(params) ⇒ Object
- .validate_tenant_match!(user, oauth_application) ⇒ Object
Class Method Details
.auth_context(params) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 27 def self.auth_context(params) case flow_type(params) when 'login_auth' = { login: params[:username], password: params[:password] } SpreeCmCommissioner::UserPasswordAuthenticator.call() when 'social_auth' = { id_token: params[:id_token] } SpreeCmCommissioner::UserIdTokenAuthenticator.call() when 'telegram_web_app_auth' = { telegram_init_data: params[:telegram_init_data], telegram_bot_username: params[:tg_bot] } SpreeCmCommissioner::UserTelegramWebAppAuthenticator.call() end end |
.call!(params) ⇒ Object
:username, :password :id_token
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 6 def self.call!(params) context = auth_context(params) raise exception(context.) 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
49 50 51 |
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 49 def self.exception() Doorkeeper::Errors::DoorkeeperError.new() end |
.find_oauth_application(params) ⇒ Object
59 60 61 |
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 59 def self.find_oauth_application(params) Spree::OauthApplication.find_by(uid: params[:client_id], secret: params[:client_secret]) end |
.flow_type(params) ⇒ Object
41 42 43 44 45 46 47 |
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 41 def self.flow_type(params) return 'login_auth' if params.key?(:username) && params.key?(:password) return 'social_auth' if params.key?(:id_token) return 'telegram_web_app_auth' if params.key?(:telegram_init_data) && params.key?(:tg_bot) raise exception(I18n.t('authenticator.invalid_or_missing_params')) end |
.validate_tenant_match!(user, oauth_application) ⇒ Object
53 54 55 56 57 |
# File 'app/services/spree_cm_commissioner/user_authenticator.rb', line 53 def self.validate_tenant_match!(user, oauth_application) return if user.tenant_id == oauth_application.tenant_id raise ActiveRecord::RecordNotFound end |