Search in sources :

Example 11 with PreconditionException

use of com.microsoft.azure.oidc.exception.PreconditionException in project azure-tools-for-java by Microsoft.

the class SimpleAuthenticationHelper method getTokenString.

@Override
public String getTokenString(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse, final String tokenName) {
    if (httpRequest == null || httpResponse == null || tokenName == null) {
        throw new PreconditionException("Required parameter is null");
    }
    final String tokenString = httpRequest.getParameter(tokenName);
    if (tokenString != null) {
        return addCookie(httpRequest, httpResponse, tokenName, tokenString);
    }
    final Cookie cookieToken = getCookie(httpRequest, tokenName);
    if (cookieToken != null) {
        return addCookie(httpRequest, httpResponse, tokenName, cookieToken.getValue());
    }
    return null;
}
Also used : Cookie(javax.servlet.http.Cookie) PreconditionException(com.microsoft.azure.oidc.exception.PreconditionException)

Example 12 with PreconditionException

use of com.microsoft.azure.oidc.exception.PreconditionException in project azure-tools-for-java by Microsoft.

the class SimpleAuthenticationFilter method doFilter.

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    final HttpServletRequest httpRequest = (HttpServletRequest) request;
    final HttpServletResponse httpResponse = (HttpServletResponse) response;
    try {
        final String tokenString = getHelper().getTokenString(httpRequest, httpResponse, TOKEN_NAME);
        final String errorString = getHelper().getErrorString(httpRequest, ERROR_NAME);
        final Boolean isAuthenticationError = getHelper().isAuthenticationError(errorString);
        final Boolean isUnauthenticated = tokenString == NO_TOKEN_STRING;
        if (isUnauthenticated || isAuthenticationError) {
            getHelper().doUnauthenticatedAction(chain, httpRequest, httpResponse, NO_TOKEN, isAuthenticationError);
            return;
        }
        final Token token = getHelper().getToken(tokenString);
        final Boolean isInvalidToken = !getHelper().isValidToken(token);
        if (isInvalidToken) {
            getHelper().doInvalidTokenAction(httpResponse);
            return;
        }
        final Boolean isActiveToken = getHelper().isActiveToken(token);
        if (isActiveToken) {
            getHelper().doActiveTokenAction(chain, httpRequest, httpResponse, token);
            return;
        }
        getHelper().doUnauthenticatedAction(chain, httpRequest, httpResponse, token, isAuthenticationError);
    } catch (GeneralException | PreconditionException e) {
        getHelper().doExceptionAction(httpResponse, e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GeneralException(com.microsoft.azure.oidc.exception.GeneralException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Token(com.microsoft.azure.oidc.token.Token) PreconditionException(com.microsoft.azure.oidc.exception.PreconditionException)

Aggregations

PreconditionException (com.microsoft.azure.oidc.exception.PreconditionException)12 GeneralException (com.microsoft.azure.oidc.exception.GeneralException)5 IOException (java.io.IOException)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Cookie (javax.servlet.http.Cookie)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Configuration (com.microsoft.azure.oidc.configuration.Configuration)3 ArrayList (java.util.ArrayList)3 HttpSession (javax.servlet.http.HttpSession)3 Base64 (org.apache.commons.codec.binary.Base64)3 ApplicationSettings (com.microsoft.azure.oidc.application.settings.ApplicationSettings)2 State (com.microsoft.azure.oidc.common.state.State)2 TimeStamp (com.microsoft.azure.oidc.common.timestamp.TimeStamp)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)2 Algorithm (com.microsoft.azure.oidc.common.algorithm.Algorithm)1 Name (com.microsoft.azure.oidc.common.name.Name)1 Key (com.microsoft.azure.oidc.configuration.key.Key)1 Exponent (com.microsoft.azure.oidc.configuration.key.exponent.Exponent)1 Modulus (com.microsoft.azure.oidc.configuration.key.modulus.Modulus)1