Class: Oauth2Token

Inherits:
ConsumerToken show all
Defined in:
lib/oauth/models/consumers/services/oauth2_token.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ConsumerToken

find_or_create_from_access_token

Methods included from Oauth::Models::Consumers::Token

included

Class Method Details

.access_token(user, code, redirect_uri) ⇒ Object



21
22
23
24
# File 'lib/oauth/models/consumers/services/oauth2_token.rb', line 21

def self.access_token(user, code, redirect_uri)
  access_token = consumer.auth_code.get_token(code, { :redirect_uri => redirect_uri })
  find_or_create_from_access_token user, access_token
end

.authorize_url(callback_url) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/oauth/models/consumers/services/oauth2_token.rb', line 13

def self.authorize_url(callback_url)
  options = { redirect_uri: callback_url }
  options[:scope] = credentials[:scope] if credentials[:scope].present?
  options[:access_type] = credentials[:access_type] if credentials[:access_type].present?
  options[:approval_prompt] = credentials[:approval_prompt] if credentials[:approval_prompt].present?
  consumer.auth_code.authorize_url(options)
end

.consumerObject



5
6
7
# File 'lib/oauth/models/consumers/services/oauth2_token.rb', line 5

def self.consumer
  @consumer ||= create_consumer
end

.create_consumer(options = {}) ⇒ Object



9
10
11
# File 'lib/oauth/models/consumers/services/oauth2_token.rb', line 9

def self.create_consumer(options={})
  OAuth2::Client.new(credentials[:key], credentials[:secret], credentials[:options])
end

Instance Method Details

#clientObject



26
27
28
# File 'lib/oauth/models/consumers/services/oauth2_token.rb', line 26

def client
  @client ||= OAuth2::AccessToken.new(self.class.consumer, token, { refresh_token: refresh_token, expires_at: expires_at, expires_in: expires_in })
end

#ensure_accessObject

Refreshes the access token to ensure access



37
38
39
40
41
42
43
# File 'lib/oauth/models/consumers/services/oauth2_token.rb', line 37

def ensure_access
  begin
    self.class.find_or_create_from_access_token(user, self, client.refresh!)
  rescue
    Rails.logger.info "Unable to refresh token."
  end
end

#expiration_date(token) ⇒ String

Note:

It will return the default expiration time as defined in the OAuth 2.0 spec when no options are set

Returns the expiration date (expires_in, expires_at)

Returns:

  • (String, String)

    Expires_in and expires_at, respectively



49
50
51
52
53
54
# File 'lib/oauth/models/consumers/services/oauth2_token.rb', line 49

def expiration_date(token)
  return token.expires_in, token.expires_at if !token.expires_in.nil? and !token.expires_at.nil?
  return token.expires_in, (Time.now.to_i + token.expires_in.to_i) if token.expires_at.nil? and !token.expires_in.nil?
  return (token.expires_at.to_i - Time.now.to_i), token.expires_at if token.expires_in.nil? and !token.expires_at.nil?
  return "3600", (Time.now.to_i + 3600)
end

#expired_and_existing?Boolean

Returns Is the access token expired and does the record exist in the datastore?.

Returns:

  • (Boolean)

    Is the access token expired and does the record exist in the datastore?



31
32
33
34
# File 'lib/oauth/models/consumers/services/oauth2_token.rb', line 31

def expired_and_existing?
  return true if !self.new_record? and Time.now.to_i >= self.expires_at.to_i
  false
end