Class: MSIDP::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/msidp/access_token.rb

Overview

Access token issued by Microsoft identity platform.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, expire, scope, refresh_token = nil, id_token = nil, type = 'Bearer') ⇒ AccessToken

Creates a new access token.

Parameters:

  • value (String)

    the access token string.

  • expire (Date)

    the expiration date.

  • scope (Array)

    the list of permissions.

  • refresh_token (String) (defaults to: nil)

    the refresh token.

  • id_token (String) (defaults to: nil)

    the id token.

  • type (String) (defaults to: 'Bearer')

    the token type.



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

#expireDate (readonly)

Returns the expiration date.

Returns:

  • (Date)

    the expiration date



14
15
16
# File 'lib/msidp/access_token.rb', line 14

def expire
  @expire
end

#id_tokenString (readonly)

Returns the id token (optional).

Returns:

  • (String)

    the id token (optional)



20
21
22
# File 'lib/msidp/access_token.rb', line 20

def id_token
  @id_token
end

#refresh_tokenString (readonly)

Returns the refresh token (optional).

Returns:

  • (String)

    the refresh token (optional)



18
19
20
# File 'lib/msidp/access_token.rb', line 18

def refresh_token
  @refresh_token
end

#scopeArray (readonly)

Returns the scope, a list of permissions.

Returns:

  • (Array)

    the scope, a list of permissions.



16
17
18
# File 'lib/msidp/access_token.rb', line 16

def scope
  @scope
end

#typeString (readonly)

Returns the type.

Returns:

  • (String)

    the type



22
23
24
# File 'lib/msidp/access_token.rb', line 22

def type
  @type
end

#valueString (readonly)

Returns the token string.

Returns:

  • (String)

    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.

Parameters:

  • res (String, Net::HTTPResponse)

    a query string or a HTTP respone.

  • supplement (Hash) (defaults to: {})

    attributes supplementary to the response.



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_sObject



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.

Examples:

token.valid? in: 5

Parameters:

  • kwd (Hash)

    a customizable set of options

Options Hash (**kwd):

  • :in (Integer)

    the number of seconds to offset.

Returns:

  • (Boolean)


74
75
76
# File 'lib/msidp/access_token.rb', line 74

def valid?(**kwd)
  @expire > Time.now + kwd.fetch(:in, 0)
end