|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | +package org.springframework.security.oauth2.server.authorization.authentication; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.springframework.lang.Nullable; |
| 24 | +import org.springframework.security.core.Authentication; |
| 25 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 26 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsent; |
| 27 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 28 | +import org.springframework.util.Assert; |
| 29 | + |
| 30 | +/** |
| 31 | + * An {@link OAuth2AuthenticationContext} that holds an |
| 32 | + * {@link OAuth2DeviceVerificationAuthenticationToken} and additional information and is |
| 33 | + * used when validating the OAuth 2.0 Device Verification Request parameters, as well as |
| 34 | + * determining if authorization consent is required. |
| 35 | + * |
| 36 | + * @author Dinesh Gupta |
| 37 | + * @since 2.0.0 |
| 38 | + * @see OAuth2AuthenticationContext |
| 39 | + * @see OAuth2DeviceVerificationAuthenticationToken |
| 40 | + * @see OAuth2DeviceVerificationAuthenticationProvider#setAuthorizationConsentRequired(java.util.function.Predicate) |
| 41 | + */ |
| 42 | +public final class OAuth2DeviceVerificationAuthenticationContext implements OAuth2AuthenticationContext { |
| 43 | + |
| 44 | + private final Map<Object, Object> context; |
| 45 | + |
| 46 | + private OAuth2DeviceVerificationAuthenticationContext(Map<Object, Object> context) { |
| 47 | + this.context = Collections.unmodifiableMap(new HashMap<>(context)); |
| 48 | + } |
| 49 | + |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + @Nullable |
| 52 | + @Override |
| 53 | + public <T extends Authentication> T getAuthentication() { |
| 54 | + return (T) get(OAuth2DeviceVerificationAuthenticationToken.class); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean hasKey(Object key) { |
| 59 | + Assert.notNull(key, "key cannot be null"); |
| 60 | + return this.context.containsKey(key); |
| 61 | + } |
| 62 | + |
| 63 | + @SuppressWarnings("unchecked") |
| 64 | + @Nullable |
| 65 | + @Override |
| 66 | + public <V> V get(Object key) { |
| 67 | + return hasKey(key) ? (V) this.context.get(key) : null; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the {@link RegisteredClient registered client}. |
| 72 | + * @return the {@link RegisteredClient} |
| 73 | + */ |
| 74 | + public RegisteredClient getRegisteredClient() { |
| 75 | + return get(RegisteredClient.class); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns the {@link OAuth2Authorization authorization}. |
| 80 | + * @return the {@link OAuth2Authorization}, or {@code null} if not available |
| 81 | + */ |
| 82 | + @Nullable |
| 83 | + public OAuth2Authorization getAuthorization() { |
| 84 | + return get(OAuth2Authorization.class); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns the {@link OAuth2AuthorizationConsent authorization consent}. |
| 89 | + * @return the {@link OAuth2AuthorizationConsent}, or {@code null} if not available |
| 90 | + */ |
| 91 | + @Nullable |
| 92 | + public OAuth2AuthorizationConsent getAuthorizationConsent() { |
| 93 | + return get(OAuth2AuthorizationConsent.class); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns the requested scopes. Never {@code null}; always a {@link Set} (possibly |
| 98 | + * empty). |
| 99 | + * @return the requested scopes |
| 100 | + */ |
| 101 | + @SuppressWarnings("unchecked") |
| 102 | + public Set<String> getRequestedScopes() { |
| 103 | + Set<String> scopes = get(Set.class); |
| 104 | + return scopes != null ? scopes : Collections.emptySet(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Constructs a new {@link Builder} with the provided |
| 109 | + * {@link OAuth2DeviceVerificationAuthenticationToken}. |
| 110 | + * @param authentication the {@link OAuth2DeviceVerificationAuthenticationToken} |
| 111 | + * @return the {@link Builder} |
| 112 | + */ |
| 113 | + public static Builder with(OAuth2DeviceVerificationAuthenticationToken authentication) { |
| 114 | + return new Builder(authentication); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * A builder for {@link OAuth2DeviceVerificationAuthenticationContext}. |
| 119 | + */ |
| 120 | + public static final class Builder { |
| 121 | + |
| 122 | + private final Map<Object, Object> context = new HashMap<>(); |
| 123 | + |
| 124 | + private Builder(OAuth2DeviceVerificationAuthenticationToken authentication) { |
| 125 | + Assert.notNull(authentication, "authentication cannot be null"); |
| 126 | + context.put(OAuth2DeviceVerificationAuthenticationToken.class, authentication); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Sets the {@link RegisteredClient registered client}. |
| 131 | + * @param registeredClient the {@link RegisteredClient} |
| 132 | + * @return the {@link Builder} for further configuration |
| 133 | + */ |
| 134 | + public Builder registeredClient(RegisteredClient registeredClient) { |
| 135 | + context.put(RegisteredClient.class, registeredClient); |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Sets the {@link OAuth2Authorization authorization}. |
| 141 | + * @param authorization the {@link OAuth2Authorization} |
| 142 | + * @return the {@link Builder} for further configuration |
| 143 | + */ |
| 144 | + public Builder authorization(@Nullable OAuth2Authorization authorization) { |
| 145 | + if (authorization != null) { |
| 146 | + context.put(OAuth2Authorization.class, authorization); |
| 147 | + } |
| 148 | + return this; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Sets the {@link OAuth2AuthorizationConsent authorization consent}. |
| 153 | + * @param authorizationConsent the {@link OAuth2AuthorizationConsent} |
| 154 | + * @return the {@link Builder} for further configuration |
| 155 | + */ |
| 156 | + public Builder authorizationConsent(@Nullable OAuth2AuthorizationConsent authorizationConsent) { |
| 157 | + if (authorizationConsent != null) { |
| 158 | + context.put(OAuth2AuthorizationConsent.class, authorizationConsent); |
| 159 | + } |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Sets the requested scopes. Never {@code null}; always a {@link Set} (possibly |
| 165 | + * empty). |
| 166 | + * @param requestedScopes the requested scopes |
| 167 | + * @return the {@link Builder} for further configuration |
| 168 | + */ |
| 169 | + public Builder requestedScopes(@Nullable Set<String> requestedScopes) { |
| 170 | + context.put(Set.class, requestedScopes != null ? requestedScopes : Collections.emptySet()); |
| 171 | + return this; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Builds a new {@link OAuth2DeviceVerificationAuthenticationContext}. |
| 176 | + * @return the {@link OAuth2DeviceVerificationAuthenticationContext} |
| 177 | + */ |
| 178 | + public OAuth2DeviceVerificationAuthenticationContext build() { |
| 179 | + Assert.notNull(context.get(RegisteredClient.class), "registeredClient cannot be null"); |
| 180 | + return new OAuth2DeviceVerificationAuthenticationContext(context); |
| 181 | + } |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments