|
| 1 | +/* |
| 2 | + * Copyright 2020-2021 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; |
| 17 | + |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +import org.springframework.lang.Nullable; |
| 23 | +import org.springframework.security.core.Authentication; |
| 24 | +import org.springframework.security.oauth2.core.context.Context; |
| 25 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; |
| 26 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 27 | +import org.springframework.util.Assert; |
| 28 | +import org.springframework.util.CollectionUtils; |
| 29 | + |
| 30 | +/** |
| 31 | + * A context that holds an {@link OAuth2AuthorizationConsent.Builder} and (optionally) additional information |
| 32 | + * and is used when customizing the building of {@link OAuth2AuthorizationConsent}. |
| 33 | + * |
| 34 | + * @author Steve Riesenberg |
| 35 | + * @since 0.2.1 |
| 36 | + * @see Context |
| 37 | + */ |
| 38 | +public final class OAuth2AuthorizationConsentContext implements Context { |
| 39 | + private final Map<Object, Object> context; |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructs an {@code OAuth2AuthorizationConsentContext} using the provided parameters. |
| 43 | + * |
| 44 | + * @param context a {@code Map} of additional context information |
| 45 | + */ |
| 46 | + private OAuth2AuthorizationConsentContext(@Nullable Map<Object, Object> context) { |
| 47 | + this.context = new HashMap<>(); |
| 48 | + if (!CollectionUtils.isEmpty(context)) { |
| 49 | + this.context.putAll(context); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns the {@link OAuth2AuthorizationConsent.Builder authorization consent builder}. |
| 55 | + * |
| 56 | + * @return the {@link OAuth2AuthorizationConsent.Builder} |
| 57 | + */ |
| 58 | + public OAuth2AuthorizationConsent.Builder getAuthorizationConsentBuilder() { |
| 59 | + return get(OAuth2AuthorizationConsent.Builder.class); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns the {@link Authentication} representing the {@code Principal} resource owner (or client). |
| 64 | + * |
| 65 | + * @param <T> the type of the {@code Authentication} |
| 66 | + * @return the {@link Authentication} representing the {@code Principal} resource owner (or client) |
| 67 | + */ |
| 68 | + @Nullable |
| 69 | + public <T extends Authentication> T getPrincipal() { |
| 70 | + return get(Builder.PRINCIPAL_AUTHENTICATION_KEY); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns the {@link RegisteredClient registered client}. |
| 75 | + * |
| 76 | + * @return the {@link RegisteredClient}, or {@code null} if not available |
| 77 | + */ |
| 78 | + @Nullable |
| 79 | + public RegisteredClient getRegisteredClient() { |
| 80 | + return get(RegisteredClient.class); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns the {@link OAuth2Authorization authorization}. |
| 85 | + * |
| 86 | + * @return the {@link OAuth2Authorization}, or {@code null} if not available |
| 87 | + */ |
| 88 | + @Nullable |
| 89 | + public OAuth2Authorization getAuthorization() { |
| 90 | + return get(OAuth2Authorization.class); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns the {@link OAuth2AuthorizationRequest authorization request}. |
| 95 | + * |
| 96 | + * @return the {@link OAuth2AuthorizationRequest}, or {@code null} if not available |
| 97 | + */ |
| 98 | + @Nullable |
| 99 | + public OAuth2AuthorizationRequest getAuthorizationRequest() { |
| 100 | + return get(OAuth2AuthorizationRequest.class); |
| 101 | + } |
| 102 | + |
| 103 | + @SuppressWarnings("unchecked") |
| 104 | + @Override |
| 105 | + public <V> V get(Object key) { |
| 106 | + return (V) this.context.get(key); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean hasKey(Object key) { |
| 111 | + return this.context.containsKey(key); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Constructs a new {@link Builder} with the provided {@link OAuth2AuthorizationConsent.Builder}. |
| 116 | + * |
| 117 | + * @param authorizationConsentBuilder the {@link OAuth2AuthorizationConsent.Builder} to initialize the builder |
| 118 | + * @return the {@link Builder} |
| 119 | + */ |
| 120 | + public static OAuth2AuthorizationConsentContext.Builder with(OAuth2AuthorizationConsent.Builder authorizationConsentBuilder) { |
| 121 | + return new Builder(authorizationConsentBuilder); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * A builder for {@link OAuth2AuthorizationConsentContext}. |
| 126 | + */ |
| 127 | + public static final class Builder { |
| 128 | + private static final String PRINCIPAL_AUTHENTICATION_KEY = |
| 129 | + Authentication.class.getName().concat(".PRINCIPAL"); |
| 130 | + private final Map<Object, Object> context = new HashMap<>(); |
| 131 | + |
| 132 | + private Builder(OAuth2AuthorizationConsent.Builder authorizationConsentBuilder) { |
| 133 | + Assert.notNull(authorizationConsentBuilder, "authorizationConsentBuilder cannot be null"); |
| 134 | + put(OAuth2AuthorizationConsent.Builder.class, authorizationConsentBuilder); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Sets the {@link Authentication} representing the {@code Principal} resource owner (or client). |
| 139 | + * |
| 140 | + * @param principal the {@link Authentication} representing the {@code Principal} resource owner (or client) |
| 141 | + * @return the {@link Builder} for further configuration |
| 142 | + */ |
| 143 | + public Builder principal(Authentication principal) { |
| 144 | + return put(PRINCIPAL_AUTHENTICATION_KEY, principal); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets the {@link RegisteredClient registered client}. |
| 149 | + * |
| 150 | + * @param registeredClient the {@link RegisteredClient} |
| 151 | + * @return the {@link Builder} for further configuration |
| 152 | + */ |
| 153 | + public Builder registeredClient(RegisteredClient registeredClient) { |
| 154 | + return put(RegisteredClient.class, registeredClient); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Sets the {@link OAuth2Authorization authorization}. |
| 159 | + * |
| 160 | + * @param authorization the {@link OAuth2Authorization} |
| 161 | + * @return the {@link Builder} for further configuration |
| 162 | + */ |
| 163 | + public Builder authorization(OAuth2Authorization authorization) { |
| 164 | + return put(OAuth2Authorization.class, authorization); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Sets the {@link OAuth2AuthorizationRequest authorization request}. |
| 169 | + * |
| 170 | + * @param authorizationRequest the {@link OAuth2AuthorizationRequest} |
| 171 | + * @return the {@link Builder} for further configuration |
| 172 | + */ |
| 173 | + public Builder authorizationRequest(OAuth2AuthorizationRequest authorizationRequest) { |
| 174 | + return put(OAuth2AuthorizationRequest.class, authorizationRequest); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Associates an attribute. |
| 179 | + * |
| 180 | + * @param key the key for the attribute |
| 181 | + * @param value the value of the attribute |
| 182 | + * @return the {@link OAuth2TokenContext.AbstractBuilder} for further configuration |
| 183 | + */ |
| 184 | + public Builder put(Object key, Object value) { |
| 185 | + Assert.notNull(key, "key cannot be null"); |
| 186 | + Assert.notNull(value, "value cannot be null"); |
| 187 | + this.context.put(key, value); |
| 188 | + return this; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * A {@code Consumer} of the attributes {@code Map} |
| 193 | + * allowing the ability to add, replace, or remove. |
| 194 | + * |
| 195 | + * @param contextConsumer a {@link Consumer} of the attributes {@code Map} |
| 196 | + * @return the {@link Builder} for further configuration |
| 197 | + */ |
| 198 | + public Builder context(Consumer<Map<Object, Object>> contextConsumer) { |
| 199 | + contextConsumer.accept(this.context); |
| 200 | + return this; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Builds a new {@link OAuth2AuthorizationConsentContext}. |
| 205 | + * |
| 206 | + * @return the {@link OAuth2AuthorizationConsentContext} |
| 207 | + */ |
| 208 | + public OAuth2AuthorizationConsentContext build() { |
| 209 | + return new OAuth2AuthorizationConsentContext(this.context); |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments