|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.saml2.provider.service.authentication; |
| 18 | + |
| 19 | +import org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding; |
| 20 | +import org.springframework.util.Assert; |
| 21 | + |
| 22 | +import java.nio.charset.Charset; |
| 23 | + |
| 24 | +/** |
| 25 | + * Data holder for {@code AuthNRequest} parameters to be sent using either the |
| 26 | + * {@link Saml2MessageBinding#POST} or {@link Saml2MessageBinding#REDIRECT} binding. |
| 27 | + * Data will be encoded and possibly deflated, but will not be escaped for transport, |
| 28 | + * ie URL encoded, {@link org.springframework.web.util.UriUtils#encode(String, Charset)} |
| 29 | + * or HTML encoded, {@link org.springframework.web.util.HtmlUtils#htmlEscape(String)}. |
| 30 | + * https://www.oasis-open.org/committees/download.php/35711/sstc-saml-core-errata-2.0-wd-06-diff.pdf (line 2031) |
| 31 | + * |
| 32 | + * @see Saml2AuthenticationRequestFactory#createPostAuthenticationRequest(Saml2AuthenticationRequestContext) |
| 33 | + * @see Saml2AuthenticationRequestFactory#createRedirectAuthenticationRequest(Saml2AuthenticationRequestContext) |
| 34 | + * @since 5.3 |
| 35 | + */ |
| 36 | +abstract class AbstractSaml2AuthenticationRequest { |
| 37 | + |
| 38 | + private final String samlRequest; |
| 39 | + private final String relayState; |
| 40 | + private final String authenticationRequestUri; |
| 41 | + |
| 42 | + /** |
| 43 | + * Mandatory constructor for the {@link AbstractSaml2AuthenticationRequest} |
| 44 | + * @param samlRequest - the SAMLRequest XML data, SAML encoded, cannot be empty or null |
| 45 | + * @param relayState - RelayState value that accompanies the request, may be null |
| 46 | + * @param authenticationRequestUri - The authenticationRequestUri, a URL, where to send the XML message, cannot be empty or null |
| 47 | + */ |
| 48 | + AbstractSaml2AuthenticationRequest( |
| 49 | + String samlRequest, |
| 50 | + String relayState, |
| 51 | + String authenticationRequestUri) { |
| 52 | + Assert.hasText(samlRequest, "samlRequest cannot be null or empty"); |
| 53 | + Assert.hasText(authenticationRequestUri, "authenticationRequestUri cannot be null or empty"); |
| 54 | + this.authenticationRequestUri = authenticationRequestUri; |
| 55 | + this.samlRequest = samlRequest; |
| 56 | + this.relayState = relayState; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the AuthNRequest XML value to be sent. This value is already encoded for transport. |
| 61 | + * If {@link #getBinding()} is {@link Saml2MessageBinding#REDIRECT} the value is deflated and SAML encoded. |
| 62 | + * If {@link #getBinding()} is {@link Saml2MessageBinding#POST} the value is SAML encoded. |
| 63 | + * @return the SAMLRequest parameter value |
| 64 | + */ |
| 65 | + public String getSamlRequest() { |
| 66 | + return this.samlRequest; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns the RelayState value, if present in the parameters |
| 71 | + * @return the RelayState value, or null if not available |
| 72 | + */ |
| 73 | + public String getRelayState() { |
| 74 | + return this.relayState; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the URI endpoint that this AuthNRequest should be sent to. |
| 79 | + * @return the URI endpoint for this message |
| 80 | + */ |
| 81 | + public String getAuthenticationRequestUri() { |
| 82 | + return this.authenticationRequestUri; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the binding this AuthNRequest will be sent and |
| 87 | + * encoded with. If {@link Saml2MessageBinding#REDIRECT} is used, the DEFLATE encoding will be automatically applied. |
| 88 | + * @return the binding this message will be sent with. |
| 89 | + */ |
| 90 | + public abstract Saml2MessageBinding getBinding(); |
| 91 | + |
| 92 | + /** |
| 93 | + * A builder for {@link AbstractSaml2AuthenticationRequest} and its subclasses. |
| 94 | + */ |
| 95 | + static class Builder<T extends Builder<T>> { |
| 96 | + String authenticationRequestUri; |
| 97 | + String samlRequest; |
| 98 | + String relayState; |
| 99 | + |
| 100 | + protected Builder() { |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Casting the return as the generic subtype, when returning itself |
| 105 | + * @return this object |
| 106 | + */ |
| 107 | + @SuppressWarnings("unchecked") |
| 108 | + protected final T _this() { |
| 109 | + return (T) this; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * Sets the {@code RelayState} parameter that will accompany this AuthNRequest |
| 115 | + * |
| 116 | + * @param relayState the relay state value, unencoded. if null or empty, the parameter will be removed from the |
| 117 | + * map. |
| 118 | + * @return this object |
| 119 | + */ |
| 120 | + public T relayState(String relayState) { |
| 121 | + this.relayState = relayState; |
| 122 | + return _this(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Sets the {@code SAMLRequest} parameter that will accompany this AuthNRequest |
| 127 | + * |
| 128 | + * @param samlRequest the SAMLRequest parameter. |
| 129 | + * @return this object |
| 130 | + */ |
| 131 | + public T samlRequest(String samlRequest) { |
| 132 | + this.samlRequest = samlRequest; |
| 133 | + return _this(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Sets the {@code authenticationRequestUri}, a URL that will receive the AuthNRequest message |
| 138 | + * |
| 139 | + * @param authenticationRequestUri the relay state value, unencoded. |
| 140 | + * @return this object |
| 141 | + */ |
| 142 | + public T authenticationRequestUri(String authenticationRequestUri) { |
| 143 | + this.authenticationRequestUri = authenticationRequestUri; |
| 144 | + return _this(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments