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

com.microsoft.azure.keyvault.authentication.ChallengeCache Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 */

package com.microsoft.azure.keyvault.authentication;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import okhttp3.HttpUrl;

/**
 * Handles caching of the challenge.
 */
class ChallengeCache {

    private final HashMap> cachedChallenges = new HashMap>();

    /**
     * Uses authority to retrieve the cached values.
     * 
     * @param url
     *            the url that is used as a cache key.
     * @return cached value or null if value is not available.
     */
    public Map getCachedChallenge(HttpUrl url) {
        if (url == null) {
            return null;
        }
        String authority = getAuthority(url);
        authority = authority.toLowerCase(Locale.ENGLISH);
        return cachedChallenges.get(authority);
    }

    /**
     * Uses authority to cache challenge.
     * 
     * @param url
     *            the url that is used as a cache key.
     * @param challenge
     *            the challenge to cache.
     */
    public void addCachedChallenge(HttpUrl url, Map challenge) {
        if (url == null || challenge == null) {
            return;
        }
        String authority = getAuthority(url);
        authority = authority.toLowerCase(Locale.ENGLISH);
        cachedChallenges.put(authority, challenge);
    }

    /**
     * Gets authority of a url.
     * 
     * @param url
     *            the url to get the authority for.
     * @return the authority.
     */
    public String getAuthority(HttpUrl url) {
        String scheme = url.scheme();
        String host = url.host();
        int port = url.port();
        StringBuilder builder = new StringBuilder();
        if (scheme != null) {
            builder.append(scheme).append("://");
        }
        builder.append(host);
        if (port >= 0) {
            builder.append(':').append(port);
        }
        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy