|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.oauth2.core.endpoint; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.time.temporal.ChronoUnit; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.springframework.security.oauth2.core.OAuth2DeviceCode; |
| 25 | +import org.springframework.security.oauth2.core.OAuth2UserCode; |
| 26 | +import org.springframework.util.Assert; |
| 27 | +import org.springframework.util.CollectionUtils; |
| 28 | + |
| 29 | +/** |
| 30 | + * A representation of an OAuth 2.0 Device Authorization Response. |
| 31 | + * |
| 32 | + * @author Steve Riesenberg |
| 33 | + * @since 6.1 |
| 34 | + * @see OAuth2DeviceCode |
| 35 | + * @see OAuth2UserCode |
| 36 | + * @see <a target="_blank" href="https://tools.ietf.org/html/rfc8628#section-3.2">Section |
| 37 | + * 3.2 Device Authorization Response</a> |
| 38 | + */ |
| 39 | +public final class OAuth2DeviceAuthorizationResponse { |
| 40 | + |
| 41 | + private OAuth2DeviceCode deviceCode; |
| 42 | + |
| 43 | + private OAuth2UserCode userCode; |
| 44 | + |
| 45 | + private String verificationUri; |
| 46 | + |
| 47 | + private String verificationUriComplete; |
| 48 | + |
| 49 | + private long interval; |
| 50 | + |
| 51 | + private Map<String, Object> additionalParameters; |
| 52 | + |
| 53 | + private OAuth2DeviceAuthorizationResponse() { |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns the {@link OAuth2DeviceCode Device Code}. |
| 58 | + * @return the {@link OAuth2DeviceCode} |
| 59 | + */ |
| 60 | + public OAuth2DeviceCode getDeviceCode() { |
| 61 | + return this.deviceCode; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the {@link OAuth2UserCode User Code}. |
| 66 | + * @return the {@link OAuth2UserCode} |
| 67 | + */ |
| 68 | + public OAuth2UserCode getUserCode() { |
| 69 | + return this.userCode; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the end-user verification URI. |
| 74 | + * @return the end-user verification URI |
| 75 | + */ |
| 76 | + public String getVerificationUri() { |
| 77 | + return this.verificationUri; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the end-user verification URI that includes the user code. |
| 82 | + * @return the end-user verification URI that includes the user code |
| 83 | + */ |
| 84 | + public String getVerificationUriComplete() { |
| 85 | + return this.verificationUriComplete; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns the minimum amount of time (in seconds) that the client should wait between |
| 90 | + * polling requests to the token endpoint. |
| 91 | + * @return the minimum amount of time between polling requests |
| 92 | + */ |
| 93 | + public long getInterval() { |
| 94 | + return this.interval; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns the additional parameters returned in the response. |
| 99 | + * @return a {@code Map} of the additional parameters returned in the response, may be |
| 100 | + * empty. |
| 101 | + */ |
| 102 | + public Map<String, Object> getAdditionalParameters() { |
| 103 | + return this.additionalParameters; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns a new {@link Builder}, initialized with the provided device code and user |
| 108 | + * code values. |
| 109 | + * @param deviceCode the value of the device code |
| 110 | + * @param userCode the value of the user code |
| 111 | + * @return the {@link Builder} |
| 112 | + */ |
| 113 | + public static Builder with(String deviceCode, String userCode) { |
| 114 | + Assert.hasText(deviceCode, "deviceCode cannot be empty"); |
| 115 | + Assert.hasText(userCode, "userCode cannot be empty"); |
| 116 | + return new Builder(deviceCode, userCode); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns a new {@link Builder}, initialized with the provided device code and user |
| 121 | + * code. |
| 122 | + * @param deviceCode the {@link OAuth2DeviceCode} |
| 123 | + * @param userCode the {@link OAuth2UserCode} |
| 124 | + * @return the {@link Builder} |
| 125 | + */ |
| 126 | + public static Builder with(OAuth2DeviceCode deviceCode, OAuth2UserCode userCode) { |
| 127 | + Assert.notNull(deviceCode, "deviceCode cannot be null"); |
| 128 | + Assert.notNull(userCode, "userCode cannot be null"); |
| 129 | + return new Builder(deviceCode, userCode); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns a new {@link Builder}, initialized with the provided response. |
| 134 | + * @param deviceAuthorizationResponse the response to initialize the builder with |
| 135 | + * @return the {@link Builder} |
| 136 | + */ |
| 137 | + public static Builder withResponse(OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse) { |
| 138 | + Assert.notNull(deviceAuthorizationResponse, "deviceAuthorizationResponse cannot be null"); |
| 139 | + return new Builder(deviceAuthorizationResponse); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * A builder for {@link OAuth2DeviceAuthorizationResponse}. |
| 144 | + */ |
| 145 | + public static final class Builder { |
| 146 | + |
| 147 | + private final String deviceCode; |
| 148 | + |
| 149 | + private final String userCode; |
| 150 | + |
| 151 | + private String verificationUri; |
| 152 | + |
| 153 | + private String verificationUriComplete; |
| 154 | + |
| 155 | + private long expiresIn; |
| 156 | + |
| 157 | + private long interval; |
| 158 | + |
| 159 | + private Map<String, Object> additionalParameters; |
| 160 | + |
| 161 | + private Builder(OAuth2DeviceAuthorizationResponse response) { |
| 162 | + OAuth2DeviceCode deviceCode = response.getDeviceCode(); |
| 163 | + OAuth2UserCode userCode = response.getUserCode(); |
| 164 | + this.deviceCode = deviceCode.getTokenValue(); |
| 165 | + this.userCode = userCode.getTokenValue(); |
| 166 | + this.verificationUri = response.getVerificationUri(); |
| 167 | + this.verificationUriComplete = response.getVerificationUriComplete(); |
| 168 | + this.expiresIn = ChronoUnit.SECONDS.between(deviceCode.getIssuedAt(), deviceCode.getExpiresAt()); |
| 169 | + this.interval = response.getInterval(); |
| 170 | + } |
| 171 | + |
| 172 | + private Builder(OAuth2DeviceCode deviceCode, OAuth2UserCode userCode) { |
| 173 | + this.deviceCode = deviceCode.getTokenValue(); |
| 174 | + this.userCode = userCode.getTokenValue(); |
| 175 | + this.expiresIn = ChronoUnit.SECONDS.between(deviceCode.getIssuedAt(), deviceCode.getExpiresAt()); |
| 176 | + } |
| 177 | + |
| 178 | + private Builder(String deviceCode, String userCode) { |
| 179 | + this.deviceCode = deviceCode; |
| 180 | + this.userCode = userCode; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Sets the end-user verification URI. |
| 185 | + * @param verificationUri the end-user verification URI |
| 186 | + * @return the {@link Builder} |
| 187 | + */ |
| 188 | + public Builder verificationUri(String verificationUri) { |
| 189 | + this.verificationUri = verificationUri; |
| 190 | + return this; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Sets the end-user verification URI that includes the user code. |
| 195 | + * @param verificationUriComplete the end-user verification URI that includes the |
| 196 | + * user code |
| 197 | + * @return the {@link Builder} |
| 198 | + */ |
| 199 | + public Builder verificationUriComplete(String verificationUriComplete) { |
| 200 | + this.verificationUriComplete = verificationUriComplete; |
| 201 | + return this; |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Sets the lifetime (in seconds) of the device code and user code. |
| 206 | + * @param expiresIn the lifetime (in seconds) of the device code and user code |
| 207 | + * @return the {@link Builder} |
| 208 | + */ |
| 209 | + public Builder expiresIn(long expiresIn) { |
| 210 | + this.expiresIn = expiresIn; |
| 211 | + return this; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Sets the minimum amount of time (in seconds) that the client should wait |
| 216 | + * between polling requests to the token endpoint. |
| 217 | + * @param interval the minimum amount of time between polling requests |
| 218 | + * @return the {@link Builder} |
| 219 | + */ |
| 220 | + public Builder interval(long interval) { |
| 221 | + this.interval = interval; |
| 222 | + return this; |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Sets the additional parameters returned in the response. |
| 227 | + * @param additionalParameters the additional parameters returned in the response |
| 228 | + * @return the {@link Builder} |
| 229 | + */ |
| 230 | + public Builder additionalParameters(Map<String, Object> additionalParameters) { |
| 231 | + this.additionalParameters = additionalParameters; |
| 232 | + return this; |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Builds a new {@link OAuth2DeviceAuthorizationResponse}. |
| 237 | + * @return a {@link OAuth2DeviceAuthorizationResponse} |
| 238 | + */ |
| 239 | + public OAuth2DeviceAuthorizationResponse build() { |
| 240 | + Assert.hasText(this.verificationUri, "verificationUri cannot be empty"); |
| 241 | + Assert.isTrue(this.expiresIn > 0, "expiresIn must be greater than zero"); |
| 242 | + |
| 243 | + Instant issuedAt = Instant.now(); |
| 244 | + Instant expiresAt = issuedAt.plusSeconds(this.expiresIn); |
| 245 | + OAuth2DeviceCode deviceCode = new OAuth2DeviceCode(this.deviceCode, issuedAt, expiresAt); |
| 246 | + OAuth2UserCode userCode = new OAuth2UserCode(this.userCode, issuedAt, expiresAt); |
| 247 | + |
| 248 | + OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse = new OAuth2DeviceAuthorizationResponse(); |
| 249 | + deviceAuthorizationResponse.deviceCode = deviceCode; |
| 250 | + deviceAuthorizationResponse.userCode = userCode; |
| 251 | + deviceAuthorizationResponse.verificationUri = this.verificationUri; |
| 252 | + deviceAuthorizationResponse.verificationUriComplete = this.verificationUriComplete; |
| 253 | + deviceAuthorizationResponse.interval = this.interval; |
| 254 | + deviceAuthorizationResponse.additionalParameters = Collections |
| 255 | + .unmodifiableMap(CollectionUtils.isEmpty(this.additionalParameters) ? Collections.emptyMap() |
| 256 | + : this.additionalParameters); |
| 257 | + |
| 258 | + return deviceAuthorizationResponse; |
| 259 | + } |
| 260 | + |
| 261 | + } |
| 262 | + |
| 263 | +} |
0 commit comments