Class: MSIDP::AccessToken
- Inherits:
-
Object
- Object
- MSIDP::AccessToken
- Defined in:
- lib/msidp/access_token.rb
Overview
Access token issued by Microsoft identity platform.
Instance Attribute Summary collapse
-
#expire ⇒ Date
readonly
The expiration date.
-
#id_token ⇒ String
readonly
The id token (optional).
-
#refresh_token ⇒ String
readonly
The refresh token (optional).
-
#scope ⇒ Array
readonly
The scope, a list of permissions.
-
#type ⇒ String
readonly
The type.
-
#value ⇒ String
readonly
The token string.
Class Method Summary collapse
-
.parse(res, supplement = {}) ⇒ Object
Parses a response from the endpoint and creates an access token object.
Instance Method Summary collapse
-
#initialize(value, expire, scope, refresh_token = nil, id_token = nil, type = 'Bearer') ⇒ AccessToken
constructor
Creates a new access token.
- #to_s ⇒ Object
-
#valid?(**kwd) ⇒ Boolean
Checks if the token is not expired.
Constructor Details
#initialize(value, expire, scope, refresh_token = nil, id_token = nil, type = 'Bearer') ⇒ AccessToken
Creates a new access token.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/msidp/access_token.rb', line 32 def initialize( # rubocop:disable Metrics/ParameterLists value, expire, scope, refresh_token = nil, id_token = nil, type = 'Bearer' ) @value = value @scope = scope @expire = expire @refresh_token = refresh_token @id_token = id_token @type = type end |
Instance Attribute Details
#expire ⇒ Date (readonly)
Returns the expiration date.
14 15 16 |
# File 'lib/msidp/access_token.rb', line 14 def expire @expire end |
#id_token ⇒ String (readonly)
Returns the id token (optional).
20 21 22 |
# File 'lib/msidp/access_token.rb', line 20 def id_token @id_token end |
#refresh_token ⇒ String (readonly)
Returns the refresh token (optional).
18 19 20 |
# File 'lib/msidp/access_token.rb', line 18 def refresh_token @refresh_token end |
#scope ⇒ Array (readonly)
Returns the scope, a list of permissions.
16 17 18 |
# File 'lib/msidp/access_token.rb', line 16 def scope @scope end |
#type ⇒ String (readonly)
Returns the type.
22 23 24 |
# File 'lib/msidp/access_token.rb', line 22 def type @type end |
#value ⇒ String (readonly)
Returns the token string.
12 13 14 |
# File 'lib/msidp/access_token.rb', line 12 def value @value end |
Class Method Details
.parse(res, supplement = {}) ⇒ Object
Parses a response from the endpoint and creates an access token object.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/msidp/access_token.rb', line 48 def self.parse(res, supplement = {}) # rubocop:disable Metrics/AbcSize case res when String hash = Hash[URI.decode_www_form(res)] date = Time.now - 1 when Net::HTTPResponse hash = JSON.parse(res.body) date = res.key?('date') ? Time.parse(res['date']) : (Time.now - 1) else raise TypeError, 'expected String or Net::HTTPResponse' end hash = supplement.transform_keys(&:to_s).merge(hash) AccessToken.new( hash['access_token'], date + hash['expires_in'].to_i, hash['scope'].split(' '), hash['refresh_token'], hash['id_token'], hash['token_type'] ) end |
Instance Method Details
#to_s ⇒ Object
78 79 80 |
# File 'lib/msidp/access_token.rb', line 78 def to_s @value end |
#valid?(**kwd) ⇒ Boolean
Checks if the token is not expired.
74 75 76 |
# File 'lib/msidp/access_token.rb', line 74 def valid?(**kwd) @expire > Time.now + kwd.fetch(:in, 0) end |