class GrapeTokenAuth::OmniAuthResource

Attributes

auth_hash[R]
omniauth_params[R]
resource[R]

Public Class Methods

fetch_or_create(resource_class, auth_hash, oauth_params) click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 25
def self.fetch_or_create(resource_class, auth_hash, oauth_params)
  resource = resource_class.where(
    uid:      auth_hash['uid'],
    provider: auth_hash['provider']).first_or_initialize
  new(resource, auth_hash, oauth_params)
end
new(resource, auth_hash, omniauth_params) click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 11
def initialize(resource, auth_hash, omniauth_params)
  @resource        = resource
  @auth_hash       = auth_hash
  @omniauth_params = omniauth_params
end

Public Instance Methods

attributes() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 40
def attributes
  { 'auth_token' => token_value,
    'client_id' => token.client_id,
    'expiry' => token.expiry }.merge(resource.serializable_hash)
end
persist_oauth_attributes!() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 17
def persist_oauth_attributes!
  set_crazy_password
  sync_token_to_resource
  sync_attributes_to_resource
  # skip_confirmable_email
  resource.save!
end
token() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 32
def token
  @token ||= Token.new
end
token_value() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 36
def token_value
  token.to_s
end

Private Instance Methods

assign_extra_attributes() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 75
def assign_extra_attributes
  extra_params = whitelisted_params
  resource.assign_attributes(extra_params) if extra_params
end
assign_provider_attrs() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 67
def assign_provider_attrs
  info_hash = auth_hash['info']
  attrs = %i(nickname name image email).each_with_object({}) do |k, hsh|
    hsh[k] = info_hash.fetch(k, '')
  end
  resource.assign_attributes(attrs)
end
scope() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 91
def scope
  klass = resource.class
  @scope ||= GrapeTokenAuth.configuration.mappings
             .find { |k,v| v == klass }.try(:[], 0)
end
set_crazy_password() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 50
def set_crazy_password
  # set crazy password for new oauth users. this is only used to prevent
  # access via email sign-in.
  return if resource.id
  p = SecureRandom.urlsafe_base64(nil, false)
  resource.password = p
  resource.password_confirmation = p
end
sync_attributes_to_resource() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 59
def sync_attributes_to_resource
  # sync user info with provider, update/generate auth token
  assign_provider_attrs

  # assign any additional (whitelisted) attributes
  assign_extra_attributes
end
sync_token_to_resource() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 97
def sync_token_to_resource
  resource.tokens[token.client_id] = token.to_h
end
whitelisted_params() click to toggle source
# File lib/grape_token_auth/omniauth/omniauth_resource.rb, line 80
def whitelisted_params
  whitelist = GrapeTokenAuth.configuration.param_white_list
  return unless whitelist
  scoped_list = whitelist[scope] || whitelist[scope.to_s]
  return unless scoped_list
  scoped_list.each_with_object({}) do |key, permitted|
    value = Utility.find_with_indifference(omniauth_params, key)
    permitted[key] = value if value
  end
end