Skip to content

Update cache keys for MSI scenarios #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public abstract class AbstractApplicationBase implements IApplicationBase {
@Getter
private Integer readTimeoutForDefaultHttpClient;

@Accessors(fluent = true)
@Getter(AccessLevel.PACKAGE)
String tenant;

//The following fields are set in only some applications and/or set internally by the library. To avoid excessive
// type casting throughout the library they are defined here as package-private, but will not be part of this class's Builder
@Accessors(fluent = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ private ConfidentialClientApplication(Builder builder) {
log = LoggerFactory.getLogger(ConfidentialClientApplication.class);

initClientAuthentication(builder.clientCredential);

this.tenant = this.authenticationAuthority.tenant;
}

private void initClientAuthentication(IClientCredential clientCredential) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class Constants {

public static final String MANAGED_IDENTITY_CLIENT_ID = "client_id";
public static final String MANAGED_IDENTITY_RESOURCE_ID = "mi_res_id";
public static final String MANAGED_IDENTITY_DEFAULT_TENTANT = "managed_identity";

public static final String IDENTITY_ENDPOINT = "IDENTITY_ENDPOINT";
public static final String IDENTITY_HEADER = "IDENTITY_HEADER";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public class ManagedIdentityApplication extends AbstractApplicationBase implemen

private ManagedIdentityApplication(Builder builder) {
super(builder);
this.managedIdentityId = builder.managedIdentityId;

log = LoggerFactory.getLogger(ManagedIdentityApplication.class);
super.tokenCache = sharedTokenCache;

this.managedIdentityId = builder.managedIdentityId;
this.tenant = Constants.MANAGED_IDENTITY_DEFAULT_TENTANT;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Map<String, String> extraHttpHeaders() {

@Override
public String tenant() {
return "managed_identity";
return Constants.MANAGED_IDENTITY_DEFAULT_TENTANT;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private PublicClientApplication(Builder builder) {
log = LoggerFactory.getLogger(PublicClientApplication.class);
this.clientAuthentication = new ClientAuthenticationPost(ClientAuthenticationMethod.NONE,
new ClientID(clientId()));
this.tenant = this.authenticationAuthority.tenant;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private static AccessTokenCacheEntity createAccessTokenCacheEntity(TokenRequestE
at.environment(environmentAlias);
at.clientId(tokenRequestExecutor.getMsalRequest().application().clientId());
at.secret(authenticationResult.accessToken());
at.realm(tokenRequestExecutor.requestAuthority.tenant());
at.realm(tokenRequestExecutor.tenant);

String scopes = !StringHelper.isBlank(authenticationResult.scopes()) ? authenticationResult.scopes() :
tokenRequestExecutor.getMsalRequest().msalAuthorizationGrant().getScopes();
Expand Down Expand Up @@ -289,7 +289,7 @@ private static IdTokenCacheEntity createIdTokenCacheEntity(TokenRequestExecutor
idToken.environment(environmentAlias);
idToken.clientId(tokenRequestExecutor.getMsalRequest().application().clientId());
idToken.secret(authenticationResult.idToken());
idToken.realm(tokenRequestExecutor.requestAuthority.tenant());
idToken.realm(tokenRequestExecutor.tenant);

if (tokenRequestExecutor.getMsalRequest() instanceof OnBehalfOfRequest) {
OnBehalfOfRequest onBehalfOfRequest = (OnBehalfOfRequest) tokenRequestExecutor.getMsalRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ class TokenRequestExecutor {
Logger log = LoggerFactory.getLogger(TokenRequestExecutor.class);

final Authority requestAuthority;
final String tenant;
private final MsalRequest msalRequest;
private final ServiceBundle serviceBundle;

TokenRequestExecutor(Authority requestAuthority, MsalRequest msalRequest, ServiceBundle serviceBundle) {
this.requestAuthority = requestAuthority;
this.serviceBundle = serviceBundle;
this.msalRequest = msalRequest;
this.tenant = msalRequest.requestContext().apiParameters().tenant() == null ?
msalRequest.application().tenant() :
msalRequest.requestContext().apiParameters().tenant() ;
}

AuthenticationResult executeTokenRequest() throws ParseException, IOException {
Expand Down