22
33import com .google .gson .Gson ;
44import com .google .gson .JsonObject ;
5- import java .io .StringReader ;
65import io .jsonwebtoken .Jwts ;
6+ import java .io .StringReader ;
77import java .nio .charset .Charset ;
88import java .security .KeyFactory ;
99import java .security .Security ;
1212import java .security .spec .PKCS8EncodedKeySpec ;
1313import java .security .spec .X509EncodedKeySpec ;
1414import java .util .Base64 ;
15- import java .util .HashMap ;
1615import java .util .Date ;
16+ import java .util .HashMap ;
1717import java .util .Map ;
1818import no .elixir .e2eTests .constants .Strings ;
1919import no .elixir .e2eTests .core .E2EState ;
2424
2525public class TokenUtils {
2626
27- public static String generateVisaToken (String resource , String pubKeyPath , String privKeyPath ) throws Exception {
28- RSAPrivateKey privateKey = getPrivateKey (privKeyPath );
29- String user , aud ;
30- // if the passport scoped access token is present and if the
31- // integration is set to EGA_DEV environment, we get some
32- // details from that provided token.
33- if (E2EState .env .getLSAAIToken () != null
34- && !E2EState .env .getLSAAIToken ().isEmpty ()
35- && E2EState .env .getIntegration ().equals ("EGA_DEV" )) {
36- HashMap <String , String > details = extractDetailsFromLSAAIToken (E2EState .env .getLSAAIToken ());
37- user = details .get ("sub" );
38- aud = details .get ("aud" );
39- } else {
40- user = Strings .JWT_SUBJECT ;
41- aud = E2EState .env .getProxyTokenAudience ();
42- }
43- // Build the GA4GH visa claim
44- Map <String , Object > ga4ghVisa = new HashMap <>();
45- ga4ghVisa .put ("asserted" , Strings .VISA_ASSERTED );
46- ga4ghVisa .put ("by" , Strings .VISA_BY );
47- ga4ghVisa .put ("source" , Strings .VISA_SOURCE );
48- ga4ghVisa .put ("type" , Strings .VISA_TYPE );
49- ga4ghVisa .put ("value" , String .format (Strings .VISA_VALUE_TEMPLATE , resource ));
50- // Build and sign the JWT
51- return Jwts .builder ()
52- .header ()
53- .add ("jku" , Strings .JWT_JKU )
54- .add ("kid" , Strings .JWT_KID )
55- .add ("typ" , Strings .JWT_TYP )
56- .add ("alg" , Strings .JWT_ALG )
57- .and ()
58- .subject (user )
59- .audience ()
60- .add (aud )
61- .and ()
62- .claim ("ga4gh_visa_v1" , ga4ghVisa )
63- .issuer (Strings .JWT_ISSUER )
64- .expiration (new Date (Strings .JWT_EXPIRATION * 1000 ))
65- .issuedAt (new Date (Strings .JWT_ISSUED_AT * 1000 ))
66- .id (Strings .JWT_ID )
67- .signWith (privateKey , Jwts .SIG .RS256 )
68- .compact ();
27+ public static String generateVisaToken (String resource , String pubKeyPath , String privKeyPath )
28+ throws Exception {
29+ RSAPrivateKey privateKey = getPrivateKey (privKeyPath );
30+ String user , aud ;
31+ // if the passport scoped access token is present and if the
32+ // integration is set to EGA_DEV environment, we get some
33+ // details from that provided token.
34+ if (E2EState .env .getLSAAIToken () != null
35+ && !E2EState .env .getLSAAIToken ().isEmpty ()
36+ && E2EState .env .getIntegration ().equals ("EGA_DEV" )) {
37+ HashMap <String , String > details = extractDetailsFromLSAAIToken (E2EState .env .getLSAAIToken ());
38+ user = details .get ("sub" );
39+ aud = details .get ("aud" );
40+ } else {
41+ user = Strings .JWT_SUBJECT ;
42+ aud = E2EState .env .getProxyTokenAudience ();
6943 }
44+ // Build the GA4GH visa claim
45+ Map <String , Object > ga4ghVisa = new HashMap <>();
46+ ga4ghVisa .put ("asserted" , Strings .VISA_ASSERTED );
47+ ga4ghVisa .put ("by" , Strings .VISA_BY );
48+ ga4ghVisa .put ("source" , Strings .VISA_SOURCE );
49+ ga4ghVisa .put ("type" , Strings .VISA_TYPE );
50+ ga4ghVisa .put ("value" , String .format (Strings .VISA_VALUE_TEMPLATE , resource ));
51+ // Build and sign the JWT
52+ return Jwts .builder ()
53+ .header ()
54+ .add ("jku" , Strings .JWT_JKU )
55+ .add ("kid" , Strings .JWT_KID )
56+ .add ("typ" , Strings .JWT_TYP )
57+ .add ("alg" , Strings .JWT_ALG )
58+ .and ()
59+ .subject (user )
60+ .audience ()
61+ .add (aud )
62+ .and ()
63+ .claim ("ga4gh_visa_v1" , ga4ghVisa )
64+ .issuer (Strings .JWT_ISSUER )
65+ .expiration (new Date (Strings .JWT_EXPIRATION * 1000 ))
66+ .issuedAt (new Date (Strings .JWT_ISSUED_AT * 1000 ))
67+ .id (Strings .JWT_ID )
68+ .signWith (privateKey , Jwts .SIG .RS256 )
69+ .compact ();
70+ }
7071
7172 public static RSAPublicKey getPublicKey (String pubKeyPath ) throws Exception {
72- String keyContent =
73- FileUtils .readFileToString (E2EState .env .getIntegration ().equals ("EGA_DEV" )
74- ? CertificateUtils .getFile (pubKeyPath )
75- : CertificateUtils .getCertificateFile (pubKeyPath ), Charset .defaultCharset ());
73+ String keyContent =
74+ FileUtils .readFileToString (
75+ E2EState .env .getIntegration ().equals ("EGA_DEV" )
76+ ? CertificateUtils .getFile (pubKeyPath )
77+ : CertificateUtils .getCertificateFile (pubKeyPath ),
78+ Charset .defaultCharset ());
7679
7780 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
7881
@@ -91,9 +94,11 @@ public static RSAPublicKey getPublicKey(String pubKeyPath) throws Exception {
9194
9295 public static RSAPrivateKey getPrivateKey (String privKeyPath ) throws Exception {
9396 String keyContent =
94- FileUtils .readFileToString (E2EState .env .getIntegration ().equals ("EGA_DEV" )
97+ FileUtils .readFileToString (
98+ E2EState .env .getIntegration ().equals ("EGA_DEV" )
9599 ? CertificateUtils .getFile (privKeyPath )
96- : CertificateUtils .getCertificateFile (privKeyPath ), Charset .defaultCharset ());
100+ : CertificateUtils .getCertificateFile (privKeyPath ),
101+ Charset .defaultCharset ());
97102
98103 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
99104
@@ -114,8 +119,8 @@ public static RSAPrivateKey getPrivateKey(String privKeyPath) throws Exception {
114119 }
115120
116121 /**
117- * Introduced with EGA_DEV runtime tests. Extracts specific details
118- * from a given passport scoped access token.
122+ * Introduced with EGA_DEV runtime tests. Extracts specific details from a given passport scoped
123+ * access token.
119124 *
120125 * @param passportScopedAccessToken token to decode
121126 * @return Map containing the `sub` and `aud`
@@ -152,15 +157,14 @@ private static RSAPrivateKey handlePKCS1PrivateKey(String keyContent) throws Exc
152157 return (RSAPrivateKey ) converter .getPrivateKey ((PrivateKeyInfo ) object );
153158 } else if (object instanceof org .bouncycastle .asn1 .pkcs .RSAPrivateKey rsaPrivKey ) {
154159 org .bouncycastle .asn1 .x509 .AlgorithmIdentifier algId =
155- new org .bouncycastle .asn1 .x509 .AlgorithmIdentifier (
156- org .bouncycastle .asn1 .pkcs .PKCSObjectIdentifiers .rsaEncryption );
160+ new org .bouncycastle .asn1 .x509 .AlgorithmIdentifier (
161+ org .bouncycastle .asn1 .pkcs .PKCSObjectIdentifiers .rsaEncryption );
157162 PrivateKeyInfo privKeyInfo = new PrivateKeyInfo (algId , rsaPrivKey );
158163 return (RSAPrivateKey ) converter .getPrivateKey (privKeyInfo );
159164 }
160165
161166 throw new IllegalArgumentException (
162- "Unable to parse private key. Object type: "
163- + (object != null ? object .getClass ().getName () : "null" ));
167+ "Unable to parse private key. Object type: "
168+ + (object != null ? object .getClass ().getName () : "null" ));
164169 }
165-
166170}
0 commit comments