Class: Spid::Saml2::AuthnRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/spid/saml2/authn_request.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid: nil, settings:) ⇒ AuthnRequest

Returns a new instance of AuthnRequest.



11
12
13
14
15
# File 'lib/spid/saml2/authn_request.rb', line 11

def initialize(uuid: nil, settings:)
  @document = REXML::Document.new
  @uuid = uuid
  @settings = settings
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/spid/saml2/authn_request.rb', line 8

def document
  @document
end

#settingsObject (readonly)

Returns the value of attribute settings.



9
10
11
# File 'lib/spid/saml2/authn_request.rb', line 9

def settings
  @settings
end

Instance Method Details

#authn_context_class_refObject



90
91
92
93
94
95
96
97
# File 'lib/spid/saml2/authn_request.rb', line 90

def authn_context_class_ref
  @authn_context_class_ref ||=
    begin
      element = REXML::Element.new("saml:AuthnContextClassRef")
      element.text = settings.authn_context
      element
    end
end

#authn_requestObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spid/saml2/authn_request.rb', line 22

def authn_request
  @authn_request ||=
    begin
      element = REXML::Element.new("samlp:AuthnRequest")
      element.add_attributes(authn_request_attributes)
      element.add_element(issuer)
      element.add_element(name_id_policy)
      element.add_element(requested_authn_context)
      element
    end
end

#authn_request_attributesObject

rubocop:disable Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spid/saml2/authn_request.rb', line 35

def authn_request_attributes
  @authn_request_attributes ||=
    begin
      attributes = {
        "xmlns:samlp" => "urn:oasis:names:tc:SAML:2.0:protocol",
        "xmlns:saml" => "urn:oasis:names:tc:SAML:2.0:assertion",
        "ID" => uuid,
        "Version" => "2.0",
        "IssueInstant" => issue_instant,
        "Destination" => settings.idp_sso_target_url,
        "AssertionConsumerServiceIndex" => settings.acs_index,
        "AttributeConsumingServiceIndex" => settings.attribute_index
      }
      attributes["ForceAuthn"] = true if settings.force_authn?
      attributes
    end
end

#issue_instantObject



99
100
101
# File 'lib/spid/saml2/authn_request.rb', line 99

def issue_instant
  @issue_instant ||= Time.now.utc.iso8601
end

#issuerObject

rubocop:enable Metrics/MethodLength



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/spid/saml2/authn_request.rb', line 54

def issuer
  @issuer ||=
    begin
      element = REXML::Element.new("saml:Issuer")
      element.add_attributes(
        "Format" => "urn:oasis:names:tc:SAML:2.0:nameid-format:entity",
        "NameQualifier" => settings.sp_entity_id
      )
      element.text = settings.sp_entity_id
      element
    end
end

#name_id_policyObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/spid/saml2/authn_request.rb', line 67

def name_id_policy
  @name_id_policy ||=
    begin
      element = REXML::Element.new("samlp:NameIDPolicy")
      element.add_attributes(
        "Format" => "urn:oasis:names:tc:SAML:2.0:nameid-format:transient"
      )
      element
    end
end

#requested_authn_contextObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/spid/saml2/authn_request.rb', line 78

def requested_authn_context
  @requested_authn_context ||=
    begin
      element = REXML::Element.new("samlp:RequestedAuthnContext")
      element.add_attributes(
        "Comparison" => Spid::MINIMUM_COMPARISON
      )
      element.add_element(authn_context_class_ref)
      element
    end
end

#to_samlObject



17
18
19
20
# File 'lib/spid/saml2/authn_request.rb', line 17

def to_saml
  document.add_element(authn_request)
  document.to_s
end

#uuidObject



103
104
105
# File 'lib/spid/saml2/authn_request.rb', line 103

def uuid
  @uuid ||= "_#{SecureRandom.uuid}"
end