Module: SAML2::Bindings::HTTP_POST

Defined in:
lib/saml2/bindings/http_post.rb

Overview

rubocop:disable Naming/ClassAndModuleCamelCase

Constant Summary collapse

URN =
"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"

Class Method Summary collapse

Class Method Details

.decode(post_params) ⇒ [Message, String]

Decode and parse a Base64 encoded SAML message.

Parameters:

  • post_params (Hash<String => String>)

    The POST params. Will check for both SAMLRequest and SAMLResponse params.

Returns:

  • ([Message, String])

    The Message and the RelayState.

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/saml2/bindings/http_post.rb', line 18

def decode(post_params)
  base64 = post_params["SAMLRequest"] || post_params["SAMLResponse"]
  raise MissingMessage unless base64

  raise MessageTooLarge if base64.bytesize > SAML2.config[:max_message_size]

  xml = begin
    Base64.decode64(base64)
  rescue ArgumentError
    raise CorruptMessage
  end

  message = Message.parse(xml)
  [message, post_params["RelayState"]]
end

.encode(message, relay_state: nil) ⇒ Hash<String => String>

Encode a SAML message into Base64 POST params.

Parameters:

  • message (Message)
  • relay_state (defaults to: nil)

    optional [String]

Returns:

  • (Hash<String => String>)

    The POST params, including RelayState, and SAMLRequest vs. SAMLResponse chosen appropriately.



41
42
43
44
45
46
47
# File 'lib/saml2/bindings/http_post.rb', line 41

def encode(message, relay_state: nil)
  xml = message.to_s(pretty: false)
  key = message.is_a?(Request) ? "SAMLRequest" : "SAMLResponse"
  post_params = { key => Base64.encode64(xml) }
  post_params["RelayState"] = relay_state if relay_state
  post_params
end