module Devise::Models::G5Authenticatable

Authenticatable module, responsible for remote credential management in G5 Auth.

The module assumes that the following attributes have already been defined on the model:

* `provider`: the value will always be 'g5' for G5 Auth users
* `uid`: the unique id for this user in G5 Auth
* `g5_access_token`: the current OAuth access token, if one exists

If this file is required, then protected attributes will be automatically mixed in to the g5 authenticatable model(s)

Public Instance Methods

attributes_from_auth(auth_data) click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 65
def attributes_from_auth(auth_data)
  {
    uid: auth_data.uid,
    provider: auth_data.provider,
    email: auth_data.info.email
  }.with_indifferent_access
end
auth_user() click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 26
def auth_user
  sync_auth_data
rescue OAuth2::Error => e
  logger.error("Couldn't save user credentials because: #{e}")
  raise ActiveRecord::RecordNotSaved, e.code
rescue StandardError => e
  logger.error("Couldn't save user credentials because: #{e}")
  raise ActiveRecord::RecordNotSaved, e.message
end
clean_up_passwords() click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 36
def clean_up_passwords
  self.password = self.password_confirmation = self.current_password = nil
end
revoke_g5_credentials!() click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 60
def revoke_g5_credentials!
  self.g5_access_token = nil
  save!
end
update_from_auth(auth_data) click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 75
def update_from_auth(auth_data)
  assign_attributes(attributes_from_auth(auth_data))
  update_g5_credentials(auth_data)
  update_roles_from_auth(auth_data)
end
update_g5_credentials(oauth_data) click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 56
def update_g5_credentials(oauth_data)
  self.g5_access_token = oauth_data.credentials.token
end
update_roles_from_auth(auth_data) click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 73
def update_roles_from_auth(auth_data); end
update_with_password(params) click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 45
def update_with_password(params)
  updated_attributes = params.reject do |k, v|
    k =~ /password/ && v.blank?
  end
  current_password = updated_attributes.delete(:current_password)

  if valid_current_password?(current_password)
    update_attributes(updated_attributes)
  end
end
valid_password?(password_to_check) click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 40
def valid_password?(password_to_check)
  validator = Devise::G5::AuthPasswordValidator.new(self)
  validator.valid_password?(password_to_check)
end

Private Instance Methods

sync_auth_data() click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 83
def sync_auth_data
  if new_record?
    G5::AuthUserCreator.new(self).create
  else
    G5::AuthUserUpdater.new(self).update
  end
end
valid_current_password?(current_password) click to toggle source
# File lib/devise_g5_authenticatable/models/g5_authenticatable.rb, line 91
def valid_current_password?(current_password)
  return true if valid_password?(current_password)
  error = current_password.blank? ? :blank : :invalid
  errors.add(:current_password, error)
  false
end