Module: Devise::Models::Oauth2Authenticatable

Defined in:
lib/devise_oauth2_authenticatable/model.rb

Overview

OAuth2 Connectable Module, responsible for validating authenticity of a user and storing credentials while signing in using their OAuth2 account.

Configuration:

You can overwrite configuration values by setting in globally in Devise (Devise.setup), using devise method, or overwriting the respective instance method.

oauth2_uid_field - Defines the name of the OAuth2 user UID database attribute/column.

oauth2_token_field - Defines the name of the OAuth2 session key database attribute/column.

oauth2_auto_create_account - Speifies if account should automatically be created upon connect

if not already exists.

Examples:

User.oauth2_connect(:uid => '123456789')     # returns authenticated user or nil
User.find(1).oauth2_connected?               # returns true/false

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



31
32
33
34
35
# File 'lib/devise_oauth2_authenticatable/model.rb', line 31

def self.included(base) #:nodoc:
  base.class_eval do
    extend ClassMethods
  end
end

Instance Method Details

#oauth2_connected?Boolean Also known as: is_oauth2_connected?

Checks if OAuth2 Connected.

Returns:

  • (Boolean)


58
59
60
# File 'lib/devise_oauth2_authenticatable/model.rb', line 58

def oauth2_connected?
  self.send(:"#{self.class.oauth2_uid_field}").present?
end

#on_after_oauth2_connect(oauth2_user_attributes) ⇒ Object

Hook that gets called after a connection (each time). Useful for fetching additional user info (etc.) from OAuth2.

Default: Do nothing.

Example:

# Overridden in OAuth2 Connect:able model, e.g. "User".
#
def after_oauth2_connect(oauth2_user_attributes)
  # See "on_before_oauth2_connect" example.
end


103
104
105
106
107
108
# File 'lib/devise_oauth2_authenticatable/model.rb', line 103

def on_after_oauth2_connect(oauth2_user_attributes)
  
  if self.respond_to?(:after_oauth2_auto_create)
    self.send(:after_oauth2_auto_create, oauth2_user_attributes) rescue nil
  end
end

#on_before_oauth2_auto_create(oauth2_user_attributes) ⇒ Object

end

For more info:

* http://oauth2er.pjkh.com/user/populate


83
84
85
86
87
88
# File 'lib/devise_oauth2_authenticatable/model.rb', line 83

def on_before_oauth2_auto_create(oauth2_user_attributes)
  
  if self.respond_to?(:before_oauth2_auto_create)
    self.send(:before_oauth2_auto_create, oauth2_user_attributes) rescue nil
  end
end

#store_oauth2_credentials!(attributes = {}) ⇒ Object

Store OAuth2 Connect account/session credentials.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/devise_oauth2_authenticatable/model.rb', line 39

def store_oauth2_credentials!(attributes = {})
  self.send(:"#{self.class.oauth2_uid_field}=", attributes[:uid])
  self.send(:"#{self.class.oauth2_token_field}=", attributes[:token])

  # Confirm without e-mail - if confirmable module is loaded.
  self.skip_confirmation! if self.respond_to?(:skip_confirmation!)

  # Only populate +email+ field if it's available (e.g. if +authenticable+ module is used).
  self.email = attributes[:email] || '' if self.respond_to?(:email)

  # Lazy hack: These database fields are required if +authenticable+/+confirmable+
  # module(s) is used. Could be avoided with :null => true for authenticatable
  # migration, but keeping this to avoid unnecessary problems.
  self.password_salt = '' if self.respond_to?(:password_salt)
  self.encrypted_password = '' if self.respond_to?(:encrypted_password)
end

#store_session(using_token) ⇒ Object

Optional: Store session key.



112
113
114
115
116
# File 'lib/devise_oauth2_authenticatable/model.rb', line 112

def store_session(using_token)
  if self.token != using_token
    self.update_attribute(self.send(:"#{self.class.oauth2_token_field}"), using_token)
  end
end