Class: ADAL::WSTrustResponse

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

Overview

Relevant fields from a WS-Trust response.

Defined Under Namespace

Modules: TokenType Classes: UnrecognizedTokenTypeError, WSTrustError

Constant Summary collapse

ACTION_XPATH =
'//s:Envelope/s:Header/a:Action/text()'
ERROR_XPATH =
'//s:Envelope/s:Body/s:Fault/s:Code/s:Subcode/s:Value/text()'
FAULT_XPATH =
'//s:Envelope/s:Body/s:Fault/s:Reason'
SECURITY_TOKEN_XPATH =
'./trust:RequestedSecurityToken'
TOKEN_RESPONSE_XPATH =
'//s:Envelope/s:Body/trust:RequestSecurityTokenResponse|//s:Envelope/s:' \
'Body/trust:RequestSecurityTokenResponseCollection/trust:RequestSecurit' \
'yTokenResponse'
TOKEN_TYPE_XPATH =
"./*[local-name() = 'TokenType']/text()"
TOKEN_XPATH =
"./*[local-name() = 'Assertion']"

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::DEFAULT_LOG_OUTPUT

Constants included from XmlNamespaces

XmlNamespaces::ACTION_TO_NAMESPACE, XmlNamespaces::BINDING_TO_ACTION, XmlNamespaces::NAMESPACES, XmlNamespaces::NAMESPACES_13, XmlNamespaces::NAMESPACES_2005, XmlNamespaces::WSTRUST_13, XmlNamespaces::WSTRUST_2005

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

fail_if_arguments_nil, http, string_hash

Methods included from Logging

logger

Constructor Details

#initialize(token, token_type) ⇒ WSTrustResponse

Constructs a WSTrustResponse.



147
148
149
150
151
152
153
# File 'lib/adal/wstrust_response.rb', line 147

def initialize(token, token_type)
  unless TokenType::ALL_TYPES.include? token_type
    fail UnrecognizedTokenTypeError, token_type
  end
  @token = token
  @token_type = token_type
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



138
139
140
# File 'lib/adal/wstrust_response.rb', line 138

def token
  @token
end

Class Method Details

.parse(raw_xml) ⇒ Object

Parses a WS-Trust response from raw XML into an ADAL::WSTrustResponse object. Throws an error if the response contains an error.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/adal/wstrust_response.rb', line 68

def self.parse(raw_xml)
  fail_if_arguments_nil(raw_xml)
  xml = Nokogiri::XML(raw_xml.to_s)
  parse_error(xml)
  namespace = ACTION_TO_NAMESPACE[parse_action(xml)]
  token, token_type = parse_token(xml, namespace)
  if token && token_type
    WSTrustResponse.new(format_xml(token), format_xml(token_type))
  else
    fail WSTrustError, 'Unable to parse token from response.'
  end
end

.parse_action(xml) ⇒ Object

Determines whether the response uses WS-Trust 2005 or WS-Trust 1.3.



86
87
88
# File 'lib/adal/wstrust_response.rb', line 86

def self.parse_action(xml)
  xml.xpath(ACTION_XPATH, NAMESPACES).to_s
end

.parse_error(xml) ⇒ Object

Checks a WS-Trust response for properly formatted error codes and descriptions. If found, raises an appropriate exception.



95
96
97
98
99
100
# File 'lib/adal/wstrust_response.rb', line 95

def self.parse_error(xml)
  fault = xml.xpath(FAULT_XPATH, NAMESPACES).first
  error = xml.xpath(ERROR_XPATH, NAMESPACES).first
  error = format_xml(error).split(':')[1] || error if error
  fail WSTrustError, "Fault: #{fault}. Error: #{error}." if fault || error
end

Instance Method Details

#grant_typeObject

Gets the OAuth grant type for the SAML token type of the response.



159
160
161
162
163
164
165
166
# File 'lib/adal/wstrust_response.rb', line 159

def grant_type
  case @token_type
  when TokenType::V1
    TokenRequest::GrantType::SAML1
  when TokenType::V2
    TokenRequest::GrantType::SAML2
  end
end