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

com.sap.cloud.sdk.cloudplatform.security.AuthTokenFacade Maven / Gradle / Ivy

/*
 * Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved.
 */

package com.sap.cloud.sdk.cloudplatform.security;

import java.util.Optional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.sap.cloud.sdk.cloudplatform.security.exception.AuthTokenAccessException;
import com.sap.cloud.sdk.cloudplatform.security.exception.TokenRequestFailedException;
import com.sap.cloud.sdk.cloudplatform.servlet.Property;
import com.sap.cloud.sdk.cloudplatform.servlet.RequestContext;
import com.sap.cloud.sdk.cloudplatform.servlet.RequestContextAccessor;
import com.sap.cloud.sdk.cloudplatform.servlet.RequestContextExecutor;
import com.sap.cloud.sdk.cloudplatform.servlet.RequestContextServletFilter;
import com.sap.cloud.sdk.cloudplatform.servlet.exception.RequestContextPropertyException;

/**
 * Facade for retrieving the current {@link AuthToken}.
 */
public class AuthTokenFacade
{
    /**
     * Returns the current {@link AuthToken}, if present. An {@link AuthToken} is not present if no request is available
     * or the request does not contain an "Authorization" header.
     *
     * @return An {@link Optional} of the current {@link AuthToken}.
     *
     * @throws AuthTokenAccessException
     *             If there is an issue while trying to access the {@link AuthToken}.
     */
    @Nonnull
    public Optional getCurrentToken()
        throws AuthTokenAccessException
    {
        final Optional requestContext = RequestContextAccessor.getCurrentRequestContext();

        if( !requestContext.isPresent() ) {
            throw new AuthTokenAccessException(
                "Failed to get authorization token: no "
                    + RequestContext.class.getSimpleName()
                    + " available."
                    + " Have you correctly configured a "
                    + RequestContextServletFilter.class.getSimpleName()
                    + " or have you wrapped your logic in a "
                    + RequestContextExecutor.class.getSimpleName()
                    + " when executing background tasks that are not triggered by a request?");
        }

        final Optional> property;
        try {
            property = requestContext.get().getProperty(AuthTokenRequestContextListener.PROPERTY_AUTH_TOKEN);
        }
        catch( final RequestContextPropertyException e ) {
            throw new AuthTokenAccessException("Failed to get authorization token.", e);
        }

        if( !property.isPresent() ) {
            throw new AuthTokenAccessException(
                "Failed to get authorization token: "
                    + RequestContext.class.getSimpleName()
                    + " property '"
                    + AuthTokenRequestContextListener.PROPERTY_AUTH_TOKEN
                    + "' not initialized."
                    + " Have you correctly configured a "
                    + AuthTokenRequestContextListener.class.getSimpleName()
                    + " in the relevant "
                    + RequestContextServletFilter.class.getSimpleName()
                    + " or "
                    + RequestContextExecutor.class.getSimpleName()
                    + "?");
        }

        @Nullable
        final Exception exception = property.get().getException();
        if( exception != null ) {
            throw new AuthTokenAccessException("Failed to get authorization token.", exception);
        }

        @Nullable
        final AuthToken value = (AuthToken) property.get().getValue();
        return Optional.ofNullable(value);
    }

    /**
     * Retrieves a validated authentication token from the bound XSUAA instance.
     *
     * @return An authentication token from the XSUAA instance.
     *
     * @throws TokenRequestFailedException
     *             If no XSUAA instance was bound or the communication with the service failed.
     */
    @Nonnull
    public AuthToken getXsuaaServiceToken()
        throws TokenRequestFailedException
    {
        return new AuthTokenRequest().getXsuaaServiceToken();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy