use of io.micronaut.security.oauth2.endpoint.AuthenticationMethod in project micronaut-security by micronaut-projects.
the class OauthPasswordAuthenticationProvider method getTokenEndpoint.
/**
* Builds the secure endpoint from the client configuration.
*
* @param clientConfiguration The client configuration
* @return The token endpoint
*/
protected SecureEndpoint getTokenEndpoint(OauthClientConfiguration clientConfiguration) {
SecureEndpointConfiguration endpointConfiguration = clientConfiguration.getToken().orElseThrow(() -> new IllegalArgumentException("Token endpoint configuration is missing for provider [" + clientConfiguration.getName() + "]"));
List<AuthenticationMethod> authMethodsSupported = Collections.singletonList(endpointConfiguration.getAuthMethod().orElse(AuthenticationMethod.CLIENT_SECRET_BASIC));
String url = endpointConfiguration.getUrl().orElseThrow(() -> new IllegalArgumentException("Token endpoint URL is null for provider [" + clientConfiguration.getName() + "]"));
return new DefaultSecureEndpoint(url, authMethodsSupported);
}
use of io.micronaut.security.oauth2.endpoint.AuthenticationMethod in project micronaut-security by micronaut-projects.
the class DefaultTokenEndpointClient method secureRequest.
/**
* Secures the request according to the context's endpoint supported authentication
* methods.
*
* @param request Token endpoint Request
* @param requestContext The request context
* @param <G> The token request grant or body
* @param <R> The token response type
*/
protected <G, R extends TokenResponse> void secureRequest(@NonNull MutableHttpRequest<G> request, TokenRequestContext<G, R> requestContext) {
List<AuthenticationMethod> authMethodsSupported = requestContext.getEndpoint().getSupportedAuthenticationMethods().orElseGet(() -> Collections.singletonList(AuthenticationMethod.CLIENT_SECRET_BASIC));
OauthClientConfiguration clientConfiguration = requestContext.getClientConfiguration();
if (LOG.isTraceEnabled()) {
LOG.trace("The token endpoint supports [{}] authentication methods", authMethodsSupported);
}
if (authMethodsSupported.contains(AuthenticationMethod.CLIENT_SECRET_BASIC)) {
if (LOG.isTraceEnabled()) {
LOG.trace("Using client_secret_basic authentication. Adding an Authorization header");
}
request.basicAuth(clientConfiguration.getClientId(), clientConfiguration.getClientSecret());
} else if (authMethodsSupported.contains(AuthenticationMethod.CLIENT_SECRET_POST)) {
if (LOG.isTraceEnabled()) {
LOG.trace("Using client_secret_post authentication. The client_id and client_secret will be present in the body");
}
request.getBody().filter(SecureGrant.class::isInstance).map(SecureGrant.class::cast).ifPresent(body -> {
body.setClientId(clientConfiguration.getClientId());
body.setClientSecret(clientConfiguration.getClientSecret());
});
} else {
if (LOG.isTraceEnabled()) {
LOG.trace("Unsupported or no authentication method. The client_id will be present in the body");
}
request.getBody().filter(SecureGrant.class::isInstance).map(SecureGrant.class::cast).ifPresent(body -> body.setClientId(clientConfiguration.getClientId()));
}
}
Aggregations