|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 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.authentication; |
| 17 | + |
| 18 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 19 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 20 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 21 | +import org.springframework.security.oauth2.jwt.DPoPProofContext; |
| 22 | +import org.springframework.security.oauth2.jwt.DPoPProofJwtDecoderFactory; |
| 23 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 24 | +import org.springframework.security.oauth2.jwt.JwtDecoder; |
| 25 | +import org.springframework.security.oauth2.jwt.JwtDecoderFactory; |
| 26 | +import org.springframework.util.StringUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Joe Grandja |
| 30 | + * @since 1.5 |
| 31 | + */ |
| 32 | +final class DPoPProofVerifier { |
| 33 | + |
| 34 | + private static final JwtDecoderFactory<DPoPProofContext> dPoPProofVerifierFactory = new DPoPProofJwtDecoderFactory(); |
| 35 | + |
| 36 | + private DPoPProofVerifier() { |
| 37 | + } |
| 38 | + |
| 39 | + static Jwt verifyIfAvailable(OAuth2AuthorizationGrantAuthenticationToken authorizationGrantAuthentication) { |
| 40 | + String dPoPProof = (String) authorizationGrantAuthentication.getAdditionalParameters().get("dpop_proof"); |
| 41 | + if (!StringUtils.hasText(dPoPProof)) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + String method = (String) authorizationGrantAuthentication.getAdditionalParameters().get("dpop_method"); |
| 46 | + String targetUri = (String) authorizationGrantAuthentication.getAdditionalParameters().get("dpop_target_uri"); |
| 47 | + |
| 48 | + Jwt dPoPProofJwt; |
| 49 | + try { |
| 50 | + // @formatter:off |
| 51 | + DPoPProofContext dPoPProofContext = DPoPProofContext.withDPoPProof(dPoPProof) |
| 52 | + .method(method) |
| 53 | + .targetUri(targetUri) |
| 54 | + .build(); |
| 55 | + // @formatter:on |
| 56 | + JwtDecoder dPoPProofVerifier = dPoPProofVerifierFactory.createDecoder(dPoPProofContext); |
| 57 | + dPoPProofJwt = dPoPProofVerifier.decode(dPoPProof); |
| 58 | + } |
| 59 | + catch (Exception ex) { |
| 60 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_DPOP_PROOF), ex); |
| 61 | + } |
| 62 | + |
| 63 | + return dPoPProofJwt; |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments