This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #584 from microsoft/trboehre/jwt-cert
Merge pull request #567 from microsoft/trboehre/expiredcert
- Loading branch information
Showing
10 changed files
with
445 additions
and
118 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
...ector/src/main/java/com/microsoft/bot/connector/authentication/CachingOpenIdMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.bot.connector.authentication; | ||
|
||
import com.auth0.jwk.Jwk; | ||
import com.auth0.jwk.JwkException; | ||
import com.auth0.jwk.SigningKeyNotFoundException; | ||
import com.auth0.jwk.UrlJwkProvider; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.security.interfaces.RSAPublicKey; | ||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Maintains a cache of OpenID metadata keys. | ||
*/ | ||
class CachingOpenIdMetadata implements OpenIdMetadata { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CachingOpenIdMetadata.class); | ||
private static final int CACHE_DAYS = 5; | ||
|
||
private String url; | ||
private long lastUpdated; | ||
private ObjectMapper mapper; | ||
private Map<String, Jwk> keyCache = new HashMap<>(); | ||
private final Object sync = new Object(); | ||
|
||
/** | ||
* Constructs a OpenIdMetaData cache for a url. | ||
* | ||
* @param withUrl The url. | ||
*/ | ||
CachingOpenIdMetadata(String withUrl) { | ||
url = withUrl; | ||
mapper = new ObjectMapper().findAndRegisterModules(); | ||
} | ||
|
||
/** | ||
* Gets a openid key. | ||
* | ||
* <p> | ||
* Note: This could trigger a cache refresh, which will incur network calls. | ||
* </p> | ||
* | ||
* @param keyId The JWT key. | ||
* @return The cached key. | ||
*/ | ||
@Override | ||
public OpenIdMetadataKey getKey(String keyId) { | ||
synchronized (sync) { | ||
// If keys are more than 5 days old, refresh them | ||
if (lastUpdated < System.currentTimeMillis() - Duration.ofDays(CACHE_DAYS).toMillis()) { | ||
refreshCache(); | ||
} | ||
|
||
// Search the cache even if we failed to refresh | ||
return findKey(keyId); | ||
} | ||
} | ||
|
||
private void refreshCache() { | ||
keyCache.clear(); | ||
|
||
try { | ||
URL openIdUrl = new URL(this.url); | ||
HashMap<String, String> openIdConf = | ||
this.mapper.readValue(openIdUrl, new TypeReference<HashMap<String, Object>>() { | ||
}); | ||
URL keysUrl = new URL(openIdConf.get("jwks_uri")); | ||
lastUpdated = System.currentTimeMillis(); | ||
UrlJwkProvider provider = new UrlJwkProvider(keysUrl); | ||
keyCache = provider.getAll().stream().collect(Collectors.toMap(Jwk::getId, jwk -> jwk)); | ||
} catch (IOException e) { | ||
LOGGER.error(String.format("Failed to load openID config: %s", e.getMessage())); | ||
lastUpdated = 0; | ||
} catch (SigningKeyNotFoundException keyexception) { | ||
LOGGER.error("refreshCache", keyexception); | ||
lastUpdated = 0; | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private OpenIdMetadataKey findKey(String keyId) { | ||
if (!keyCache.containsKey(keyId)) { | ||
LOGGER.warn("findKey: keyId " + keyId + " doesn't exist."); | ||
return null; | ||
} | ||
|
||
try { | ||
Jwk jwk = keyCache.get(keyId); | ||
OpenIdMetadataKey key = new OpenIdMetadataKey(); | ||
key.key = (RSAPublicKey) jwk.getPublicKey(); | ||
key.endorsements = (List<String>) jwk.getAdditionalAttributes().get("endorsements"); | ||
key.certificateChain = jwk.getCertificateChain(); | ||
return key; | ||
} catch (JwkException e) { | ||
String errorDescription = String.format("Failed to load keys: %s", e.getMessage()); | ||
LOGGER.warn(errorDescription); | ||
} | ||
return null; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...c/main/java/com/microsoft/bot/connector/authentication/CachingOpenIdMetadataResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.bot.connector.authentication; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* Maintains a cache of OpenIdMetadata objects. | ||
*/ | ||
public class CachingOpenIdMetadataResolver implements OpenIdMetadataResolver { | ||
private static final ConcurrentMap<String, CachingOpenIdMetadata> OPENID_METADATA_CACHE = | ||
new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Gets the OpenIdMetadata object for the specified key. | ||
* @param metadataUrl The key | ||
* @return The OpenIdMetadata object. If the key is not found, an new OpenIdMetadata | ||
* object is created. | ||
*/ | ||
@Override | ||
public OpenIdMetadata get(String metadataUrl) { | ||
return OPENID_METADATA_CACHE | ||
.computeIfAbsent(metadataUrl, key -> new CachingOpenIdMetadata(metadataUrl)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.