19
19
import org .springframework .security .saml2 .credentials .Saml2X509Credential ;
20
20
import org .springframework .util .Assert ;
21
21
22
+ import java .util .Collection ;
22
23
import java .util .LinkedList ;
23
24
import java .util .List ;
25
+ import java .util .function .Consumer ;
24
26
25
27
/**
26
28
* Data holder for information required to send an {@code AuthNRequest}
27
29
* from the service provider to the identity provider
30
+ * https://www.oasis-open.org/committees/download.php/35711/sstc-saml-core-errata-2.0-wd-06-diff.pdf (line 2031)
28
31
*
29
32
* @see {@link Saml2AuthenticationRequestFactory}
30
- * @see https://www.oasis-open.org/committees/download.php/35711/sstc-saml-core-errata-2.0-wd-06-diff.pdf (line 2031)
31
33
* @since 5.2
32
34
*/
33
- public class Saml2AuthenticationRequest {
34
- private final String localSpEntityId ;
35
+ public final class Saml2AuthenticationRequest {
36
+ private final String issuer ;
35
37
private final List <Saml2X509Credential > credentials ;
36
- private String webSsoUri ;
38
+ private final String destination ;
39
+ private final String assertionConsumerServiceUrl ;
37
40
38
- public Saml2AuthenticationRequest (String localSpEntityId , String webSsoUri , List <Saml2X509Credential > credentials ) {
39
- Assert .hasText (localSpEntityId , "localSpEntityId cannot be null" );
40
- Assert .hasText (localSpEntityId , "webSsoUri cannot be null" );
41
- this .localSpEntityId = localSpEntityId ;
42
- this .webSsoUri = webSsoUri ;
41
+ private Saml2AuthenticationRequest (
42
+ String issuer ,
43
+ String destination ,
44
+ String assertionConsumerServiceUrl ,
45
+ List <Saml2X509Credential > credentials ) {
46
+ Assert .hasText (issuer , "issuer cannot be null" );
47
+ Assert .hasText (destination , "destination cannot be null" );
48
+ Assert .hasText (assertionConsumerServiceUrl , "spAssertionConsumerServiceUrl cannot be null" );
49
+ this .issuer = issuer ;
50
+ this .destination = destination ;
51
+ this .assertionConsumerServiceUrl = assertionConsumerServiceUrl ;
43
52
this .credentials = new LinkedList <>();
44
53
for (Saml2X509Credential c : credentials ) {
45
54
if (c .isSigningCredential ()) {
@@ -50,15 +59,126 @@ public Saml2AuthenticationRequest(String localSpEntityId, String webSsoUri, List
50
59
}
51
60
52
61
53
- public String getLocalSpEntityId () {
54
- return this .localSpEntityId ;
62
+ /**
63
+ * returns the issuer, the local SP entity ID, for this authentication request.
64
+ * This property should be used to populate the {@code AuthNRequest.Issuer} XML element.
65
+ * This value typically is a URI, but can be an arbitrary string.
66
+ * @return issuer
67
+ */
68
+ public String getIssuer () {
69
+ return this .issuer ;
55
70
}
56
71
57
- public String getWebSsoUri () {
58
- return this .webSsoUri ;
72
+ /**
73
+ * returns the destination, the WEB Single Sign On URI, for this authentication request.
74
+ * This property populates the {@code AuthNRequest#Destination} XML attribute.
75
+ * @return destination
76
+ */
77
+ public String getDestination () {
78
+ return this .destination ;
59
79
}
60
80
81
+ /**
82
+ * Returns the desired {@code AssertionConsumerServiceUrl} that this SP wishes to receive the
83
+ * assertion on. The IDP may or may not honor this request.
84
+ * This property populates the {@code AuthNRequest#AssertionConsumerServiceURL} XML attribute.
85
+ * @return the AssertionConsumerServiceURL value
86
+ */
87
+ public String getAssertionConsumerServiceUrl () {
88
+ return assertionConsumerServiceUrl ;
89
+ }
90
+
91
+ /**
92
+ * Returns a list of credentials that can be used to sign the {@code AuthNRequest} object
93
+ * @return signing credentials
94
+ */
61
95
public List <Saml2X509Credential > getCredentials () {
62
96
return this .credentials ;
63
97
}
98
+
99
+ /**
100
+ * A builder for {@link Saml2AuthenticationRequest}.
101
+ * returns a builder object
102
+ */
103
+ public static Builder builder () {
104
+ return new Builder ();
105
+ }
106
+
107
+ /**
108
+ * A builder for {@link Saml2AuthenticationRequest}.
109
+ */
110
+ public static class Builder {
111
+ private String issuer ;
112
+ private List <Saml2X509Credential > credentials = new LinkedList <>();
113
+ private String destination ;
114
+ private String assertionConsumerServiceUrl ;
115
+
116
+ private Builder () {
117
+ }
118
+
119
+ /**
120
+ * Sets the issuer for the authentication request.
121
+ * @param issuer - a required value
122
+ * @return this {@code Builder}
123
+ */
124
+ public Builder issuer (String issuer ) {
125
+ this .issuer = issuer ;
126
+ return this ;
127
+ }
128
+
129
+ /**
130
+ * Modifies the collection of {@link Saml2X509Credential} credentials
131
+ * used in communication between IDP and SP, specifically signing the
132
+ * authentication request.
133
+ * For example:
134
+ * <code>
135
+ * Saml2X509Credential credential = ...;
136
+ * return Saml2AuthenticationRequest.withLocalSpEntityId("id")
137
+ * .credentials(c -> c.add(credential))
138
+ * ...
139
+ * .build();
140
+ * </code>
141
+ * @param credentials - a consumer that can modify the collection of credentials
142
+ * @return this object
143
+ */
144
+ public Builder credentials (Consumer <Collection <Saml2X509Credential >> credentials ) {
145
+ credentials .accept (this .credentials );
146
+ return this ;
147
+ }
148
+
149
+ /**
150
+ * Sets the Destination for the authentication request. Typically the {@code Service Provider EntityID}
151
+ * @param destination - a required value
152
+ * @return this {@code Builder}
153
+ */
154
+ public Builder destination (String destination ) {
155
+ this .destination = destination ;
156
+ return this ;
157
+ }
158
+
159
+ /**
160
+ * Sets the {@code assertionConsumerServiceURL} for the authentication request.
161
+ * Typically the {@code Service Provider EntityID}
162
+ * @param assertionConsumerServiceUrl - a required value
163
+ * @return this {@code Builder}
164
+ */
165
+ public Builder assertionConsumerServiceUrl (String assertionConsumerServiceUrl ) {
166
+ this .assertionConsumerServiceUrl = assertionConsumerServiceUrl ;
167
+ return this ;
168
+ }
169
+
170
+ /**
171
+ * Creates a {@link Saml2AuthenticationRequest} object.
172
+ * @return the Saml2AuthenticationRequest object
173
+ * @throws {@link IllegalArgumentException} if a required property is not set
174
+ */
175
+ public Saml2AuthenticationRequest build () {
176
+ return new Saml2AuthenticationRequest (
177
+ this .issuer ,
178
+ this .destination ,
179
+ this .assertionConsumerServiceUrl ,
180
+ this .credentials
181
+ );
182
+ }
183
+ }
64
184
}
0 commit comments