Class: ADAL::SuccessResponse

Inherits:
TokenResponse show all
Includes:
Logging
Defined in:
lib/adal/token_response.rb

Overview

A token response that contains an access token. All fields are read only and may be nil. Some fields are only populated in certain flows.

Constant Summary collapse

OAUTH_FIELDS =

These fields may or may not be included in the response from the token endpoint.

[:access_token, :expires_in, :expires_on, :id_token,
:not_before, :refresh_token, :resource, :scope, :token_type]

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::DEFAULT_LOG_OUTPUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Methods inherited from TokenResponse

#error?, parse

Constructor Details

#initialize(fields = {}) ⇒ SuccessResponse

Constructs a SuccessResponse from a collection of fields returned from a token endpoint.

Parameters:

  • Hash


82
83
84
85
86
87
88
89
90
91
# File 'lib/adal/token_response.rb', line 82

def initialize(fields = {})
  @fields = fields
  fields.each { |k, v| instance_variable_set("@#{k}", v) }
  parse_id_token(id_token)
  @expires_on = @expires_in.to_i + Time.now.to_i
  logger.info('Parsed a SuccessResponse with access token digest ' \
              "#{Digest::SHA256.hexdigest @access_token.to_s} and " \
              'refresh token digest ' \
              "#{Digest::SHA256.hexdigest @refresh_token.to_s}.")
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



75
76
77
# File 'lib/adal/token_response.rb', line 75

def fields
  @fields
end

#user_infoObject (readonly)

Returns the value of attribute user_info.



74
75
76
# File 'lib/adal/token_response.rb', line 74

def 
  @user_info
end

Instance Method Details

#parse_id_token(id_token) ⇒ Object

Parses the raw id token into an ADAL::UserInformation. If the id token is missing, an ADAL::UserInformation will still be generated, it just won’t contain any displayable information.

Parameters:

  • String

    id_token The id token to parse Adds an id token to the token response if one is not present



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/adal/token_response.rb', line 113

def parse_id_token(id_token)
  if id_token.nil?
    logger.warn('No id token found.')
    @user_info ||= ADAL::UserInformation.new(unique_id: SecureRandom.uuid)
    return
  end
  logger.verbose('Attempting to decode id token in token response.')
  claims = JWT.decode(id_token.to_s, nil, false).first
  @id_token = id_token
  @user_info = ADAL::UserInformation.new(claims)
end

#to_json(_ = nil) ⇒ Object

Converts the fields that were used to create this token response into a JSON string. This is helpful for storing then in a database.

Parameters:

  • JSON::Ext::Generator::State

    We don’t care about this, because the JSON representation of this object does not depend on the fields before it.

Returns:

  • String



101
102
103
# File 'lib/adal/token_response.rb', line 101

def to_json(_ = nil)
  JSON.unparse(fields)
end