github-advanced-security[bot] commented on code in PR #14: URL: https://github.com/apache/sling-org-apache-sling-auth-oauth-client/pull/14#discussion_r2020810902
########## src/main/java/org/apache/sling/auth/oauth_client/impl/OidcAuthenticationHandler.java: ########## @@ -0,0 +1,454 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.auth.oauth_client.impl; + +import com.nimbusds.oauth2.sdk.AuthorizationCode; +import com.nimbusds.oauth2.sdk.AuthorizationCodeGrant; +import com.nimbusds.oauth2.sdk.AuthorizationErrorResponse; +import com.nimbusds.oauth2.sdk.AuthorizationRequest; +import com.nimbusds.oauth2.sdk.AuthorizationResponse; +import com.nimbusds.oauth2.sdk.ErrorObject; +import com.nimbusds.oauth2.sdk.ErrorResponse; +import com.nimbusds.oauth2.sdk.ParseException; +import com.nimbusds.oauth2.sdk.ResponseType; +import com.nimbusds.oauth2.sdk.Scope; +import com.nimbusds.oauth2.sdk.TokenRequest; +import com.nimbusds.oauth2.sdk.TokenResponse; +import com.nimbusds.oauth2.sdk.auth.ClientSecretBasic; +import com.nimbusds.oauth2.sdk.auth.Secret; +import com.nimbusds.oauth2.sdk.http.HTTPRequest; +import com.nimbusds.oauth2.sdk.http.HTTPResponse; +import com.nimbusds.oauth2.sdk.id.ClientID; +import com.nimbusds.oauth2.sdk.id.Identifier; +import com.nimbusds.oauth2.sdk.id.State; +import com.nimbusds.openid.connect.sdk.OIDCTokenResponseParser; +import com.nimbusds.openid.connect.sdk.UserInfoRequest; +import com.nimbusds.openid.connect.sdk.UserInfoResponse; +import com.nimbusds.openid.connect.sdk.claims.UserInfo; +import org.apache.jackrabbit.oak.spi.security.authentication.credentials.CredentialsSupport; +import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityProvider; +import org.apache.sling.auth.core.spi.AuthenticationHandler; +import org.apache.sling.auth.core.spi.AuthenticationInfo; +import org.apache.sling.auth.core.spi.DefaultAuthenticationFeedbackHandler; +import org.apache.sling.auth.oauth_client.ClientConnection; +import org.apache.sling.auth.oauth_client.spi.LoginCookieManager; +import org.apache.sling.auth.oauth_client.spi.OAuthState; +import org.apache.sling.auth.oauth_client.spi.OAuthStateManager; +import org.apache.sling.auth.oauth_client.spi.OAuthTokenStore; +import org.apache.sling.auth.oauth_client.spi.OidcAuthCredentials; +import org.apache.sling.auth.oauth_client.spi.UserInfoProcessor; +import org.apache.sling.jcr.api.SlingRepository; +import org.apache.sling.jcr.resource.api.JcrResourceConstants; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.osgi.framework.BundleContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; +import org.osgi.service.component.annotations.ReferencePolicyOption; +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.Designate; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; + +@Component( + service = AuthenticationHandler.class, + immediate = true +) + +@Designate(ocd = OidcAuthenticationHandler.Config.class, factory = true) +public class OidcAuthenticationHandler extends DefaultAuthenticationFeedbackHandler implements AuthenticationHandler { + + + private static final Logger logger = LoggerFactory.getLogger(OidcAuthenticationHandler.class); + private static final String AUTH_TYPE = "oidc"; + public static final String REDIRECT_ATTRIBUTE_NAME = "sling.redirect"; + + private final SlingRepository repository; + + private final Map<String, ClientConnection> connections; + private final OAuthStateManager stateManager; + + String idp; + + private final OAuthTokenStore tokenStore; + + private final String callbackUri; + + private LoginCookieManager loginCookieManager; + + private String defaultRedirect; + + private String defaultConnectionName; + + private UserInfoProcessor userInfoProcessor; + private static final long serialVersionUID = 1L; + + // We don't want leave the cookie lying around for a long time because it it not needed. + // At the same time, some OAuth user authentication flows take a long time due to + // consent, account selection, 2FA, etc so we cannot make this too short. + protected static final int COOKIE_MAX_AGE_SECONDS = 300; + + @ObjectClassDefinition( + name = "Apache Sling Oidc Authentication Handler", + description = "Apache Sling Oidc Authentication Handler Service" + ) + + @interface Config { + @AttributeDefinition(name = "Path", + description = "Repository path for which this authentication handler should be used by Sling. If this is " + + "empty, the authentication handler will be disabled. By default this is set to \"/\".") + String path() default "/"; + + @AttributeDefinition(name = "Sync Handler Configuration Name", + description = "Name of Sync Handler Configuration") + String idp() default "oidc"; + + @AttributeDefinition(name = "Callback URI", + description = "Callback URI") + String callbackUri() default "callbackUri"; + + @AttributeDefinition(name = "Default Redirect", + description = "Default Redirect") + String defaultRedirect() default "/"; + + @AttributeDefinition(name = "Default Connection Name", + description = "Default Connection Name") + String defaultConnectionName() default ""; + + } + + @Activate + public OidcAuthenticationHandler(@Reference(policyOption = ReferencePolicyOption.GREEDY) @NotNull SlingRepository repository, + @NotNull BundleContext bundleContext, @Reference List<ClientConnection> connections, + @Reference OAuthStateManager stateManager, + @Reference OAuthTokenStore tokenStore, Config config, + @Reference(cardinality = ReferenceCardinality.OPTIONAL, policyOption = ReferencePolicyOption.GREEDY) LoginCookieManager loginCookieManager, + @Reference(policyOption = ReferencePolicyOption.GREEDY) UserInfoProcessor userInfoProcessor + ) { + + this.repository = repository; + this.connections = connections.stream() + .collect(Collectors.toMap( ClientConnection::name, Function.identity())); + this.stateManager = stateManager; + this.tokenStore = tokenStore; + this.idp = config.idp(); + this.callbackUri = config.callbackUri(); + this.defaultRedirect = config.defaultRedirect(); + this.loginCookieManager = loginCookieManager; + this.defaultConnectionName = config.defaultConnectionName(); + this.userInfoProcessor = userInfoProcessor; + + logger.debug("activate: registering ExternalIdentityProvider"); + bundleContext.registerService( + new String[]{ExternalIdentityProvider.class.getName(), CredentialsSupport.class.getName()}, new OidcIdentityProvider(idp), + null); + + logger.info("OidcAuthenticationHandler successfully activated"); + + } + + + + @Override + public AuthenticationInfo extractCredentials(@Nullable HttpServletRequest request, @Nullable HttpServletResponse response) { + logger.debug("inside extractCredentials"); + + // Check if the request is authenticated by a oidc login token + AuthenticationInfo authInfo = loginCookieManager.verifyLoginCookie(request, response); + if (authInfo != null) { + // User has a login token + return authInfo; + } + + //The request is not authenticate. Check the Authorization Code + StringBuffer requestURL = request.getRequestURL(); + if ( request.getQueryString() != null ) + requestURL.append('?').append(request.getQueryString()); + + AuthorizationResponse authResponse; + Optional<OAuthState> clientState; + Cookie stateCookie = null; + try { + authResponse = AuthorizationResponse.parse(new URI(requestURL.toString())); + + clientState = stateManager.toOAuthState(authResponse.getState()); + if ( !clientState.isPresent() ) { + logger.debug("No state found in authorization response"); + // Return null to indicate that the handler cannot extract credentials + return null; + } + + Cookie[] cookies = request.getCookies(); + // iterate over the cookie and get the one with name OAuthStateManager.COOKIE_NAME_REQUEST_KEY + for (Cookie cookie : cookies) { + if (OAuthStateManager.COOKIE_NAME_REQUEST_KEY.equals(cookie.getName())) { + stateCookie = cookie; + break; + } + } + if ( stateCookie == null ) { + logger.debug("Failed state check: No request cookie named '{}' found", OAuthStateManager.COOKIE_NAME_REQUEST_KEY); + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + return AuthenticationInfo.FAIL_AUTH; + } + + } catch (ParseException | URISyntaxException e) { + logger.debug("Failed to parse authorization response"); + return null; + } + + try { + String stateFromAuthServer = clientState.get().perRequestKey(); + String stateFromClient = stateCookie.getValue(); + if ( ! stateFromAuthServer.equals(stateFromClient) ) + throw new IllegalStateException("Failed state check: request keys from client and server are not the same"); + + if ( !authResponse.indicatesSuccess() ) { + AuthorizationErrorResponse errorResponse = authResponse.toErrorResponse(); + throw new OAuthCallbackException("Authentication failed", new RuntimeException(toErrorMessage("Error in authentication response", errorResponse))); + } + + Optional<String> redirect = Optional.ofNullable(clientState.get().redirect()); + // TODO: find a better way to pass it? + request.setAttribute(REDIRECT_ATTRIBUTE_NAME,redirect); + + String authCode = authResponse.toSuccessResponse().getAuthorizationCode().getValue(); + + String desiredConnectionName = clientState.get().connectionName(); + if ( desiredConnectionName == null || desiredConnectionName.isEmpty() ) + throw new IllegalArgumentException("No connection found in clientState"); + + ClientConnection connection = connections.get(desiredConnectionName); + if ( connection == null ) + throw new IllegalArgumentException(String.format("Requested unknown connection '%s'", desiredConnectionName)); + + ResolvedOAuthConnection conn = ResolvedOAuthConnection.resolve(connection); + + ClientID clientId = new ClientID(conn.clientId()); + Secret clientSecret = new Secret(conn.clientSecret()); + ClientSecretBasic clientCredentials = new ClientSecretBasic(clientId, clientSecret); + + AuthorizationCode code = new AuthorizationCode(authCode); + + URI tokenEndpoint = new URI(conn.tokenEndpoint()); + + TokenRequest tokenRequest = new TokenRequest.Builder( + tokenEndpoint, + clientCredentials, + new AuthorizationCodeGrant(code, new URI(callbackUri)) + ).build(); + + HTTPRequest httpRequest = tokenRequest.toHTTPRequest(); + // GitHub requires an explicitly set Accept header, otherwise the response is url encoded + // https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github + // see also https://bitbucket.org/connect2id/oauth-2.0-sdk-with-openid-connect-extensions/issues/107/support-application-x-www-form-urlencoded + httpRequest.setAccept("application/json"); + HTTPResponse httpResponse = httpRequest.send(); + + // extract oid token from the response + TokenResponse tokenResponse = OIDCTokenResponseParser.parse(httpResponse); + if ( !tokenResponse.indicatesSuccess() ) { + logger.debug("Token error. Received code: {}, message: {}", tokenResponse.toErrorResponse().getErrorObject().getCode(), tokenResponse.toErrorResponse().getErrorObject().getDescription()); + throw new OAuthCallbackException("Token exchange error", new RuntimeException(toErrorMessage("Error in token response", tokenResponse.toErrorResponse()))); + } + // Make the request to userInfo + // TODO: fix the cast and manage OAuthConnections as well? + HTTPResponse httpResponseUserInfo = new UserInfoRequest(new URI(((OidcConnectionImpl)connection).userInfoUrl()), tokenResponse.toSuccessResponse().getTokens().getAccessToken()) + .toHTTPRequest() + .send(); + + UserInfoResponse userInfoResponse = UserInfoResponse.parse(httpResponseUserInfo); + if (! userInfoResponse.indicatesSuccess()) { + // The request failed, e.g. due to invalid or expired token + logger.debug("UserInfo error. Received code: {}, message: {}",userInfoResponse.toErrorResponse().getErrorObject().getCode(), userInfoResponse.toErrorResponse().getErrorObject().getDescription()); + throw new OAuthCallbackException("Token exchange error", new RuntimeException(toErrorMessage("Error in token response", tokenResponse.toErrorResponse()))); + + } + + // Extract the claims + UserInfo userInfo = userInfoResponse.toSuccessResponse().getUserInfo(); + + //process credentials + OidcAuthCredentials credentials = userInfoProcessor.process(userInfo, tokenResponse, idp); + + //create authInfo + authInfo = new AuthenticationInfo(AUTH_TYPE, userInfoProcessor.getSubject(userInfo)); + authInfo.put(JcrResourceConstants.AUTHENTICATION_INFO_CREDENTIALS, credentials); + + logger.info("User {} authenticated", userInfo.getSubject()); + return authInfo; + + + } catch (IllegalStateException | IllegalArgumentException | OAuthCallbackException e) { + logger.error("State check failed", e); + return AuthenticationInfo.FAIL_AUTH; + } catch (Exception e) { + logger.error("Unknown error", e); + return AuthenticationInfo.FAIL_AUTH; + } + + } + + private static String toErrorMessage(String context, ErrorResponse error) { + + ErrorObject errorObject = error.getErrorObject(); + StringBuilder message = new StringBuilder(); + + message.append(context) + .append(": ") + .append(errorObject.getCode()); + + message.append(". Status code: ").append(errorObject.getHTTPStatusCode()); + + String description = errorObject.getDescription(); + if ( description != null ) + message.append(". ").append(description); + + return message.toString(); + } + + @Override + public boolean requestCredentials(HttpServletRequest request, HttpServletResponse response) { + logger.debug("inside requestCredentials"); + try { + String desiredConnectionName = request.getParameter("c"); + if ( desiredConnectionName == null ) { + logger.debug("Missing mandatory request parameter 'c' using default connection '{}'", defaultConnectionName); + desiredConnectionName = defaultConnectionName; + } + + ClientConnection connection = connections.get(desiredConnectionName); + if ( connection == null ) { + if ( logger.isDebugEnabled() ) + logger.debug("Client requested unknown connection '{}'; known: '{}'", desiredConnectionName, connections.keySet()); Review Comment: ## Logging should not be vulnerable to injection attacks <!--SONAR_ISSUE_KEY:AZXrzRE6yoTu9vQCDDf--->Change this code to not log user-controlled data. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-auth-oauth-client&issues=AZXrzRE6yoTu9vQCDDf-&open=AZXrzRE6yoTu9vQCDDf-&pullRequest=14">SonarQube Cloud</a></p> [Show more details](https://github.com/apache/sling-org-apache-sling-auth-oauth-client/security/code-scanning/2) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
