use of com.nimbusds.jose.crypto.RSASSASigner in project Payara by payara.
the class GCPSecretsConfigSource method bootstrap.
@Override
public void bootstrap() {
String clientEmail = null;
String privateKey = null;
try {
final File tokenFile = getTokenFile();
if (tokenFile == null) {
LOGGER.warning("Couldn't find token file, make sure it's configured.");
} else {
try (JsonParser parser = Json.createParser(new FileInputStream(getTokenFile()))) {
while (parser.hasNext()) {
JsonParser.Event parseEvent = parser.next();
if (parseEvent == Event.KEY_NAME) {
final String keyName = parser.getString();
parser.next();
switch(keyName) {
case "client_email":
clientEmail = parser.getString();
break;
case "private_key":
privateKey = parser.getString();
break;
}
if (clientEmail != null && privateKey != null) {
break;
}
}
}
if (clientEmail == null || privateKey == null) {
throw new PropertyVetoException("Error reading JSON key file", new PropertyChangeEvent(configuration, "jsonKeyFile", null, null));
}
}
}
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Couldn't find or read the GCP key file, make sure it exists.", ex);
}
Map<String, String> data = new HashMap<>();
data.put("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
if (clientEmail != null && privateKey != null) {
try {
final SignedJWT jwt = buildJwt(// issuer
clientEmail, // scope
"https://www.googleapis.com/auth/cloud-platform");
jwt.sign(new RSASSASigner(parsePrivateKey(privateKey)));
data.put("assertion", jwt.serialize());
} catch (NoSuchAlgorithmException | InvalidKeySpecException | JOSEException e) {
LOGGER.log(Level.WARNING, "An error occurred while signing the GCP auth token", e);
}
}
this.authClient = new OAuth2Client(AUTH_URL, data);
}
use of com.nimbusds.jose.crypto.RSASSASigner in project quickstart by wildfly.
the class JwtManager method createJwt.
public String createJwt(final String subject, final String[] roles) throws Exception {
JWSSigner signer = new RSASSASigner(privateKey);
JsonArrayBuilder rolesBuilder = Json.createArrayBuilder();
for (String role : roles) {
rolesBuilder.add(role);
}
JsonObjectBuilder claimsBuilder = Json.createObjectBuilder().add("sub", subject).add("iss", ISSUER).add("aud", AUDIENCE).add(CLAIM_ROLES, rolesBuilder.build()).add("exp", ((System.currentTimeMillis() / 1000) + TOKEN_VALIDITY));
JWSObject jwsObject = new JWSObject(new JWSHeader.Builder(JWSAlgorithm.RS256).type(new JOSEObjectType("jwt")).build(), new Payload(claimsBuilder.build().toString()));
jwsObject.sign(signer);
return jwsObject.serialize();
}
Aggregations