Class: OAuth2::AuthCode

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth20/auth_code.rb

Overview

Authorization code class represents short-lived authorization for user which should be exchanged for access token until it expires.

Constant Summary collapse

EXPIRES_IN =

Expires in 10 minutes.

10 * 60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ AuthCode

Initialize new authorization code.

Parameters:



17
18
19
20
21
22
23
24
# File 'lib/oauth20/auth_code.rb', line 17

def initialize(data)
  @key          = data[:key]
  @created_at   = data[:created_at] || Time.now
  @expires_at   = data[:expires_at] || Time.now + EXPIRES_IN
  @access_token = data[:access_token]
  @user_id      = data[:user_id]
  @client_key   = data[:client_key]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/oauth20/auth_code.rb', line 7

def access_token
  @access_token
end

#client_keyObject (readonly)

Returns the value of attribute client_key.



7
8
9
# File 'lib/oauth20/auth_code.rb', line 7

def client_key
  @client_key
end

#created_atObject (readonly)

Returns the value of attribute created_at.



7
8
9
# File 'lib/oauth20/auth_code.rb', line 7

def created_at
  @created_at
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



7
8
9
# File 'lib/oauth20/auth_code.rb', line 7

def expires_at
  @expires_at
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/oauth20/auth_code.rb', line 7

def key
  @key
end

#user_idObject (readonly)

Returns the value of attribute user_id.



7
8
9
# File 'lib/oauth20/auth_code.rb', line 7

def user_id
  @user_id
end

Class Method Details

.find_by_key(key) ⇒ Object



42
43
44
# File 'lib/oauth20/auth_code.rb', line 42

def self.find_by_key(key)
  Storage.instance.auth_code_find_by_key(key)
end

Instance Method Details

#expired?Boolean

Check if the authorization code is still valid to generate new access token.

Returns:

  • (Boolean)


29
30
31
# File 'lib/oauth20/auth_code.rb', line 29

def expired?
  Time.now > @expires_at
end

#invalidate(access_token) ⇒ Object



37
38
39
40
# File 'lib/oauth20/auth_code.rb', line 37

def invalidate(access_token)
  @access_token = access_token.key
  save
end

#saveObject



46
47
48
# File 'lib/oauth20/auth_code.rb', line 46

def save
  Storage.instance.auth_code_save(self)
end

#used?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/oauth20/auth_code.rb', line 33

def used?
  not @access_token.nil?
end