|
| 1 | +/* |
| 2 | + * Copyright 2002-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 | + |
| 17 | +package org.springframework.security.oauth2.jwt; |
| 18 | + |
| 19 | +import java.security.interfaces.ECPrivateKey; |
| 20 | +import java.security.interfaces.ECPublicKey; |
| 21 | +import java.security.interfaces.RSAPrivateKey; |
| 22 | +import java.security.interfaces.RSAPublicKey; |
| 23 | +import java.util.Date; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import javax.crypto.SecretKey; |
| 27 | + |
| 28 | +import com.nimbusds.jose.JOSEException; |
| 29 | +import com.nimbusds.jose.JWSAlgorithm; |
| 30 | +import com.nimbusds.jose.crypto.impl.ECDSA; |
| 31 | +import com.nimbusds.jose.jwk.Curve; |
| 32 | +import com.nimbusds.jose.jwk.ECKey; |
| 33 | +import com.nimbusds.jose.jwk.KeyOperation; |
| 34 | +import com.nimbusds.jose.jwk.KeyUse; |
| 35 | +import com.nimbusds.jose.jwk.OctetSequenceKey; |
| 36 | +import com.nimbusds.jose.jwk.RSAKey; |
| 37 | + |
| 38 | +final class JWKS { |
| 39 | + |
| 40 | + private JWKS() { |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + static OctetSequenceKey.Builder signing(SecretKey key) throws JOSEException { |
| 45 | + Date issued = new Date(); |
| 46 | + return new OctetSequenceKey.Builder(key).keyOperations(Set.of(KeyOperation.SIGN)) |
| 47 | + .keyUse(KeyUse.SIGNATURE) |
| 48 | + .algorithm(JWSAlgorithm.HS256) |
| 49 | + .keyIDFromThumbprint() |
| 50 | + .issueTime(issued) |
| 51 | + .notBeforeTime(issued); |
| 52 | + } |
| 53 | + |
| 54 | + static ECKey.Builder signingWithEc(ECPublicKey pub, ECPrivateKey key) throws JOSEException { |
| 55 | + Date issued = new Date(); |
| 56 | + Curve curve = Curve.forECParameterSpec(pub.getParams()); |
| 57 | + JWSAlgorithm algorithm = computeAlgorithm(curve); |
| 58 | + return new ECKey.Builder(curve, pub).privateKey(key) |
| 59 | + .keyOperations(Set.of(KeyOperation.SIGN)) |
| 60 | + .keyUse(KeyUse.SIGNATURE) |
| 61 | + .algorithm(algorithm) |
| 62 | + .keyIDFromThumbprint() |
| 63 | + .issueTime(issued) |
| 64 | + .notBeforeTime(issued); |
| 65 | + } |
| 66 | + |
| 67 | + private static JWSAlgorithm computeAlgorithm(Curve curve) { |
| 68 | + try { |
| 69 | + return ECDSA.resolveAlgorithm(curve); |
| 70 | + } |
| 71 | + catch (JOSEException ex) { |
| 72 | + throw new IllegalArgumentException(ex); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + static RSAKey.Builder signingWithRsa(RSAPublicKey pub, RSAPrivateKey key) throws JOSEException { |
| 77 | + Date issued = new Date(); |
| 78 | + return new RSAKey.Builder(pub).privateKey(key) |
| 79 | + .keyUse(KeyUse.SIGNATURE) |
| 80 | + .keyOperations(Set.of(KeyOperation.SIGN)) |
| 81 | + .algorithm(JWSAlgorithm.RS256) |
| 82 | + .keyIDFromThumbprint() |
| 83 | + .issueTime(issued) |
| 84 | + .notBeforeTime(issued); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments