class Oauth2Token

Attributes

state[RW]

Public Class Methods

access_token(user, code, redirect_uri) click to toggle source
# File lib/oauth/models/consumers/services/oauth2_token.rb, line 19
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) click to toggle source
# 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?
  consumer.auth_code.authorize_url(options)
end
consumer() click to toggle source
# File lib/oauth/models/consumers/services/oauth2_token.rb, line 5
def self.consumer
  @consumer||=create_consumer
end
create_consumer(options={}) click to toggle source
# File lib/oauth/models/consumers/services/oauth2_token.rb, line 9
def self.create_consumer(options={})
  @consumer||=OAuth2::Client.new credentials[:key],credentials[:secret],credentials[:options]
end

Public Instance Methods

as_json(options={}) click to toggle source
# File lib/generators/active_record/oauth_provider_templates/oauth2_token.rb, line 3
def as_json(options={})
  d = {:access_token=>token, :token_type => 'bearer'}
  d[:expires_in] = expires_in if expires_at
  d
end
client() click to toggle source
# File lib/oauth/models/consumers/services/oauth2_token.rb, line 24
def client
  @client ||= OAuth2::AccessToken.new self.class.consumer, token, {refresh_token: refresh_token, expires_at: expires_at, expires_in: expires_in }
end
ensure_access() click to toggle source

Refreshes the access token to ensure access

# File lib/oauth/models/consumers/services/oauth2_token.rb, line 35
def ensure_access
  self.class.find_or_create_from_access_token user, self, client.refresh!
end
expiration_date(token) click to toggle source

Returns the expiration date (expires_in, expires_at)

@return [String, String] Expires_in and expires_at, respectively @note It will return the default expiration time as defined in the OAuth 2.0 spec when no options are set

# File lib/oauth/models/consumers/services/oauth2_token.rb, line 43
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?() click to toggle source

@return [Boolean] Is the access token expired and does the record exist in the datastore?

# File lib/oauth/models/consumers/services/oauth2_token.rb, line 29
def expired_and_existing?
  return true if !self.new_record? and Time.now.to_i >= self.expires_at.to_i
  false
end
expires_in() click to toggle source
# File lib/generators/active_record/oauth_provider_templates/oauth2_token.rb, line 17
def expires_in
  expires_at.to_i - Time.now.to_i
end
to_query() click to toggle source
# File lib/generators/active_record/oauth_provider_templates/oauth2_token.rb, line 9
def to_query
  q = "access_token=#{token}&token_type=bearer"
  q << "&state=#{URI.escape(state)}" if @state
  q << "&expires_in=#{expires_in}" if expires_at
  q << "&scope=#{URI.escape(scope)}" if scope
  q
end