anchela commented on code in PR #14: URL: https://github.com/apache/sling-org-apache-sling-auth-oauth-client/pull/14#discussion_r2068208897
########## 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; Review Comment: i refactored the code to avoid duplications between the auth-handler and the servlet and in that effort the cookie-max-age-seconds constant got moved as private constant to the corresponding utility -- 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: commits-unsubscr...@sling.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org