class MSIDP::AccessToken

Access token issued by Microsoft identity platform.

Attributes

expire[R]

@return [Date] the expiration date

id_token[R]

@return [String] the id token (optional)

refresh_token[R]

@return [String] the refresh token (optional)

scope[R]

@return [Array] the scope, a list of permissions.

type[R]

@return [String] the type

value[R]

@return [String] the token string

Public Class Methods

new( value, expire, scope, refresh_token = nil, id_token = nil, type = 'Bearer' ) click to toggle source

Creates a new access token.

@param [String] value the access token string. @param [Date] expire the expiration date. @param [Array] scope the list of permissions. @param [String] refresh_token the refresh token. @param [String] id_token the id token. @param [String] type the token type.

# 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
parse(res, supplement = {}) click to toggle source

Parses a response from the endpoint and creates an access token object.

@param [String,Net::HTTPResponse] res a query string or a HTTP respone. @param [Hash] supplement attributes supplementary to the response.

# 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

Public Instance Methods

to_s() click to toggle source
# File lib/msidp/access_token.rb, line 78
def to_s
  @value
end
valid?(**kwd) click to toggle source

Checks if the token is not expired.

@option kwd [Integer] :in the number of seconds to offset.

@example

token.valid? in: 5
# File lib/msidp/access_token.rb, line 74
def valid?(**kwd)
  @expire > Time.now + kwd.fetch(:in, 0)
end