1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.application

File NimbusJwt.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
69
7
1
217
177
18
0.26
9.86
7
2.57

Classes

Class Line # Actions
NimbusJwt 35 69 0% 18 8
0.911111191.1%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    package org.europa.together.application;
2   
3    import com.nimbusds.jose.JWSAlgorithm;
4    import com.nimbusds.jose.JWSHeader;
5    import com.nimbusds.jose.JWSObject;
6    import com.nimbusds.jose.JWSSigner;
7    import com.nimbusds.jose.JWSVerifier;
8    import com.nimbusds.jose.Payload;
9    import com.nimbusds.jose.crypto.MACSigner;
10    import com.nimbusds.jose.crypto.MACVerifier;
11    import com.nimbusds.jwt.JWTClaimsSet;
12    import com.nimbusds.jwt.SignedJWT;
13    import java.util.Date;
14    import java.util.List;
15    import org.europa.together.business.ConfigurationDAO;
16    import org.europa.together.business.CryptoTools;
17    import org.europa.together.business.JsonWebToken;
18    import org.europa.together.business.Logger;
19    import org.europa.together.domain.ConfigurationDO;
20    import org.europa.together.domain.HashAlgorithm;
21    import org.europa.together.domain.LogLevel;
22    import org.europa.together.exceptions.DAOException;
23    import org.europa.together.exceptions.JsonProcessingException;
24    import org.europa.together.utils.Constraints;
25    import org.europa.together.utils.StringUtils;
26    import org.springframework.beans.factory.annotation.Autowired;
27    import org.springframework.stereotype.Repository;
28    import org.springframework.transaction.annotation.Transactional;
29   
30    /**
31    * Implementation of JSON Web Token.
32    */
33    @Repository
34    @Transactional
 
35    public class NimbusJwt implements JsonWebToken {
36   
37    private static final long serialVersionUID = 16L;
38    private static final Logger LOGGER = new LogbackLogger(NimbusJwt.class);
39   
40    private static final int INT_30 = 30;
41   
42    private String sharedSecret = "";
43    private String issuer = "";
44    private String subject = "";
45    //1000 * 60 * 10 := 10 minutes = 600000
46    private int expieredTime = 0;
47   
48    @Autowired
49    private ConfigurationDAO configurationDAO;
50    @Autowired
51    private CryptoTools cryptoTools;
52   
53    /**
54    * Constructor.
55    *
56    * @throws DAOException case of error
57    */
 
58  1 toggle public NimbusJwt() throws DAOException {
59  1 LOGGER.log("instance class", LogLevel.INFO);
60    }
61   
 
62  3 toggle @Override
63    public String buildHMAC512SignedJws(final String jsonPayload)
64    throws JsonProcessingException {
65  3 String jws = "";
66  3 try {
67  3 proccessDbConfiguration();
68   
69  3 JWSSigner signer = new MACSigner(sharedSecret);
70  2 JWSHeader jswHeader = new JWSHeader(JWSAlgorithm.HS512);
71   
72  2 Payload payload = new Payload(jsonPayload);
73   
74  2 JWSObject jwsObject = new JWSObject(jswHeader, payload);
75  2 jwsObject.sign(signer);
76   
77  2 jws = jwsObject.serialize();
78    } catch (Exception ex) {
79  1 throw new JsonProcessingException(ex.getClass().getTypeName()
80    + " " + ex.getMessage());
81    }
82  2 return jws;
83    }
84   
 
85  4 toggle @Override
86    public String buildHMAC512SignedJwt(final String issuer, final String subject,
87    final List<String> audience)
88    throws JsonProcessingException {
89  4 String jwt = "";
90  4 try {
91  4 proccessDbConfiguration();
92   
93  4 JWSSigner signer = new MACSigner(sharedSecret);
94  3 JWSHeader jswHeader = new JWSHeader(JWSAlgorithm.HS512);
95   
96  3 Date currentTime = new Date();
97  3 JWTClaimsSet jwtClaims = new JWTClaimsSet.Builder()
98    .issuer(issuer)
99    .subject(subject)
100    .audience(audience)
101    .expirationTime(new Date(new Date().getTime() + expieredTime))
102    .notBeforeTime(currentTime)
103    .issueTime(currentTime)
104    .jwtID(StringUtils.generateUUID())
105    .build();
106   
107  3 SignedJWT signedJWT = new SignedJWT(jswHeader, jwtClaims);
108  3 signedJWT.sign(signer);
109  3 jwt = signedJWT.serialize();
110    } catch (Exception ex) {
111  1 throw new JsonProcessingException(ex.getClass().getTypeName()
112    + " " + ex.getMessage());
113    }
114  3 return jwt;
115    }
116   
 
117  3 toggle @Override
118    public String buildHMAC512SignedJwt(final List<String> audience)
119    throws JsonProcessingException {
120  3 return this.buildHMAC512SignedJwt(this.issuer, this.subject, audience);
121    }
122   
 
123  3 toggle @Override
124    public String parseHMAC512SingedJws(final String jws)
125    throws JsonProcessingException {
126  3 String payload = "";
127  3 try {
128  3 proccessDbConfiguration();
129   
130  3 JWSObject jwsObject = JWSObject.parse(jws);
131  1 JWSVerifier verifier = new MACVerifier(sharedSecret);
132  1 payload = jwsObject.getPayload().toString();
133   
134  1 if (!jwsObject.verify(verifier)) {
135  0 LOGGER.log("JWS is not valid.", LogLevel.ERROR);
136  0 LOGGER.log("\t payload: " + payload, LogLevel.ERROR);
137  0 throw new JsonProcessingException(
138    "JWS is not valid. - for security reasons rejected!");
139    }
140   
141    } catch (Exception ex) {
142  2 throw new JsonProcessingException(ex.getClass().getTypeName()
143    + " " + ex.getMessage());
144    }
145  1 return payload;
146    }
147   
 
148  3 toggle @Override
149    public String parseHMAC512SingedJwt(final String jwt)
150    throws JsonProcessingException {
151  3 String payload = "";
152  3 try {
153  3 proccessDbConfiguration();
154   
155  3 SignedJWT signedJWT = SignedJWT.parse(jwt);
156  1 JWSVerifier verifier = new MACVerifier(sharedSecret);
157  1 payload = signedJWT.getPayload().toString();
158   
159    //TODO: extract for validation
160  1 if (!signedJWT.verify(verifier)) {
161  0 LOGGER.log("JWT is not valid.", LogLevel.ERROR);
162  0 LOGGER.log("\t payload: " + payload, LogLevel.ERROR);
163  0 throw new Exception("JWT is not valid. - for security reasons rejected!");
164    }
165   
166    } catch (Exception ex) {
167  2 throw new JsonProcessingException(ex.getClass().getTypeName()
168    + " " + ex.getMessage());
169    }
170  1 return payload;
171    }
172   
173    //--------------------------------------------------------------------------
 
174  13 toggle private void proccessDbConfiguration() throws DAOException {
175  13 List<ConfigurationDO> configurationEntries = configurationDAO
176    .getAllConfigurationSetEntries(Constraints.MODULE_NAME, CONFIG_VERSION, CONFIG_SET);
177  13 LOGGER.log("Process JWT Configuration [" + configurationEntries.size()
178    + "] (" + configurationEntries.toString() + ")", LogLevel.DEBUG);
179   
180  13 for (ConfigurationDO entry : configurationEntries) {
181  61 String value;
182  61 if (StringUtils.isEmpty(entry.getValue())) {
183  52 value = entry.getDefaultValue();
184    } else {
185  9 value = entry.getValue();
186    }
187   
188  61 if (entry.getKey()
189    .equals(cryptoTools.calculateHash("json.sharedSecret",
190    HashAlgorithm.SHA256))) {
191  13 this.sharedSecret = value;
192  13 LOGGER.log("SharedSecret: " + value, LogLevel.DEBUG);
193  48 } else if (entry.getKey()
194    .equals(cryptoTools.calculateHash("json.issuer",
195    HashAlgorithm.SHA256))) {
196  13 this.issuer = value;
197  13 LOGGER.log("Issuer: " + value, LogLevel.DEBUG);
198  35 } else if (entry.getKey()
199    .equals(cryptoTools.calculateHash("json.subject",
200    HashAlgorithm.SHA256))) {
201  13 this.subject = value;
202  13 LOGGER.log("Subiject: " + value, LogLevel.DEBUG);
203  22 } else if (entry.getKey()
204    .equals(cryptoTools.calculateHash("json.expiration.time",
205    HashAlgorithm.SHA256))) {
206  13 this.expieredTime = Integer.parseInt(value);
207  13 LOGGER.log("Timeout: " + value, LogLevel.DEBUG);
208    }
209    }
210  13 LOGGER.log("Config: \n"
211    + "\t SharedSecret: " + this.sharedSecret + "\n"
212    + "\t Issuer: " + this.issuer + "\n"
213    + "\t Subiject: " + this.subject + "\n"
214    + "\t Timeout: " + this.expieredTime, LogLevel.DEBUG);
215    }
216   
217    }