|
| 1 | +/* |
| 2 | + * Copyright 2002-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 | + |
| 17 | +package org.springframework.security.oauth2.core; |
| 18 | + |
| 19 | +import java.net.URL; |
| 20 | +import java.time.Instant; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.springframework.lang.Nullable; |
| 24 | + |
| 25 | +/** |
| 26 | + * A {@link ClaimAccessor} for the "claims" that may be contained in the |
| 27 | + * Introspection Response. |
| 28 | + * |
| 29 | + * @author David Kovac |
| 30 | + * @since 5.6 |
| 31 | + * @see ClaimAccessor |
| 32 | + * @see OAuth2TokenIntrospectionClaimNames |
| 33 | + * @see <a target="_blank" href= |
| 34 | + * "https://tools.ietf.org/html/rfc7662#section-2.2">Introspection Response</a> |
| 35 | + */ |
| 36 | +public interface OAuth2TokenIntrospectionClaimAccessor extends ClaimAccessor { |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns the indicator {@code (active)} whether or not the token is currently active |
| 40 | + * @return the indicator whether or not the token is currently active |
| 41 | + */ |
| 42 | + default boolean isActive() { |
| 43 | + return Boolean.TRUE.equals(getClaimAsBoolean(OAuth2TokenIntrospectionClaimNames.ACTIVE)); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns a human-readable identifier {@code (username)} for the resource owner that |
| 48 | + * authorized the token |
| 49 | + * @return a human-readable identifier for the resource owner that authorized the |
| 50 | + * token |
| 51 | + */ |
| 52 | + @Nullable |
| 53 | + default String getUsername() { |
| 54 | + return getClaimAsString(OAuth2TokenIntrospectionClaimNames.USERNAME); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns the client identifier {@code (client_id)} for the token |
| 59 | + * @return the client identifier for the token |
| 60 | + */ |
| 61 | + @Nullable |
| 62 | + default String getClientId() { |
| 63 | + return getClaimAsString(OAuth2TokenIntrospectionClaimNames.CLIENT_ID); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns the scopes {@code (scope)} associated with the token |
| 68 | + * @return the scopes associated with the token |
| 69 | + */ |
| 70 | + @Nullable |
| 71 | + default List<String> getScopes() { |
| 72 | + return getClaimAsStringList(OAuth2TokenIntrospectionClaimNames.SCOPE); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the type of the token {@code (token_type)}, for example {@code bearer}. |
| 77 | + * @return the type of the token, for example {@code bearer}. |
| 78 | + */ |
| 79 | + @Nullable |
| 80 | + default String getTokenType() { |
| 81 | + return getClaimAsString(OAuth2TokenIntrospectionClaimNames.TOKEN_TYPE); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns a timestamp {@code (exp)} indicating when the token expires |
| 86 | + * @return a timestamp indicating when the token expires |
| 87 | + */ |
| 88 | + @Nullable |
| 89 | + default Instant getExpiresAt() { |
| 90 | + return getClaimAsInstant(OAuth2TokenIntrospectionClaimNames.EXP); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns a timestamp {@code (iat)} indicating when the token was issued |
| 95 | + * @return a timestamp indicating when the token was issued |
| 96 | + */ |
| 97 | + @Nullable |
| 98 | + default Instant getIssuedAt() { |
| 99 | + return getClaimAsInstant(OAuth2TokenIntrospectionClaimNames.IAT); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns a timestamp {@code (nbf)} indicating when the token is not to be used |
| 104 | + * before |
| 105 | + * @return a timestamp indicating when the token is not to be used before |
| 106 | + */ |
| 107 | + @Nullable |
| 108 | + default Instant getNotBefore() { |
| 109 | + return getClaimAsInstant(OAuth2TokenIntrospectionClaimNames.NBF); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns usually a machine-readable identifier {@code (sub)} of the resource owner |
| 114 | + * who authorized the token |
| 115 | + * @return usually a machine-readable identifier of the resource owner who authorized |
| 116 | + * the token |
| 117 | + */ |
| 118 | + @Nullable |
| 119 | + default String getSubject() { |
| 120 | + return getClaimAsString(OAuth2TokenIntrospectionClaimNames.SUB); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns the intended audience {@code (aud)} for the token |
| 125 | + * @return the intended audience for the token |
| 126 | + */ |
| 127 | + @Nullable |
| 128 | + default List<String> getAudience() { |
| 129 | + return getClaimAsStringList(OAuth2TokenIntrospectionClaimNames.AUD); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns the issuer {@code (iss)} of the token |
| 134 | + * @return the issuer of the token |
| 135 | + */ |
| 136 | + @Nullable |
| 137 | + default URL getIssuer() { |
| 138 | + return getClaimAsURL(OAuth2TokenIntrospectionClaimNames.ISS); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns the identifier {@code (jti)} for the token |
| 143 | + * @return the identifier for the token |
| 144 | + */ |
| 145 | + @Nullable |
| 146 | + default String getId() { |
| 147 | + return getClaimAsString(OAuth2TokenIntrospectionClaimNames.JTI); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments