Skip to content

Commit 1ff1d88

Browse files
committed
Manual move of spring-projects/spring-authorization-server src/test
Issue gh-17880
1 parent cf7e258 commit 1ff1d88

File tree

127 files changed

+37354
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+37354
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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.jose;
17+
18+
import java.security.KeyPair;
19+
import java.security.KeyPairGenerator;
20+
import java.security.interfaces.ECPrivateKey;
21+
import java.security.interfaces.ECPublicKey;
22+
import java.security.interfaces.RSAPrivateKey;
23+
import java.security.interfaces.RSAPublicKey;
24+
import java.util.UUID;
25+
26+
import javax.crypto.SecretKey;
27+
28+
import com.nimbusds.jose.jwk.Curve;
29+
import com.nimbusds.jose.jwk.ECKey;
30+
import com.nimbusds.jose.jwk.KeyUse;
31+
import com.nimbusds.jose.jwk.OctetSequenceKey;
32+
import com.nimbusds.jose.jwk.RSAKey;
33+
34+
/**
35+
* @author Joe Grandja
36+
*/
37+
public final class TestJwks {
38+
39+
private static final KeyPairGenerator rsaKeyPairGenerator;
40+
static {
41+
try {
42+
rsaKeyPairGenerator = KeyPairGenerator.getInstance("RSA");
43+
rsaKeyPairGenerator.initialize(2048);
44+
}
45+
catch (Exception ex) {
46+
throw new IllegalStateException(ex);
47+
}
48+
}
49+
50+
// @formatter:off
51+
public static final RSAKey DEFAULT_RSA_JWK =
52+
jwk(
53+
TestKeys.DEFAULT_PUBLIC_KEY,
54+
TestKeys.DEFAULT_PRIVATE_KEY
55+
).build();
56+
// @formatter:on
57+
58+
// @formatter:off
59+
public static final ECKey DEFAULT_EC_JWK =
60+
jwk(
61+
(ECPublicKey) TestKeys.DEFAULT_EC_KEY_PAIR.getPublic(),
62+
(ECPrivateKey) TestKeys.DEFAULT_EC_KEY_PAIR.getPrivate()
63+
).build();
64+
// @formatter:on
65+
66+
// @formatter:off
67+
public static final OctetSequenceKey DEFAULT_SECRET_JWK =
68+
jwk(
69+
TestKeys.DEFAULT_SECRET_KEY
70+
).build();
71+
// @formatter:on
72+
73+
private TestJwks() {
74+
}
75+
76+
public static RSAKey.Builder generateRsaJwk() {
77+
KeyPair keyPair = rsaKeyPairGenerator.generateKeyPair();
78+
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
79+
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
80+
// @formatter:off
81+
return jwk(publicKey, privateKey)
82+
.keyID(UUID.randomUUID().toString());
83+
// @formatter:on
84+
}
85+
86+
public static RSAKey.Builder jwk(RSAPublicKey publicKey, RSAPrivateKey privateKey) {
87+
// @formatter:off
88+
return new RSAKey.Builder(publicKey)
89+
.privateKey(privateKey)
90+
.keyUse(KeyUse.SIGNATURE)
91+
.keyID("rsa-jwk-kid");
92+
// @formatter:on
93+
}
94+
95+
public static ECKey.Builder jwk(ECPublicKey publicKey, ECPrivateKey privateKey) {
96+
// @formatter:off
97+
Curve curve = Curve.forECParameterSpec(publicKey.getParams());
98+
return new ECKey.Builder(curve, publicKey)
99+
.privateKey(privateKey)
100+
.keyUse(KeyUse.SIGNATURE)
101+
.keyID("ec-jwk-kid");
102+
// @formatter:on
103+
}
104+
105+
public static OctetSequenceKey.Builder jwk(SecretKey secretKey) {
106+
// @formatter:off
107+
return new OctetSequenceKey.Builder(secretKey)
108+
.keyUse(KeyUse.SIGNATURE)
109+
.keyID("secret-jwk-kid");
110+
// @formatter:on
111+
}
112+
113+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright 2020-2022 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.jose;
17+
18+
import java.math.BigInteger;
19+
import java.security.KeyFactory;
20+
import java.security.KeyPair;
21+
import java.security.KeyPairGenerator;
22+
import java.security.NoSuchAlgorithmException;
23+
import java.security.interfaces.RSAPrivateKey;
24+
import java.security.interfaces.RSAPublicKey;
25+
import java.security.spec.ECFieldFp;
26+
import java.security.spec.ECParameterSpec;
27+
import java.security.spec.ECPoint;
28+
import java.security.spec.EllipticCurve;
29+
import java.security.spec.InvalidKeySpecException;
30+
import java.security.spec.PKCS8EncodedKeySpec;
31+
import java.security.spec.X509EncodedKeySpec;
32+
import java.util.Base64;
33+
34+
import javax.crypto.SecretKey;
35+
import javax.crypto.spec.SecretKeySpec;
36+
37+
/**
38+
* @author Joe Grandja
39+
*/
40+
public final class TestKeys {
41+
42+
public static final KeyFactory kf;
43+
static {
44+
try {
45+
kf = KeyFactory.getInstance("RSA");
46+
}
47+
catch (NoSuchAlgorithmException ex) {
48+
throw new IllegalStateException(ex);
49+
}
50+
}
51+
public static final String DEFAULT_ENCODED_SECRET_KEY = "bCzY/M48bbkwBEWjmNSIEPfwApcvXOnkCxORBEbPr+4=";
52+
53+
public static final SecretKey DEFAULT_SECRET_KEY = new SecretKeySpec(
54+
Base64.getDecoder().decode(DEFAULT_ENCODED_SECRET_KEY), "AES");
55+
56+
// @formatter:off
57+
public static final String DEFAULT_RSA_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3FlqJr5TRskIQIgdE3Dd"
58+
+ "7D9lboWdcTUT8a+fJR7MAvQm7XXNoYkm3v7MQL1NYtDvL2l8CAnc0WdSTINU6IRv"
59+
+ "c5Kqo2Q4csNX9SHOmEfzoROjQqahEcve1jBXluoCXdYuYpx4/1tfRgG6ii4Uhxh6"
60+
+ "iI8qNMJQX+fLfqhbfYfxBQVRPywBkAbIP4x1EAsbC6FSNmkhCxiMNqEgxaIpY8C2"
61+
+ "kJdJ/ZIV+WW4noDdzpKqHcwmB8FsrumlVY/DNVvUSDIipiq9PbP4H99TXN1o746o"
62+
+ "RaNa07rq1hoCgMSSy+85SagCoxlmyE+D+of9SsMY8Ol9t0rdzpobBuhyJ/o5dfvj"
63+
+ "KwIDAQAB";
64+
// @formatter:on
65+
66+
public static final RSAPublicKey DEFAULT_PUBLIC_KEY;
67+
static {
68+
X509EncodedKeySpec spec = new X509EncodedKeySpec(Base64.getDecoder().decode(DEFAULT_RSA_PUBLIC_KEY));
69+
try {
70+
DEFAULT_PUBLIC_KEY = (RSAPublicKey) kf.generatePublic(spec);
71+
}
72+
catch (InvalidKeySpecException ex) {
73+
throw new IllegalArgumentException(ex);
74+
}
75+
}
76+
77+
// @formatter:off
78+
public static final String DEFAULT_RSA_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDcWWomvlNGyQhA"
79+
+ "iB0TcN3sP2VuhZ1xNRPxr58lHswC9Cbtdc2hiSbe/sxAvU1i0O8vaXwICdzRZ1JM"
80+
+ "g1TohG9zkqqjZDhyw1f1Ic6YR/OhE6NCpqERy97WMFeW6gJd1i5inHj/W19GAbqK"
81+
+ "LhSHGHqIjyo0wlBf58t+qFt9h/EFBVE/LAGQBsg/jHUQCxsLoVI2aSELGIw2oSDF"
82+
+ "oiljwL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R8lA"
97+
+ "Uf/+HF5ZfpMrVXtZD4Igs3I33Duv4sCuqhEvLWTc44pHifVloozNxYfRfU0CgYBN"
98+
+ "HXa7a6cJ1Yp829l62QlJKtx6Ymj95oAnQu5Ez2ROiZMqXRO4nucOjGUP55Orac1a"
99+
+ "FiGm+mC/skFS0MWgW8evaHGDbWU180wheQ35hW6oKAb7myRHtr4q20ouEtQMdQIF"
100+
+ "snV39G1iyqeeAsf7dxWElydXpRi2b68i3BIgzhzebQKBgQCdUQuTsqV9y/JFpu6H"
101+
+ "c5TVvhG/ubfBspI5DhQqIGijnVBzFT//UfIYMSKJo75qqBEyP2EJSmCsunWsAFsM"
102+
+ "TszuiGTkrKcZy9G0wJqPztZZl2F2+bJgnA6nBEV7g5PA4Af+QSmaIhRwqGDAuROR"
103+
+ "47jndeyIaMTNETEmOnms+as17g==";
104+
// @formatter:on
105+
106+
public static final RSAPrivateKey DEFAULT_PRIVATE_KEY;
107+
static {
108+
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(DEFAULT_RSA_PRIVATE_KEY));
109+
try {
110+
DEFAULT_PRIVATE_KEY = (RSAPrivateKey) kf.generatePrivate(spec);
111+
}
112+
catch (InvalidKeySpecException ex) {
113+
throw new IllegalArgumentException(ex);
114+
}
115+
}
116+
117+
public static final KeyPair DEFAULT_RSA_KEY_PAIR = new KeyPair(DEFAULT_PUBLIC_KEY, DEFAULT_PRIVATE_KEY);
118+
119+
public static final KeyPair DEFAULT_EC_KEY_PAIR = generateEcKeyPair();
120+
121+
static KeyPair generateEcKeyPair() {
122+
EllipticCurve ellipticCurve = new EllipticCurve(
123+
new ECFieldFp(new BigInteger(
124+
"115792089210356248762697446949407573530086143415290314195533631308867097853951")),
125+
new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853948"),
126+
new BigInteger("41058363725152142129326129780047268409114441015993725554835256314039467401291"));
127+
ECPoint ecPoint = new ECPoint(
128+
new BigInteger("48439561293906451759052585252797914202762949526041747995844080717082404635286"),
129+
new BigInteger("36134250956749795798585127919587881956611106672985015071877198253568414405109"));
130+
ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, ecPoint,
131+
new BigInteger("115792089210356248762697446949407573529996955224135760342422259061068512044369"), 1);
132+
133+
KeyPair keyPair;
134+
try {
135+
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
136+
keyPairGenerator.initialize(ecParameterSpec);
137+
keyPair = keyPairGenerator.generateKeyPair();
138+
}
139+
catch (Exception ex) {
140+
throw new IllegalStateException(ex);
141+
}
142+
return keyPair;
143+
}
144+
145+
private TestKeys() {
146+
}
147+
148+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2020-2022 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.jwt;
17+
18+
import java.util.Arrays;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
23+
24+
/**
25+
* @author Joe Grandja
26+
*/
27+
public final class TestJwsHeaders {
28+
29+
private TestJwsHeaders() {
30+
}
31+
32+
public static JwsHeader.Builder jwsHeader() {
33+
return jwsHeader(SignatureAlgorithm.RS256);
34+
}
35+
36+
public static JwsHeader.Builder jwsHeader(SignatureAlgorithm signatureAlgorithm) {
37+
// @formatter:off
38+
return JwsHeader.with(signatureAlgorithm)
39+
.jwkSetUrl("https://provider.com/oauth2/jwks")
40+
.jwk(rsaJwk())
41+
.keyId("keyId")
42+
.x509Url("https://provider.com/oauth2/x509")
43+
.x509CertificateChain(Arrays.asList("x509Cert1", "x509Cert2"))
44+
.x509SHA1Thumbprint("x509SHA1Thumbprint")
45+
.x509SHA256Thumbprint("x509SHA256Thumbprint")
46+
.type("JWT")
47+
.contentType("jwt-content-type")
48+
.header("custom-header-name", "custom-header-value");
49+
// @formatter:on
50+
}
51+
52+
private static Map<String, Object> rsaJwk() {
53+
Map<String, Object> rsaJwk = new HashMap<>();
54+
rsaJwk.put("kty", "RSA");
55+
rsaJwk.put("n", "modulus");
56+
rsaJwk.put("e", "exponent");
57+
return rsaJwk;
58+
}
59+
60+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.jwt;
17+
18+
import java.time.Instant;
19+
import java.time.temporal.ChronoUnit;
20+
import java.util.Collections;
21+
22+
/**
23+
* @author Joe Grandja
24+
*/
25+
public final class TestJwtClaimsSets {
26+
27+
private TestJwtClaimsSets() {
28+
}
29+
30+
public static JwtClaimsSet.Builder jwtClaimsSet() {
31+
String issuer = "https://provider.com";
32+
Instant issuedAt = Instant.now();
33+
Instant expiresAt = issuedAt.plus(1, ChronoUnit.HOURS);
34+
35+
// @formatter:off
36+
return JwtClaimsSet.builder()
37+
.issuer(issuer)
38+
.subject("subject")
39+
.audience(Collections.singletonList("client-1"))
40+
.issuedAt(issuedAt)
41+
.notBefore(issuedAt)
42+
.expiresAt(expiresAt)
43+
.id("jti")
44+
.claim("custom-claim-name", "custom-claim-value");
45+
// @formatter:on
46+
}
47+
48+
}

0 commit comments

Comments
 (0)