anchela commented on code in PR #14: URL: https://github.com/apache/sling-org-apache-sling-auth-oauth-client/pull/14#discussion_r2062998944
########## src/main/java/org/apache/sling/auth/oauth_client/impl/OidcAuthenticationHandler.java: ########## @@ -0,0 +1,509 @@ +/* + * 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.jose.JOSEException; +import com.nimbusds.jose.JWSAlgorithm; +import com.nimbusds.jose.proc.BadJOSEException; +import com.nimbusds.oauth2.sdk.AuthorizationCode; +import com.nimbusds.oauth2.sdk.AuthorizationCodeGrant; +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.Issuer; +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.IDTokenClaimsSet; +import com.nimbusds.openid.connect.sdk.claims.UserInfo; +import com.nimbusds.openid.connect.sdk.validators.IDTokenValidator; +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.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.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +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; + + private String idp; + + private final String callbackUri; + + private LoginCookieManager loginCookieManager; + + private String defaultRedirect; + + private String defaultConnectionName; + + private UserInfoProcessor userInfoProcessor; + + private boolean userInfoEnabled; + + // 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 ""; + + @AttributeDefinition(name = "UserInfo Enabled", + description = "UserInfo Enabled") + boolean userInfoEnabled() default true; + + } + + @Activate + public OidcAuthenticationHandler(@Reference(policyOption = ReferencePolicyOption.GREEDY) @NotNull SlingRepository repository, + @NotNull BundleContext bundleContext, @Reference List<ClientConnection> connections, + @Reference OAuthStateManager stateManager, + 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.idp = config.idp(); + this.callbackUri = config.callbackUri(); + this.defaultRedirect = config.defaultRedirect(); + this.loginCookieManager = loginCookieManager; + this.defaultConnectionName = config.defaultConnectionName(); + this.userInfoProcessor = userInfoProcessor; + this.userInfoEnabled = config.userInfoEnabled(); + + 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) { Review Comment: i gave it a try.... @nscendoni , please check if i didn't introduce any bugs :) -- 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]
