Class: OAuth2::AccessToken
- Inherits:
-
Object
- Object
- OAuth2::AccessToken
- Defined in:
- lib/oauth20/access_token.rb
Overview
Access token represents aceess created on behalf of specific user which first successfully authenticated with authroization server. It can be used to access resource server protected resources until it expires.
Constant Summary collapse
- EXPIRES_IN =
Default timeout in seconds for access token expiration.
3600
- TYPE_BEARED =
Beared token type.
'Beared'
Instance Attribute Summary collapse
-
#client_key ⇒ Object
readonly
Returns the value of attribute client_key.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#token_type ⇒ Object
readonly
Returns the value of attribute token_type.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Class Method Summary collapse
Instance Method Summary collapse
-
#expired? ⇒ Boolean
Check if the access token is valid to be used to access protected resource on the server.
-
#initialize(data) ⇒ AccessToken
constructor
Initialize new access token instance with given attributes.
-
#revoke! ⇒ Object
Revoke the access token.
- #save ⇒ Object
-
#to_json ⇒ Object
Return the token description data according to oauth protocol specification.
Constructor Details
#initialize(data) ⇒ AccessToken
Initialize new access token instance with given attributes.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/oauth20/access_token.rb', line 24 def initialize(data) @client_key = data[:client_key] @user_id = data[:user_id] @expires_in = data[:expires_in] || EXPIRES_IN @created_at = data[:created_at] || Time.now @expires_at = data[:expires_at] || Time.now + EXPIRES_IN @scope = data[:scope] @key = data[:key] || OAuth2::Utils.generate_key @token_type = data[:token_type] || TYPE_BEARED end |
Instance Attribute Details
#client_key ⇒ Object (readonly)
Returns the value of attribute client_key.
9 10 11 |
# File 'lib/oauth20/access_token.rb', line 9 def client_key @client_key end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
9 10 11 |
# File 'lib/oauth20/access_token.rb', line 9 def created_at @created_at end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
9 10 11 |
# File 'lib/oauth20/access_token.rb', line 9 def expires_at @expires_at end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
9 10 11 |
# File 'lib/oauth20/access_token.rb', line 9 def expires_in @expires_in end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/oauth20/access_token.rb', line 9 def key @key end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
9 10 11 |
# File 'lib/oauth20/access_token.rb', line 9 def scope @scope end |
#token_type ⇒ Object (readonly)
Returns the value of attribute token_type.
9 10 11 |
# File 'lib/oauth20/access_token.rb', line 9 def token_type @token_type end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
9 10 11 |
# File 'lib/oauth20/access_token.rb', line 9 def user_id @user_id end |
Class Method Details
.find_by_key(key) ⇒ Object
63 64 65 |
# File 'lib/oauth20/access_token.rb', line 63 def self.find_by_key(key) Storage.instance.access_token_find_by_key(key) end |
Instance Method Details
#expired? ⇒ Boolean
Check if the access token is valid to be used to access protected resource on the server.
38 39 40 |
# File 'lib/oauth20/access_token.rb', line 38 def expired? Time.now >= @expires_at end |
#revoke! ⇒ Object
Revoke the access token. Its no longer valid to be used to access protected resources.
45 46 47 48 |
# File 'lib/oauth20/access_token.rb', line 45 def revoke! @expires_at = Time.now save end |
#save ⇒ Object
67 68 69 |
# File 'lib/oauth20/access_token.rb', line 67 def save Storage.instance.access_token_save(self) end |
#to_json ⇒ Object
Return the token description data according to oauth protocol specification. Rest of token attributes is avoided.
53 54 55 56 57 58 59 60 61 |
# File 'lib/oauth20/access_token.rb', line 53 def to_json data = { 'access_token' => @key, 'expires_in' => @expires_in, 'token_type' => @token_type } data.to_json end |