class ADAL::WSTrustResponse

Relevant fields from a WS-Trust response.

Constants

ACTION_XPATH
ERROR_XPATH
FAULT_XPATH
SECURITY_TOKEN_XPATH
TOKEN_RESPONSE_XPATH
TOKEN_TYPE_XPATH
TOKEN_XPATH

Attributes

token[R]

Public Class Methods

new(token, token_type) click to toggle source

Constructs a WSTrustResponse.

@param String token

The content of the returned token.

@param WSTrustResponse::TokenType token_type

The type of the token contained within the WS-Trust response.
# 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
parse(raw_xml) click to toggle source

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

@param String|Nokogiri::XML raw_xml @return ADAL::WSTrustResponse

# 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) click to toggle source

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

@param Nokogiri::XML::Document xml @return String

# File lib/adal/wstrust_response.rb, line 86
def self.parse_action(xml)
  xml.xpath(ACTION_XPATH, NAMESPACES).to_s
end
parse_error(xml) click to toggle source

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

@param Nokogiri::XML::Document xml

# 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

Private Class Methods

format_xml(xml) click to toggle source

@param Nokogiri::XML::Document xml @return String

# File lib/adal/wstrust_response.rb, line 104
def self.format_xml(xml)
  xml.to_s.split("\n").map(&:strip).join
end
parse_token(xml, namespace) click to toggle source

@param Nokogiri::XML::Document @return [Nokogiri::XML::Element, Nokogiri::XML::Text]

# File lib/adal/wstrust_response.rb, line 111
def self.parse_token(xml, namespace)
  xml.xpath(TOKEN_RESPONSE_XPATH, namespace).select do |node|
    requested_token = node.xpath(SECURITY_TOKEN_XPATH, namespace)
    case requested_token.size
    when 0
      logger.warn('No security token in token response.')
      next
    when 1
      token = requested_token.xpath(TOKEN_XPATH, namespace).first
      next if token.nil?
      return token, parse_token_type(node)
    else
      fail WSTrustError, 'Found too many RequestedSecurityTokens.'
    end
  end
end
parse_token_type(token_response_node) click to toggle source

@param Nokogiri::XML::Element token_response_node @return Nokogiri::XML::Text

# File lib/adal/wstrust_response.rb, line 131
def self.parse_token_type(token_response_node)
  type = token_response_node.xpath(TOKEN_TYPE_XPATH, NAMESPACES).first
  logger.warn('No type in token response node.') if type.nil?
  type
end

Public Instance Methods

grant_type() click to toggle source

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

@return TokenRequest::GrantType

# 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