Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f2c1076

Browse files
Security_part_11_3: add JwtTestUtils.java
1 parent 830b38f commit f2c1076

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package me.oldboy.jwt_test_utils;
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer;
4+
import org.jose4j.jwk.RsaJsonWebKey;
5+
import org.jose4j.jws.AlgorithmIdentifiers;
6+
import org.jose4j.jws.JsonWebSignature;
7+
import org.jose4j.jwt.JwtClaims;
8+
9+
import java.util.Map;
10+
11+
import static me.oldboy.test_constant.TestConstantFields.EXIST_EMAIL;
12+
13+
public class JwtTestUtils {
14+
public static String generateJWT(String mainUser,
15+
RsaJsonWebKey rsaJsonWebKey,
16+
WireMockServer wireMockServer,
17+
Map<String, Object> realmAccessClaims) throws Exception {
18+
JwtClaims claims = new JwtClaims();
19+
claims.setIssuer("http://localhost:" + wireMockServer.port() + "/auth/realms/test-realm");
20+
claims.setSubject(mainUser);
21+
claims.setAudience("test-client");
22+
claims.setClaim("username", mainUser);
23+
claims.setClaim("preferred_username", mainUser);
24+
claims.setClaim("realm_access", realmAccessClaims);
25+
claims.setExpirationTimeMinutesInTheFuture(10);
26+
claims.setIssuedAtToNow();
27+
28+
JsonWebSignature jws = new JsonWebSignature();
29+
jws.setPayload(claims.toJson());
30+
jws.setKey(rsaJsonWebKey.getPrivateKey());
31+
jws.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId());
32+
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
33+
34+
return jws.getCompactSerialization();
35+
}
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /