Skip to content

Commit 5caa342

Browse files
Copilotrujche
andcommitted
Address code review feedback: fix token expiration, add validation, improve docs
Co-authored-by: rujche <171773178+rujche@users.noreply.github.com>
1 parent c7440f6 commit 5caa342

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

sdk/keyvault/azure-security-keyvault-jca/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,11 @@ System.out.println(result);
197197
Note if you want to use Azure managed identity, you should set the value of `azure.keyvault.uri`, and the rest of the parameters would be `null`.
198198

199199
#### Authentication with Access Token
200-
If you want to use a pre-obtained bearer token for authentication (e.g., for multi-factor authentication scenarios), you can use the `azure.keyvault.access-token` property:
200+
If you want to use a pre-obtained bearer token for authentication, you can use the `azure.keyvault.access-token` property:
201201

202202
```java
203203
// First, obtain your access token through your authentication flow
204+
// For example, authenticate with a certificate or other credential
204205
String accessToken = "<your-pre-obtained-access-token>";
205206

206207
System.setProperty("azure.keyvault.uri", "<your-azure-keyvault-uri>");
@@ -214,10 +215,12 @@ KeyStore keyStore = KeyVaultKeyStore.getKeyVaultKeyStoreBySystemProperty();
214215
// Use the keyStore as needed for your SSL/TLS operations
215216
```
216217

217-
This approach allows you to programmatically implement multi-factor authentication by:
218-
1. Authenticating your service principal with a certificate (something you have)
219-
2. Requesting a temporary access token
220-
3. Using that temporary access token (something you know) for Key Vault operations
218+
This approach is useful in scenarios where:
219+
- You need to use a specific authentication flow (e.g., certificate-based authentication) to obtain a token
220+
- You want to manage token lifecycle and refresh independently
221+
- You are working in environments where managed identity or client credentials are not suitable
222+
223+
**Note:** The provided access token is cached and used for approximately 1 hour. After that time, you will need to provide a fresh token through your application's token refresh mechanism.
221224

222225
### mTLS
223226
#### Server side mTLS

sdk/keyvault/azure-security-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/KeyVaultLoadStoreParameter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ public KeyVaultLoadStoreParameter(String keyVaultUri, String managedIdentity) {
6565
this(keyVaultUri, null, null, null, managedIdentity, null);
6666
}
6767

68+
/**
69+
* Constructor for access token authentication.
70+
*
71+
* @param keyVaultUri The Azure Key Vault URI.
72+
* @param accessToken The access token.
73+
* @param useAccessToken Marker parameter to differentiate from managedIdentity constructor (pass true).
74+
*/
75+
public KeyVaultLoadStoreParameter(String keyVaultUri, String accessToken, boolean useAccessToken) {
76+
this(keyVaultUri, null, null, null, null, accessToken);
77+
}
78+
6879
/**
6980
* Constructor.
7081
*

sdk/keyvault/azure-security-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/implementation/KeyVaultClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,12 @@ private AccessToken getAccessTokenByHttpRequest() {
227227
if (managedIdentity != null) {
228228
LOGGER.info("Using managed identity for authentication");
229229
accessToken = AccessTokenUtil.getAccessToken(resource, managedIdentity);
230-
} else if (providedAccessToken != null) {
230+
} else if (providedAccessToken != null && !providedAccessToken.isEmpty()) {
231231
LOGGER.info("Using provided access token for authentication");
232232
// Create an AccessToken object from the provided token string
233-
// We set an expiration far in the future since we don't know the actual expiration
234-
accessToken = new AccessToken(providedAccessToken, Long.MAX_VALUE);
233+
// Set expiration to 1 hour (3600 seconds) as a reasonable default since we don't know the actual expiration
234+
// The token will be treated as expired after 1 hour and the caller will need to provide a new one
235+
accessToken = new AccessToken(providedAccessToken, 3600);
235236
} else if (tenantId != null && clientId != null && clientSecret != null) {
236237
LOGGER.info("Using client credentials (client ID/secret) for authentication");
237238
String aadAuthenticationUri = getLoginUri(keyVaultUri + "certificates" + API_VERSION_POSTFIX,

0 commit comments

Comments
 (0)