Class: ADAL::AuthenticationParameters

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
Util
Defined in:
lib/adal/authentication_parameters.rb

Overview

Authentication parameters from an unauthorized 401 response from a resource server that can be used to create an AuthenticationContext.

Constant Summary collapse

AUTHENTICATE_HEADER =
'www-authenticate'
AUTHORITY_KEY =
'authorization_uri'
RESOURCE_KEY =
'resource'

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::DEFAULT_LOG_OUTPUT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger

Methods included from Util

#fail_if_arguments_nil, #http, #string_hash

Constructor Details

#initialize(authority_uri, resource = nil) ⇒ AuthenticationParameters

Constructs a new AuthenticationParameters.

Parameters:

  • String|URI

    authority_uri The uri of the authority server, including both host and tenant.

  • String


112
113
114
115
116
# File 'lib/adal/authentication_parameters.rb', line 112

def initialize(authority_uri, resource = nil)
  fail_if_arguments_nil(authority_uri)
  @authority_uri = URI.parse(authority_uri.to_s)
  @resource = resource
end

Instance Attribute Details

#authority_uriObject (readonly)

Returns the value of attribute authority_uri.



48
49
50
# File 'lib/adal/authentication_parameters.rb', line 48

def authority_uri
  @authority_uri
end

#resourceObject (readonly)

Returns the value of attribute resource.



49
50
51
# File 'lib/adal/authentication_parameters.rb', line 49

def resource
  @resource
end

Class Method Details

.create_from_authenticate_header(challenge) ⇒ Object

Creates an AuthenticationParameters object from a www-authenticate response header.

Parameters:

  • String

    challenge The raw www-authenticate header.

Returns:

  • AuthenticationParameters



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/adal/authentication_parameters.rb', line 77

def self.create_from_authenticate_header(challenge)
  params = parse_challenge(challenge)
  if params.nil? || !params.key?(AUTHORITY_KEY)
    logger.warn('Unable to create AuthenticationParameters from header ' \
                "#{challenge}.")
    return
  end
  logger.verbose("Authentication header #{challenge} was successfully " \
                 'parsed as an OAuth challenge into a parameters hash.')
  AuthenticationParameters.new(
    params[AUTHORITY_KEY], params[RESOURCE_KEY])
end

.create_from_resource_url(resource_url) ⇒ Object

Creates authentication parameters from the address of the resource. The resource server must respond with 401 unauthorized response with a www-authenticate header containing the authentication parameters.

Parameters:

  • URI

    resource_url The address of the desired resource.

Returns:

  • AuthenticationParameters



59
60
61
62
63
64
65
66
67
68
# File 'lib/adal/authentication_parameters.rb', line 59

def self.create_from_resource_url(resource_url)
  logger.verbose('Attempting to retrieve authentication parameters from ' \
                 "#{resource_url}.")
  response = Net::HTTP.post_form(URI.parse(resource_url.to_s), {})
  unless response.key? AUTHENTICATE_HEADER
    fail ArgumentError, 'The specified resource uri does not support ' \
      'OAuth challenges.'
  end
  create_from_authenticate_header(response[AUTHENTICATE_HEADER])
end

Instance Method Details

#create_contextObject

Creates an AuthenticationContext based on the parameters.

Returns:

  • AuthenticationContext



122
123
124
# File 'lib/adal/authentication_parameters.rb', line 122

def create_context
  AuthenticationContext.new(@authority_uri.host, @authority_uri.path[1..-1])
end