-
Notifications
You must be signed in to change notification settings - Fork 147
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 #520 from AzureAD/dev
merge to main
- Loading branch information
Showing
17 changed files
with
318 additions
and
23 deletions.
There are no files selected for viewing
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
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
74 changes: 74 additions & 0 deletions
74
src/main/java/com/microsoft/aad/msal4j/AcquireTokenByAppProviderSupplier.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,74 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.aad.msal4j; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class AcquireTokenByAppProviderSupplier extends AuthenticationResultSupplier { | ||
|
||
private AppTokenProviderParameters appTokenProviderParameters; | ||
|
||
private ClientCredentialRequest clientCredentialRequest; | ||
|
||
AcquireTokenByAppProviderSupplier(AbstractClientApplicationBase clientApplication, | ||
ClientCredentialRequest clientCredentialRequest, | ||
AppTokenProviderParameters appTokenProviderParameters) { | ||
super(clientApplication, clientCredentialRequest); | ||
this.clientCredentialRequest = clientCredentialRequest; | ||
this.appTokenProviderParameters = appTokenProviderParameters; | ||
} | ||
|
||
private static void validateTokenProviderResult(TokenProviderResult tokenProviderResult) { | ||
if (null == tokenProviderResult.getAccessToken() || tokenProviderResult.getAccessToken().isEmpty()) { | ||
handleInvalidExternalValueError(tokenProviderResult.getAccessToken()); | ||
} | ||
|
||
if (tokenProviderResult.getExpiresInSeconds() == 0 || tokenProviderResult.getExpiresInSeconds() < 0) { | ||
handleInvalidExternalValueError(Long.valueOf(tokenProviderResult.getExpiresInSeconds()).toString()); | ||
} | ||
|
||
if (null == tokenProviderResult.getTenantId() || tokenProviderResult.getTenantId().isEmpty()) { | ||
handleInvalidExternalValueError(tokenProviderResult.getTenantId()); | ||
} | ||
} | ||
|
||
private static void handleInvalidExternalValueError(String nameOfValue) { | ||
throw new MsalClientException("The following token provider result value is invalid" + nameOfValue, "Invalid_TokenProviderResult_Input"); | ||
} | ||
|
||
@Override | ||
AuthenticationResult execute() throws Exception { | ||
|
||
AuthenticationResult authenticationResult = fetchTokenUsingAppTokenProvider(appTokenProviderParameters); | ||
|
||
TokenRequestExecutor tokenRequestExecutor = new TokenRequestExecutor( | ||
clientCredentialRequest.application().authenticationAuthority, | ||
msalRequest, | ||
clientApplication.getServiceBundle() | ||
); | ||
|
||
clientApplication.tokenCache.saveTokens(tokenRequestExecutor, authenticationResult, clientCredentialRequest.application().authenticationAuthority.host); | ||
|
||
return authenticationResult; | ||
} | ||
|
||
public AuthenticationResult fetchTokenUsingAppTokenProvider(AppTokenProviderParameters appTokenProviderParameters) throws ExecutionException, InterruptedException { | ||
|
||
CompletableFuture<TokenProviderResult> completableFuture = this.clientCredentialRequest.appTokenProvider.apply(appTokenProviderParameters); | ||
|
||
TokenProviderResult tokenProviderResult = completableFuture.get(); | ||
|
||
validateTokenProviderResult(tokenProviderResult); | ||
|
||
return AuthenticationResult.builder() | ||
.accessToken(tokenProviderResult.getAccessToken()) | ||
.refreshToken(null) | ||
.idToken(null) | ||
.expiresOn(tokenProviderResult.getExpiresInSeconds()) | ||
.refreshOn(tokenProviderResult.getRefreshInSeconds()) | ||
.build(); | ||
|
||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/microsoft/aad/msal4j/AppTokenProviderParameters.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,26 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.aad.msal4j; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.Set; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
/// The authentication parameters provided to the app token provider callback. | ||
public class AppTokenProviderParameters { | ||
|
||
/// Specifies which scopes to request. | ||
public Set<String> scopes; | ||
/// Correlation id of the authentication request. | ||
public String correlationId; | ||
/// A string with one or multiple claims. | ||
public String claims; | ||
/// tenant id | ||
public String tenantId; | ||
} |
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.