Module: EasyAuth::Models::Identities::OauthCore::ClassMethods
- Defined in:
- lib/easy_auth/models/identities/oauth_core.rb
Instance Method Summary collapse
- #account_attributes(account_attributes, identity) ⇒ Object
- #account_attributes_map ⇒ Object
- #authenticate(controller) ⇒ Object
- #can_authenticate?(controller) ⇒ Boolean
- #client_id ⇒ Object
- #provider ⇒ Object
- #retrieve_uid(account_attributes) ⇒ Object
- #secret ⇒ Object
- #settings ⇒ Object
- #with_account(identity, controller, account_attributes) ⇒ Object
- #without_account(identity, controller, account_attributes) ⇒ Object
Instance Method Details
#account_attributes(account_attributes, identity) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 59 def account_attributes(account_attributes, identity) EasyAuth.account_model.define_attribute_methods unless EasyAuth.account_model.attribute_methods_generated? setters = EasyAuth.account_model.instance_methods.grep(/=$/) - [:id=] attributes = account_attributes_map.inject({}) do |hash, kv| if setters.include?("#{kv[0]}=".to_sym) hash[kv[0]] = account_attributes[kv[1]] end hash end attributes[:identities_attributes] = [ { uid: identity.uid, token: identity.token, type: identity.class.model_name.to_s } ] return attributes end |
#account_attributes_map ⇒ Object
78 79 80 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 78 def account_attributes_map { :email => 'email' } end |
#authenticate(controller) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 5 def authenticate(controller) if can_authenticate?(controller) identity, account_attributes = *yield if controller.current_account with_account(identity, controller, account_attributes) else without_account(identity, controller, account_attributes) end end end |
#can_authenticate?(controller) ⇒ Boolean
55 56 57 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 55 def can_authenticate?(controller) raise NotImplementedError end |
#client_id ⇒ Object
82 83 84 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 82 def client_id settings.client_id end |
#provider ⇒ Object
94 95 96 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 94 def provider self.to_s.split('::').last.underscore.to_sym end |
#retrieve_uid(account_attributes) ⇒ Object
98 99 100 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 98 def retrieve_uid(account_attributes) raise NotImplementedError end |
#secret ⇒ Object
86 87 88 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 86 def secret settings.secret end |
#settings ⇒ Object
90 91 92 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 90 def settings EasyAuth.send(version)[provider] end |
#with_account(identity, controller, account_attributes) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 17 def with_account(identity, controller, account_attributes) if identity.account if identity.account != controller.current_account controller.flash[:error] = I18n.t('easy_auth.oauth2.sessions.create.error') return nil end else identity.account = controller.current_account end identity.save! return identity end |
#without_account(identity, controller, account_attributes) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/easy_auth/models/identities/oauth_core.rb', line 32 def without_account(identity, controller, account_attributes) if identity.account return identity else account_model_name = EasyAuth.account_model.model_name env = clean_env(controller.env.dup) env['QUERY_STRING'] = {account_model_name.param_key => account_attributes(account_attributes, identity)}.to_param account_controller_class = ActiveSupport::Dependencies.constantize("#{account_model_name.route_key.camelize}Controller") account_controller = account_controller_class.new account_controller.dispatch(:create, ActionDispatch::Request.new(env)) controller.status = account_controller.status controller.location = account_controller.location controller.content_type = account_controller.content_type controller.response_body = account_controller.response_body controller.request.session = account_controller.session return nil end end |