Class: Oauth2Token

Inherits:
AccessToken show all
Defined in:
lib/oauth/models/consumers/services/oauth2_token.rb,
lib/generators/mongoid/oauth_provider_templates/oauth2_token.rb,
lib/generators/active_record/oauth_provider_templates/oauth2_token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OauthToken

#authorized?, #invalidate!, #invalidated?

Instance Attribute Details

#stateObject

Returns the value of attribute state.



2
3
4
# File 'lib/generators/mongoid/oauth_provider_templates/oauth2_token.rb', line 2

def state
  @state
end

Class Method Details

.access_token(user, code, redirect_uri) ⇒ Object



18
19
20
21
# File 'lib/oauth/models/consumers/services/oauth2_token.rb', line 18

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



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

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

.consumerObject



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

def self.consumer
  @consumer||=create_consumer
end

.create_consumer(options = {}) ⇒ Object



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

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

Instance Method Details

#as_json(options = {}) ⇒ Object



3
4
5
6
7
# File 'lib/generators/mongoid/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

#clientObject



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

def client
  @client ||= OAuth2::AccessToken.new self.class.consumer, token
end

#expires_inObject



17
18
19
# File 'lib/generators/mongoid/oauth_provider_templates/oauth2_token.rb', line 17

def expires_in
  expires_at.to_i - Time.now.to_i
end

#to_queryObject



9
10
11
12
13
14
15
# File 'lib/generators/mongoid/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