All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.security.oauth2.endpoint.endsession.request.OktaEndSessionEndpoint Maven / Gradle / Ivy

There is a newer version: 4.11.1
Show newest version
/*
 * Copyright 2017-2023 original authors
 *
 * Licensed 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
 *
 * https://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 io.micronaut.security.oauth2.endpoint.endsession.request;

import io.micronaut.http.HttpRequest;
import io.micronaut.security.authentication.Authentication;
import io.micronaut.security.authentication.AuthenticationMode;
import io.micronaut.security.config.SecurityConfiguration;
import io.micronaut.security.oauth2.client.OpenIdProviderMetadata;
import io.micronaut.security.oauth2.configuration.OauthClientConfiguration;
import io.micronaut.security.oauth2.endpoint.endsession.response.EndSessionCallbackUrlBuilder;
import io.micronaut.security.oauth2.endpoint.token.response.OpenIdAuthenticationMapper;
import io.micronaut.security.token.reader.TokenResolver;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * Provides specific configuration to logout from Okta.
 *
 * @see Okta Logout Endpont
 *
 * @author Sergio del Amo
 * @since 1.2.0
 */
public class OktaEndSessionEndpoint extends AbstractEndSessionRequest {

    private static final String PARAM_POST_LOGOUT_REDIRECT_URI = "post_logout_redirect_uri";
    private static final String PARAM_ID_TOKEN_HINT = "id_token_hint";

    private final SecurityConfiguration securityConfiguration;
    private final TokenResolver> tokenResolver;

    /**
     * @param endSessionCallbackUrlBuilder The end session callback URL builder
     * @param clientConfiguration The client configuration
     * @param providerMetadata The provider metadata supplier
     * @param securityConfiguration Security configuration
     * @param tokenResolver Token Resolver
     */
    public OktaEndSessionEndpoint(EndSessionCallbackUrlBuilder endSessionCallbackUrlBuilder,
                                  OauthClientConfiguration clientConfiguration,
                                  Supplier providerMetadata,
                                  SecurityConfiguration securityConfiguration,
                                  TokenResolver> tokenResolver) {
        super(endSessionCallbackUrlBuilder, clientConfiguration, providerMetadata);
        this.securityConfiguration = securityConfiguration;
        this.tokenResolver = tokenResolver;
    }

    @Override
    protected String getUrl() {
        return providerMetadataSupplier.get().getEndSessionEndpoint();
    }

    @Override
    protected Map getArguments(HttpRequest originating,
                                               Authentication authentication) {
        Optional idToken = parseIdToken(originating, authentication);
        Map arguments = new HashMap<>();
        idToken.ifPresent(token -> arguments.put(PARAM_ID_TOKEN_HINT, token));
        arguments.put(PARAM_POST_LOGOUT_REDIRECT_URI, getRedirectUri(originating));
        return arguments;
    }

    /**
     *
     /**
     * @param request The HTTP Request
     * @param authentication The authentication
     * @return An ID token if it could be resolved
     */
    protected Optional parseIdToken(HttpRequest request, Authentication authentication) {
        Map attributes = authentication.getAttributes();
        if (attributes.containsKey(OpenIdAuthenticationMapper.OPENID_TOKEN_KEY)) {
            return Optional.of(attributes.get(OpenIdAuthenticationMapper.OPENID_TOKEN_KEY).toString());
        }
        if (securityConfiguration.getAuthentication() == AuthenticationMode.IDTOKEN) {
            return tokenResolver.resolveTokens(request).stream().findFirst();
        }
        return Optional.empty();
    }
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy