use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project docker-client by spotify.
the class ContainerRegistryAuthSupplierTest method testAuthForSwarm_TokenWithoutExpirationDoesNotCauseRefresh.
@Test
public void testAuthForSwarm_TokenWithoutExpirationDoesNotCauseRefresh() throws Exception {
final AccessToken accessToken = new AccessToken(tokenValue, null);
final GoogleCredentials credentials = new GoogleCredentials(accessToken);
final ContainerRegistryAuthSupplier supplier = new ContainerRegistryAuthSupplier(credentials, clock, TimeUnit.SECONDS.toMillis(minimumExpirationSecs), refresher);
assertThat(supplier.authForSwarm(), matchesAccessToken(accessToken));
verify(refresher, never()).refresh(credentials);
}
use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project docker-client by spotify.
the class ContainerRegistryAuthSupplierTest method testAuthForBuild_TokenWithoutExpirationDoesNotCauseRefresh.
@Test
public void testAuthForBuild_TokenWithoutExpirationDoesNotCauseRefresh() throws Exception {
final AccessToken accessToken = new AccessToken(tokenValue, null);
final GoogleCredentials credentials = new GoogleCredentials(accessToken);
final ContainerRegistryAuthSupplier supplier = new ContainerRegistryAuthSupplier(credentials, clock, TimeUnit.SECONDS.toMillis(minimumExpirationSecs), refresher);
final RegistryConfigs configs = supplier.authForBuild();
assertThat(configs.configs().values(), is(not(empty())));
assertThat(configs.configs().values(), everyItem(matchesAccessToken(accessToken)));
verify(refresher, never()).refresh(credentials);
}
use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project docker-client by spotify.
the class ContainerRegistryAuthSupplier method authForBuild.
@Override
public RegistryConfigs authForBuild() throws DockerException {
final AccessToken accessToken;
try {
accessToken = getAccessToken();
} catch (IOException e) {
// do not fail as the GCR access token may not be necessary for building the image currently
// being built
log.warn("unable to get access token for Google Container Registry, " + "configuration for building image will not contain RegistryAuth for GCR", e);
return RegistryConfigs.empty();
}
final Map<String, RegistryAuth> configs = new HashMap<>(GCR_REGISTRIES.size());
for (String serverName : GCR_REGISTRIES) {
configs.put(serverName, authForAccessToken(accessToken));
}
return RegistryConfigs.create(configs);
}
use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project apiman by apiman.
the class KeycloakOAuthFactory method directGrant.
private static AuthHandler directGrant(Vertx vertx, VertxEngineConfig apimanConfig, JsonObject authConfig, OAuth2FlowType flowType, String role) {
return new AuthHandler() {
@Override
public void handle(RoutingContext context) {
try {
String[] auth = Basic.decodeWithScheme(context.request().getHeader("Authorization"));
doBasic2Oauth(context, role, auth[0], auth[1]);
} catch (RuntimeException e) {
handle400(context, e.getMessage());
}
}
private void doBasic2Oauth(RoutingContext context, String role, String username, String password) {
JsonObject params = new JsonObject().put("username", username).put("password", password);
HttpClientOptions sslOptions = getHttpClientOptionsForKeycloak(authConfig);
OAuth2Auth oauth2 = KeycloakAuth.create(vertx, flowType, authConfig, sslOptions);
oauth2.authenticate(params, tokenResult -> {
if (tokenResult.succeeded()) {
log.debug("OAuth2 Keycloak exchange succeeded.");
AccessToken token = (AccessToken) tokenResult.result();
token.isAuthorised(role, res -> {
if (res.result()) {
context.next();
} else {
String message = MessageFormat.format("User {0} does not have required role: {1}.", username, role);
log.error(message);
handle403(context, "insufficient_scope", message);
}
});
} else {
String message = tokenResult.cause().getMessage();
log.error("Access Token Error: {0}.", message);
handle401(context, "invalid_token", message);
}
});
}
private void handle400(RoutingContext context, String message) {
if (message != null)
context.response().setStatusMessage(message);
context.fail(400);
}
private void handle401(RoutingContext context, String error, String message) {
String value = MessageFormat.format("Basic realm=\"{0}\" error=\"{1}\" error_message=\"{2}\"", "apiman-gw", error, message);
context.response().putHeader("WWW-Authenticate", value);
context.fail(401);
}
private void handle403(RoutingContext context, String error, String message) {
String value = MessageFormat.format("Basic realm=\"{0}\" error=\"{1}\" error_message=\"{2}\"", "apiman-gw", error, message);
context.response().putHeader("WWW-Authenticate", value);
context.fail(403);
}
@Override
public AuthHandler addAuthority(String authority) {
return this;
}
@Override
public AuthHandler addAuthorities(Set<String> authorities) {
return this;
}
@Override
public void parseCredentials(RoutingContext routingContext, Handler<AsyncResult<JsonObject>> handler) {
}
@Override
public void authorize(User user, Handler<AsyncResult<Void>> handler) {
}
};
}
use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project apiman by apiman.
the class KeycloakOAuth2 method authenticate.
@Override
public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap, Handler<AsyncResult<Void>> resultHandler) {
OAuth2FlowType flowType = getFlowType(config.get("flowType"));
JsonObject params = new JsonObject();
if (config.get("username") != null) {
params.put("username", config.get("username"));
}
if (config.get("password") != null) {
params.put("password", config.get("password"));
}
OAuth2Auth oauth2 = KeycloakAuth.create(vertx, flowType, mapToJson(config));
oauth2.getToken(params, tokenResult -> {
if (tokenResult.succeeded()) {
log.debug("OAuth2 Keycloak exchange succeeded.");
AccessToken token = tokenResult.result();
headerMap.set("Authorization", "Bearer " + token.principal().getString("access_token"));
resultHandler.handle(Future.succeededFuture());
} else {
log.error("Access Token Error: {0}.", tokenResult.cause().getMessage());
resultHandler.handle(Future.failedFuture(tokenResult.cause()));
}
});
return this;
}
Aggregations