| 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 |
|
|
| 32 |
|
|
| 33 |
|
@Repository |
| 34 |
|
@Transactional |
| |
|
| 91.1% |
Uncovered Elements: 8 (90) |
Complexity: 18 |
Complexity Density: 0.26 |
|
| 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 |
|
|
| 46 |
|
private int expieredTime = 0; |
| 47 |
|
|
| 48 |
|
@Autowired |
| 49 |
|
private ConfigurationDAO configurationDAO; |
| 50 |
|
@Autowired |
| 51 |
|
private CryptoTools cryptoTools; |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
@throws |
| 57 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 58 |
1 |
public NimbusJwt() throws DAOException {... |
| 59 |
1 |
LOGGER.log("instance class", LogLevel.INFO); |
| 60 |
|
} |
| 61 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
|
| 62 |
3 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
|
| 85 |
4 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 117 |
3 |
@Override... |
| 118 |
|
public String buildHMAC512SignedJwt(final List<String> audience) |
| 119 |
|
throws JsonProcessingException { |
| 120 |
3 |
return this.buildHMAC512SignedJwt(this.issuer, this.subject, audience); |
| 121 |
|
} |
| 122 |
|
|
| |
|
| 71.4% |
Uncovered Elements: 4 (14) |
Complexity: 3 |
Complexity Density: 0.25 |
|
| 123 |
3 |
@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 |
|
|
| |
|
| 71.4% |
Uncovered Elements: 4 (14) |
Complexity: 3 |
Complexity Density: 0.25 |
|
| 148 |
3 |
@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 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (30) |
Complexity: 6 |
Complexity Density: 0.3 |
|
| 174 |
13 |
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 |
|
} |