Module: Oauth::Models::Consumers::Token::ClassMethods

Defined in:
lib/oauth/models/consumers/token.rb

Instance Method Summary collapse

Instance Method Details

#build_user_from_tokenObject



84
85
# File 'lib/oauth/models/consumers/token.rb', line 84

def build_user_from_token
end

#consumerObject



24
25
26
27
# File 'lib/oauth/models/consumers/token.rb', line 24

def consumer
  options = credentials[:options] || {}
  @consumer||=OAuth::Consumer.new credentials[:key],credentials[:secret],options
end

#credentialsObject



87
88
89
# File 'lib/oauth/models/consumers/token.rb', line 87

def credentials
  @credentials||=OAUTH_CREDENTIALS[service_name]
end

#find_or_create_from_access_token(user, access_token, new_token = nil) ⇒ Object

Finds, creates or updates a ConsumerToken by finding the token or taking it when it’s given. It then updates the attributes and saves the changes/new record to a datastore. Usage example: find_or_create_from_access_token(current_user, access_token) <– Find or create a new access token find_or_create_from_access-token(current_user, Oauth2Token.last, client.refresh!) <– Edits existing record with new refreshed information

Parameters:

  • user (User)

    The user to which the access token should belong to

  • access_token (AccessToken || Oauth2Token)

    Either a request token taken from the service or a ConsumerToken

  • new_token (AccessToken) (defaults to: nil)

    A new access token, used for refreshing the access token with OAuth 2.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/oauth/models/consumers/token.rb', line 50

def find_or_create_from_access_token(user, access_token, new_token = nil)
  if access_token.class.ancestors.include?(Oauth2Token)
    token = access_token
  else
    if user
      token = self.find_or_initialize_by(user_id: user.id, token: access_token.token)
    else
      token = self.find_or_initialize_by(token: access_token.token)
    end
  end

  token = if new_token then set_details(new_token, access_token) else set_details(access_token, token) end

  token.save! if token.new_record? or token.changed?

  token
end

#find_or_create_from_request_token(user, token, secret, oauth_verifier) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/oauth/models/consumers/token.rb', line 33

def find_or_create_from_request_token(user,token,secret,oauth_verifier)
  request_token=OAuth::RequestToken.new consumer,token,secret
  options={}
  options[:oauth_verifier]=oauth_verifier if oauth_verifier
  access_token=request_token.get_access_token options
  find_or_create_from_access_token user, access_token
end

#get_request_token(callback_url) ⇒ Object



29
30
31
# File 'lib/oauth/models/consumers/token.rb', line 29

def get_request_token(callback_url)
  consumer.get_request_token(:oauth_callback=>callback_url)
end

#service_nameObject



20
21
22
# File 'lib/oauth/models/consumers/token.rb', line 20

def service_name
  @service_name||=self.to_s.underscore.scan(/^(.*?)(_token)?$/)[0][0].to_sym
end

#set_details(access_token, token) ⇒ ConsumerToken

Set the details such as the secret, refresh token and expiration time to an instance of ConsumerToken

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/oauth/models/consumers/token.rb', line 70

def set_details(access_token, token)
  secret = access_token.respond_to?(:secret) ? access_token.secret : nil
  refresh_token = access_token.respond_to?(:refresh_token) ? access_token.refresh_token : nil
  expires_in, expires_at = token.expiration_date(access_token) if token.class.ancestors.include?(Oauth2Token)

  token.token = access_token.token
  token.refresh_token = refresh_token
  token.secret = secret
  token.expires_at = expires_at
  token.expires_in = expires_in

  token
end