Module: Devise::Models::OauthTokenAuthenticatable::ClassMethods

Defined in:
lib/devise/oauth_token_authenticatable/models/oauth_token_authenticatable.rb

Instance Method Summary collapse

Instance Method Details

#find_for_oauth_token_authentication(conditions) ⇒ Object

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/devise/oauth_token_authenticatable/models/oauth_token_authenticatable.rb', line 28

def find_for_oauth_token_authentication(conditions)
  raise NotImplementedError, "You must define find_for_oauth_token_authentication in your User model"
end

#validate_oauth_token(token_str) ⇒ Object

Raises:

  • (::OAuth2::Error)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/devise/oauth_token_authenticatable/models/oauth_token_authenticatable.rb', line 32

def validate_oauth_token(token_str)
  # Make a new OAuth2 client and call the validation URL
  @@client ||= ::OAuth2::Client.new(oauth_client_id, oauth_client_secret, oauth_client_options)
  params = {                # Params for client.request
    client_id: oauth_client_id,
    access_token: token_str
  }
  access_token_opts = {}    # Params initializing AccessToken object

  opts = {}
  if @@client.options[:token_method] == :post
    headers = params.delete(:headers)
    opts[:body] = params
    opts[:headers] =  {'Content-Type' => 'application/x-www-form-urlencoded'}
    opts[:headers].merge!(headers) if headers
  else
    opts[:params] = params
  end

  response = @@client.request(@@client.options[:token_method], oauth_token_validation_url, opts)
  # Return the OAuth2::AccessToken object
  raise ::OAuth2::Error.new(response) if @@client.options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
  ::OAuth2::AccessToken.from_hash(@@client, response.parsed.merge(access_token_opts))
end