|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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.client; |
| 17 | + |
| 18 | +import org.springframework.lang.Nullable; |
| 19 | +import org.springframework.security.core.Authentication; |
| 20 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 21 | +import org.springframework.util.Assert; |
| 22 | +import org.springframework.util.CollectionUtils; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.LinkedHashMap; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.function.Consumer; |
| 29 | + |
| 30 | +/** |
| 31 | + * Represents a request the {@link OAuth2AuthorizedClientManager} uses to |
| 32 | + * {@link OAuth2AuthorizedClientManager#authorize(OAuth2AuthorizeRequest) authorize} (or re-authorize) |
| 33 | + * the {@link ClientRegistration client} identified by the provided {@link #getClientRegistrationId() clientRegistrationId}. |
| 34 | + * |
| 35 | + * @author Joe Grandja |
| 36 | + * @since 5.2 |
| 37 | + * @see OAuth2AuthorizedClientManager |
| 38 | + */ |
| 39 | +public final class OAuth2AuthorizeRequest { |
| 40 | + private String clientRegistrationId; |
| 41 | + private OAuth2AuthorizedClient authorizedClient; |
| 42 | + private Authentication principal; |
| 43 | + private Map<String, Object> attributes; |
| 44 | + |
| 45 | + private OAuth2AuthorizeRequest() { |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns the identifier for the {@link ClientRegistration client registration}. |
| 50 | + * |
| 51 | + * @return the identifier for the client registration |
| 52 | + */ |
| 53 | + public String getClientRegistrationId() { |
| 54 | + return this.clientRegistrationId; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns the {@link OAuth2AuthorizedClient authorized client} or {@code null} if it was not provided. |
| 59 | + * |
| 60 | + * @return the {@link OAuth2AuthorizedClient} or {@code null} if it was not provided |
| 61 | + */ |
| 62 | + @Nullable |
| 63 | + public OAuth2AuthorizedClient getAuthorizedClient() { |
| 64 | + return this.authorizedClient; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the {@code Principal} (to be) associated to the authorized client. |
| 69 | + * |
| 70 | + * @return the {@code Principal} (to be) associated to the authorized client |
| 71 | + */ |
| 72 | + public Authentication getPrincipal() { |
| 73 | + return this.principal; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns the attributes associated to the request. |
| 78 | + * |
| 79 | + * @return a {@code Map} of the attributes associated to the request |
| 80 | + */ |
| 81 | + public Map<String, Object> getAttributes() { |
| 82 | + return this.attributes; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the value of an attribute associated to the request or {@code null} if not available. |
| 87 | + * |
| 88 | + * @param name the name of the attribute |
| 89 | + * @param <T> the type of the attribute |
| 90 | + * @return the value of the attribute associated to the request |
| 91 | + */ |
| 92 | + @Nullable |
| 93 | + @SuppressWarnings("unchecked") |
| 94 | + public <T> T getAttribute(String name) { |
| 95 | + return (T) this.getAttributes().get(name); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns a new {@link Builder} initialized with the identifier for the {@link ClientRegistration client registration}. |
| 100 | + * |
| 101 | + * @param clientRegistrationId the identifier for the {@link ClientRegistration client registration} |
| 102 | + * @return the {@link Builder} |
| 103 | + */ |
| 104 | + public static Builder withClientRegistrationId(String clientRegistrationId) { |
| 105 | + return new Builder(clientRegistrationId); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns a new {@link Builder} initialized with the {@link OAuth2AuthorizedClient authorized client}. |
| 110 | + * |
| 111 | + * @param authorizedClient the {@link OAuth2AuthorizedClient authorized client} |
| 112 | + * @return the {@link Builder} |
| 113 | + */ |
| 114 | + public static Builder withAuthorizedClient(OAuth2AuthorizedClient authorizedClient) { |
| 115 | + return new Builder(authorizedClient); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * A builder for {@link OAuth2AuthorizeRequest}. |
| 120 | + */ |
| 121 | + public static class Builder { |
| 122 | + private String clientRegistrationId; |
| 123 | + private OAuth2AuthorizedClient authorizedClient; |
| 124 | + private Authentication principal; |
| 125 | + private Map<String, Object> attributes; |
| 126 | + |
| 127 | + private Builder(String clientRegistrationId) { |
| 128 | + Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty"); |
| 129 | + this.clientRegistrationId = clientRegistrationId; |
| 130 | + } |
| 131 | + |
| 132 | + private Builder(OAuth2AuthorizedClient authorizedClient) { |
| 133 | + Assert.notNull(authorizedClient, "authorizedClient cannot be null"); |
| 134 | + this.authorizedClient = authorizedClient; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Sets the {@code Principal} (to be) associated to the authorized client. |
| 139 | + * |
| 140 | + * @param principal the {@code Principal} (to be) associated to the authorized client |
| 141 | + * @return the {@link Builder} |
| 142 | + */ |
| 143 | + public Builder principal(Authentication principal) { |
| 144 | + this.principal = principal; |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Provides a {@link Consumer} access to the attributes associated to the request. |
| 150 | + * |
| 151 | + * @param attributesConsumer a {@link Consumer} of the attributes associated to the request |
| 152 | + * @return the {@link Builder} |
| 153 | + */ |
| 154 | + public Builder attributes(Consumer<Map<String, Object>> attributesConsumer) { |
| 155 | + if (this.attributes == null) { |
| 156 | + this.attributes = new HashMap<>(); |
| 157 | + } |
| 158 | + attributesConsumer.accept(this.attributes); |
| 159 | + return this; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Sets an attribute associated to the request. |
| 164 | + * |
| 165 | + * @param name the name of the attribute |
| 166 | + * @param value the value of the attribute |
| 167 | + * @return the {@link Builder} |
| 168 | + */ |
| 169 | + public Builder attribute(String name, Object value) { |
| 170 | + if (this.attributes == null) { |
| 171 | + this.attributes = new HashMap<>(); |
| 172 | + } |
| 173 | + this.attributes.put(name, value); |
| 174 | + return this; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Builds a new {@link OAuth2AuthorizeRequest}. |
| 179 | + * |
| 180 | + * @return a {@link OAuth2AuthorizeRequest} |
| 181 | + */ |
| 182 | + public OAuth2AuthorizeRequest build() { |
| 183 | + Assert.notNull(this.principal, "principal cannot be null"); |
| 184 | + OAuth2AuthorizeRequest authorizeRequest = new OAuth2AuthorizeRequest(); |
| 185 | + if (this.authorizedClient != null) { |
| 186 | + authorizeRequest.clientRegistrationId = this.authorizedClient.getClientRegistration().getRegistrationId(); |
| 187 | + authorizeRequest.authorizedClient = this.authorizedClient; |
| 188 | + } else { |
| 189 | + authorizeRequest.clientRegistrationId = this.clientRegistrationId; |
| 190 | + } |
| 191 | + authorizeRequest.principal = this.principal; |
| 192 | + authorizeRequest.attributes = Collections.unmodifiableMap( |
| 193 | + CollectionUtils.isEmpty(this.attributes) ? |
| 194 | + Collections.emptyMap() : new LinkedHashMap<>(this.attributes)); |
| 195 | + return authorizeRequest; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments