|
| 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 destination; |
| 41 | + private String issuer; |
| 42 | + |
| 43 | + /** |
| 44 | + * Mandatory constructor for the {@link AbstractSaml2AuthenticationRequest} |
| 45 | + * @param issuer - typically a URL, cannot be empty or null |
| 46 | + * @param samlRequest - the SAMLRequest XML data, SAML encoded, cannot be empty or null |
| 47 | + * @param relayState - RelayState value that accompanies the request, may be null |
| 48 | + * @param destination - The destination, a URL, where to send the XML message, cannot be empty or null |
| 49 | + */ |
| 50 | + AbstractSaml2AuthenticationRequest( |
| 51 | + String issuer, |
| 52 | + String samlRequest, |
| 53 | + String relayState, |
| 54 | + String destination) { |
| 55 | + Assert.hasText(issuer, "issuer cannot be null or empty"); |
| 56 | + Assert.hasText(samlRequest, "samlRequest cannot be null or empty"); |
| 57 | + Assert.hasText(destination, "destination cannot be null or empty"); |
| 58 | + this.issuer = issuer; |
| 59 | + this.destination = destination; |
| 60 | + this.samlRequest = samlRequest; |
| 61 | + this.relayState = relayState; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the AuthNRequest Issuer value of this SP. |
| 66 | + * @return the AuthNRequest Issuer value |
| 67 | + */ |
| 68 | + public String getIssuer() { |
| 69 | + return this.issuer; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the AuthNRequest XML value to be sent. This value is already encoded for transport. |
| 74 | + * If {@link #getBinding()} is {@link Saml2MessageBinding#REDIRECT} the value is deflated and SAML encoded. |
| 75 | + * If {@link #getBinding()} is {@link Saml2MessageBinding#POST} the value is SAML encoded. |
| 76 | + * @return the SAMLRequest parameter value |
| 77 | + */ |
| 78 | + public String getSamlRequest() { |
| 79 | + return this.samlRequest; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns the RelayState value, if present in the parameters |
| 84 | + * @return the RelayState value, or null if not available |
| 85 | + */ |
| 86 | + public String getRelayState() { |
| 87 | + return this.relayState; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns the destination that this AuthNRequest should be sent to and |
| 92 | + * the value stored inside the AuthNRequest XML |
| 93 | + * @return the destination URL for this message |
| 94 | + */ |
| 95 | + public String getDestination() { |
| 96 | + return this.destination; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns the binding this AuthNRequest will be sent and |
| 101 | + * encoded with. If {@link Saml2MessageBinding#REDIRECT} is used, the DEFLATE encoding will be automatically applied. |
| 102 | + * @return the binding this message will be sent with. |
| 103 | + */ |
| 104 | + public abstract Saml2MessageBinding getBinding(); |
| 105 | + |
| 106 | + /** |
| 107 | + * A builder for {@link AbstractSaml2AuthenticationRequest} and its subclasses. |
| 108 | + */ |
| 109 | + static class Builder<T extends Builder<T>> { |
| 110 | + String destination; |
| 111 | + String samlRequest; |
| 112 | + String relayState; |
| 113 | + String issuer; |
| 114 | + |
| 115 | + protected Builder() { |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Casting the return as the generic subtype, when returning itself |
| 120 | + * @return this object |
| 121 | + */ |
| 122 | + @SuppressWarnings("unchecked") |
| 123 | + protected final T _this() { |
| 124 | + return (T) this; |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + /** |
| 129 | + * Sets the {@code RelayState} parameter that will accompany this AuthNRequest |
| 130 | + * |
| 131 | + * @param relayState the relay state value, unencoded. if null or empty, the parameter will be removed from the |
| 132 | + * map. |
| 133 | + * @return this object |
| 134 | + */ |
| 135 | + public T relayState(String relayState) { |
| 136 | + this.relayState = relayState; |
| 137 | + return _this(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Sets the {@code issuer} value that was used to generate the request |
| 142 | + * |
| 143 | + * @param issuer the issuer state value, unencoded. |
| 144 | + * @return this object |
| 145 | + */ |
| 146 | + public T issuer(String issuer) { |
| 147 | + this.issuer = issuer; |
| 148 | + return _this(); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Sets the {@code SAMLRequest} parameter that will accompany this AuthNRequest |
| 153 | + * |
| 154 | + * @param samlRequest the SAMLRequest parameter. |
| 155 | + * @return this object |
| 156 | + */ |
| 157 | + public T samlRequest(String samlRequest) { |
| 158 | + this.samlRequest = samlRequest; |
| 159 | + return _this(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Sets the {@code destination}, a URL that will receive the AuthNRequest message |
| 164 | + * |
| 165 | + * @param destination the relay state value, unencoded. if null or empty, the parameter will be removed from the |
| 166 | + * map. |
| 167 | + * @return this object |
| 168 | + */ |
| 169 | + public T destination(String destination) { |
| 170 | + this.destination = destination; |
| 171 | + return _this(); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments