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