use of fish.payara.microprofile.config.extensions.oauth.OAuth2Client in project Payara by payara.
the class AzureSecretsConfigSource method bootstrap.
@Override
public void bootstrap() {
StringBuilder contentBuilder = new StringBuilder();
try {
final File tokenFile = getPrivateKeyFile();
if (tokenFile == null) {
LOGGER.warning("Couldn't find private key file, make sure it's configured.");
} else {
try (Stream<String> stream = Files.lines(tokenFile.toPath())) {
stream.forEach(s -> contentBuilder.append(s));
}
}
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Couldn't find or read the private key file, make sure it exists.", ex);
}
Map<String, String> data = new HashMap<>();
String tenantId = configuration.getTenantId();
String clientId = configuration.getClientId();
if (tenantId == null || clientId == null) {
LOGGER.warning("An error occurred while authenticating Azure to get a token, makes sure Azure Config Source has been configured with correct configuration options.");
} else {
data.put("grant_type", "client_credentials");
data.put("client_id", clientId);
data.put("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
data.put("scope", SCOPE_URL);
try {
final SignedJWT jwt = buildJwt(clientId, String.format(AUTH_URL, tenantId), configuration.getThumbprint());
jwt.sign(new RSASSASigner(parsePrivateKey(contentBuilder.toString())));
data.put("client_assertion", jwt.serialize());
} catch (NoSuchAlgorithmException | InvalidKeySpecException | JOSEException e) {
LOGGER.log(Level.WARNING, "An error occurred while signing the Azure auth token", e);
}
this.authClient = new OAuth2Client(String.format(AUTH_URL, tenantId), data);
}
}
use of fish.payara.microprofile.config.extensions.oauth.OAuth2Client 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);
}
Aggregations