Class: OAuth2::AccessToken

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ AccessToken

Initialize new access token instance with given attributes.

Parameters:

  • Client (OAuth2::Client)

    for which the token is created.

  • User (OAuth2::User)

    on which behalf token is created.

  • Hash (Hash)

    of additional options.



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_keyObject (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_atObject (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_atObject (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_inObject (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

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/oauth20/access_token.rb', line 9

def key
  @key
end

#scopeObject (readonly)

Returns the value of attribute scope.



9
10
11
# File 'lib/oauth20/access_token.rb', line 9

def scope
  @scope
end

#token_typeObject (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_idObject (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.

Returns:

  • (Boolean)


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

#saveObject



67
68
69
# File 'lib/oauth20/access_token.rb', line 67

def save
  Storage.instance.access_token_save(self)
end

#to_jsonObject

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