|
| 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.oidc.authentication; |
| 17 | + |
| 18 | +import java.time.Instant; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 23 | +import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm; |
| 24 | +import org.springframework.security.oauth2.jwt.JoseHeader; |
| 25 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 26 | +import org.springframework.security.oauth2.jwt.JwtClaimsSet; |
| 27 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 28 | +import org.springframework.util.CollectionUtils; |
| 29 | +import org.springframework.util.StringUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * |
| 33 | + * Utility methods used by the {@link OidcClientRegistrationAuthenticationProvider} when issuing {@link Jwt}'s. |
| 34 | + * @author Ovidiu Popa |
| 35 | + * @since 0.2.1 |
| 36 | + */ |
| 37 | +final class JwtUtils { |
| 38 | + |
| 39 | + //TODO Duplicate of {@code org.springframework.security.oauth2.server.authorization.authentication.JwtUtils}. To be refactored |
| 40 | + private JwtUtils() { |
| 41 | + } |
| 42 | + |
| 43 | + static JoseHeader.Builder headers() { |
| 44 | + return JoseHeader.withAlgorithm(SignatureAlgorithm.RS256); |
| 45 | + } |
| 46 | + |
| 47 | + static JwtClaimsSet.Builder accessTokenClaims(RegisteredClient registeredClient, |
| 48 | + String issuer, String subject, Set<String> authorizedScopes) { |
| 49 | + |
| 50 | + Instant issuedAt = Instant.now(); |
| 51 | + Instant expiresAt = issuedAt.plus(registeredClient.getTokenSettings().getAccessTokenTimeToLive()); |
| 52 | + |
| 53 | + // @formatter:off |
| 54 | + JwtClaimsSet.Builder claimsBuilder = JwtClaimsSet.builder(); |
| 55 | + if (StringUtils.hasText(issuer)) { |
| 56 | + claimsBuilder.issuer(issuer); |
| 57 | + } |
| 58 | + claimsBuilder |
| 59 | + .subject(subject) |
| 60 | + .audience(Collections.singletonList(registeredClient.getClientId())) |
| 61 | + .issuedAt(issuedAt) |
| 62 | + .expiresAt(expiresAt) |
| 63 | + .notBefore(issuedAt); |
| 64 | + if (!CollectionUtils.isEmpty(authorizedScopes)) { |
| 65 | + claimsBuilder.claim(OAuth2ParameterNames.SCOPE, authorizedScopes); |
| 66 | + } |
| 67 | + // @formatter:on |
| 68 | + |
| 69 | + return claimsBuilder; |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments