Search in sources :

Example 1 with PreconditionException

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

the class SimpleAuthenticationHelper method addCookie.

private String addCookie(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse, final String cookieName, final String cookieValue) {
    if (httpRequest == null || httpResponse == null || cookieName == null || cookieValue == null) {
        throw new PreconditionException("Required parameter is null");
    }
    final Cookie cookie = new Cookie(cookieName, "");
    cookie.setValue(cookieValue);
    cookie.setMaxAge(-1);
    cookie.setSecure(true);
    cookie.setDomain(httpRequest.getServerName());
    cookie.setPath("/");
    cookie.setHttpOnly(true);
    httpResponse.addCookie(cookie);
    return cookie.getValue();
}
Also used : Cookie(javax.servlet.http.Cookie) PreconditionException(com.microsoft.azure.oidc.exception.PreconditionException)

Example 2 with PreconditionException

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

the class SimpleAuthenticationHelper method getAuthenticationEndPoint.

private String getAuthenticationEndPoint(final HttpServletRequest httpRequest, final Token token, final Boolean isError) {
    if (httpRequest == null) {
        throw new PreconditionException("Required parameter is null");
    }
    try {
        final String requestURI = httpRequest.getRequestURI();
        final String queryString = httpRequest.getQueryString();
        final ApplicationSettings applicationSettings = applicationSettingsLoader.load();
        final Configuration configuration = configurationCache.load();
        if (configuration == null) {
            throw new GeneralException("Error loading configuration");
        }
        final HttpSession session = httpRequest.getSession(false);
        final String sessionName = session == null ? "" : session.getId();
        final StringBuilder uriStringBuilder = new StringBuilder();
        Base64 encoder = new Base64();
        if (isError) {
            final State previousState = getState(httpRequest);
            uriStringBuilder.append(previousState.getRequestURI());
        } else {
            uriStringBuilder.append(requestURI);
            if (queryString != null && !"".equals(queryString.trim())) {
                uriStringBuilder.append("?");
                uriStringBuilder.append(queryString);
            }
        }
        final String userID = token == null ? "" : token.getUserID().getValue();
        final State state = stateFactory.createState(userID, sessionName, uriStringBuilder.toString());
        final ObjectMapper mapper = new ObjectMapper();
        final String stateString = mapper.writeValueAsString(state);
        final String urlString = String.format("%s%sclient_Id=%s&state=%s&nonce=defaultNonce&redirect_uri=%s&scope=openid%%20offline_access&response_type=code+id_token&prompt=%s&response_mode=form_post", configuration.getAuthenticationEndPoint(), configuration.getAuthenticationEndPoint().getName().contains("?") ? "&" : "?", applicationSettings.getApplicationId(), new String(encoder.encode(stateString.getBytes()), "UTF-8"), URLEncoder.encode(applicationSettings.getRedirectURL().getValue(), "UTF-8"), token == null ? "login" : "none");
        return urlString;
    } catch (IOException e) {
        throw new GeneralException("IO Exception", e);
    }
}
Also used : ApplicationSettings(com.microsoft.azure.oidc.application.settings.ApplicationSettings) GeneralException(com.microsoft.azure.oidc.exception.GeneralException) Base64(org.apache.commons.codec.binary.Base64) Configuration(com.microsoft.azure.oidc.configuration.Configuration) HttpSession(javax.servlet.http.HttpSession) State(com.microsoft.azure.oidc.common.state.State) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PreconditionException(com.microsoft.azure.oidc.exception.PreconditionException)

Example 3 with PreconditionException

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

the class SimpleWellKnownParser method getAlgorithms.

@Override
public List<Algorithm> getAlgorithms(JsonNode node) {
    if (node == null) {
        throw new PreconditionException("Required parameter is null");
    }
    final List<Algorithm> algorithms = new ArrayList<Algorithm>();
    for (final JsonNode n : node.get("id_token_signing_alg_values_supported")) {
        final Algorithm algorithm = algorithmFactory.createAlgorithm(n.asText());
        algorithms.add(algorithm);
    }
    return algorithms;
}
Also used : ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Algorithm(com.microsoft.azure.oidc.common.algorithm.Algorithm) PreconditionException(com.microsoft.azure.oidc.exception.PreconditionException)

Example 4 with PreconditionException

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

the class SimpleKeyStoreLoader method loadAsync.

@Override
public Future<JsonNode> loadAsync(final EndPoint endPoint) {
    if (endPoint == null) {
        throw new PreconditionException("Required parameter is null");
    }
    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    final Future<JsonNode> future = executorService.submit(new Callable<JsonNode>() {

        public JsonNode call() throws Exception {
            return load(endPoint);
        }
    });
    executorService.shutdown();
    return future;
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) PreconditionException(com.microsoft.azure.oidc.exception.PreconditionException) PreconditionException(com.microsoft.azure.oidc.exception.PreconditionException)

Example 5 with PreconditionException

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

the class SimpleKeyStoreLoader method load.

public JsonNode load(final EndPoint endPoint) {
    if (endPoint == null) {
        throw new PreconditionException("Required parameter is null");
    }
    try {
        final StringBuilder builder = new StringBuilder();
        final URL url = new URL(endPoint.getName());
        final URLConnection connection = url.openConnection();
        try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            for (String line = in.readLine(); line != null; line = in.readLine()) {
                builder.append(line);
            }
        }
        final ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(builder.toString(), JsonNode.class);
    } catch (IOException e) {
        LOGGER.error("IO Exception", e);
    } catch (RuntimeException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) 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