|
| 1 | +/* |
| 2 | + * Copyright 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 | +package org.springframework.security.oauth2.server.authorization; |
| 17 | + |
| 18 | +import org.springframework.lang.Nullable; |
| 19 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 20 | +import org.springframework.util.Assert; |
| 21 | + |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | + |
| 26 | +/** |
| 27 | + * An {@link OAuth2Authorization} implementation with {@link OAuth2AccessToken} access token. |
| 28 | + * |
| 29 | + * @author Krisztian Toth |
| 30 | + */ |
| 31 | +public class AccessTokenOAuth2Authorization extends OAuth2Authorization { |
| 32 | + private final OAuth2AccessToken accessToken; |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates an {@link AccessTokenOAuth2Authorization} object with the provided params and an empty, unmodifiable |
| 36 | + * {@code attributes} map. |
| 37 | + * |
| 38 | + * @see #AccessTokenOAuth2Authorization(String, String, Map, OAuth2AccessToken) |
| 39 | + */ |
| 40 | + public AccessTokenOAuth2Authorization(String registeredClientId, String principalName, |
| 41 | + OAuth2AccessToken accessToken) { |
| 42 | + this(registeredClientId, principalName, Collections.emptyMap(), accessToken); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates an {@link AccessTokenOAuth2Authorization} object with the provided parameters and an |
| 47 | + * {@link OAuth2AccessToken access token}. |
| 48 | + * |
| 49 | + * @see OAuth2Authorization#OAuth2Authorization(String, String, Map) |
| 50 | + */ |
| 51 | + public AccessTokenOAuth2Authorization(String registeredClientId, String principalName, |
| 52 | + @Nullable Map<String, Object> attributes, OAuth2AccessToken accessToken) { |
| 53 | + super(registeredClientId, principalName, attributes); |
| 54 | + Assert.notNull(accessToken, "accessToken cannot be null"); |
| 55 | + this.accessToken = accessToken; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getTokenValue() { |
| 60 | + return accessToken.getTokenValue(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public TokenType getTokenType() { |
| 65 | + return TokenType.ACCESS_TOKEN; |
| 66 | + } |
| 67 | + |
| 68 | + public OAuth2AccessToken getAccessToken() { |
| 69 | + return accessToken; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean equals(Object o) { |
| 74 | + if (this == o) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + if (o == null || getClass() != o.getClass()) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + if (!super.equals(o)) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + AccessTokenOAuth2Authorization that = (AccessTokenOAuth2Authorization) o; |
| 84 | + return Objects.equals(accessToken, that.accessToken); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public int hashCode() { |
| 89 | + return Objects.hash(super.hashCode(), accessToken); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String toString() { |
| 94 | + return "AccessTokenOAuth2Authorization{" + |
| 95 | + "accessToken=" + accessToken + |
| 96 | + "} " + super.toString(); |
| 97 | + } |
| 98 | +} |
0 commit comments