Search in sources :

Example 6 with UserPass

use of org.commonjava.indy.subsys.http.util.UserPass in project indy by Commonjava.

the class BasicAuthenticationOAuthTranslator method authenticate.

@Override
public AuthenticationMechanismOutcome authenticate(final HttpServerExchange exchange, final SecurityContext securityContext) {
    if (!enabled) {
        return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
    }
    logger.debug("BASIC authenticate injector checking for " + AUTHORIZATION_HEADER + " header.");
    final HeaderMap headers = exchange.getRequestHeaders();
    final Collection<String> vals = headers.remove(AUTHORIZATION_HEADER);
    String basicAuth = null;
    String bearerAuth = null;
    final List<String> resultValues = new ArrayList<>();
    if (vals != null) {
        for (final String value : vals) {
            logger.debug("Found Authorization header: '{}'", value);
            if (value.toLowerCase().startsWith(BASIC_AUTH_PREFIX)) {
                logger.debug("detected basic auth");
                basicAuth = value;
            } else if (value.toLowerCase().startsWith(BEARER_AUTH_PREFIX)) {
                bearerAuth = value;
                resultValues.add(value);
            } else {
                resultValues.add(value);
            }
        }
    }
    if (bearerAuth == null && basicAuth != null) {
        final UserPass userPass = UserPass.parse(basicAuth);
        logger.debug("Parsed BASIC authorization: {}", userPass);
        if (userPass != null) {
            final AccessTokenResponse token = lookupToken(userPass);
            if (token != null) {
                final String encodedToken = token.getToken();
                logger.debug("Raw token: {}", encodedToken);
                final String value = BEARER_AUTH_PREFIX + " " + encodedToken;
                logger.debug("Adding {} value: {}", AUTHORIZATION_HEADER, value);
                logger.info("BASIC authentication translated into OAuth 2.0 bearer token. Handing off to Keycloak.");
                resultValues.add(value);
                // KeycloakBearerTokenDebug.debugToken( encodedToken );
                exchange.getResponseHeaders().add(new HttpString(INDY_BEARER_TOKEN), encodedToken);
            }
        }
    }
    logger.debug("Re-adding {} values: {}", AUTHORIZATION_HEADER, resultValues);
    headers.addAll(new HttpString(AUTHORIZATION_HEADER), resultValues);
    // The best we can do is lookup the token for the given basic auth fields, and inject it for keycloak to use.
    return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
}
Also used : HeaderMap(io.undertow.util.HeaderMap) ArrayList(java.util.ArrayList) UserPass(org.commonjava.indy.subsys.http.util.UserPass) HttpString(io.undertow.util.HttpString) AccessTokenResponse(org.keycloak.representations.AccessTokenResponse) HttpString(io.undertow.util.HttpString)

Aggregations

UserPass (org.commonjava.indy.subsys.http.util.UserPass)6 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)3 UrlInfo (org.commonjava.indy.util.UrlInfo)3 IOException (java.io.IOException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 RequestLine (org.apache.http.RequestLine)2 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)2 IndyDataException (org.commonjava.indy.data.IndyDataException)2 HttpConduitWrapper (org.commonjava.indy.httprox.util.HttpConduitWrapper)2 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)2 HeaderMap (io.undertow.util.HeaderMap)1 HttpString (io.undertow.util.HttpString)1 SocketChannel (java.nio.channels.SocketChannel)1 TrackingKey (org.commonjava.indy.folo.model.TrackingKey)1 AbstractProxyRepositoryCreator (org.commonjava.indy.httprox.handler.AbstractProxyRepositoryCreator)1 ProxyCreationResult (org.commonjava.indy.httprox.handler.ProxyCreationResult)1 ProxyMeter (org.commonjava.indy.httprox.util.ProxyMeter)1 ProxyResponseHelper (org.commonjava.indy.httprox.util.ProxyResponseHelper)1 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)1