Skip to content

Commit d846a76

Browse files
Merge pull request #929 from AzureAD/nebharg/AddAPIsTR
Add APIs to add client capabilities and claims
2 parents 765852e + 722f9c5 commit d846a76

File tree

5 files changed

+248
-36
lines changed

5 files changed

+248
-36
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByManagedIdentitySupplier.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ AuthenticationResult execute() throws Exception {
5252
SilentParameters parameters = SilentParameters
5353
.builder(scopes)
5454
.tenant(managedIdentityParameters.tenant())
55+
.claims(managedIdentityParameters.claims())
5556
.build();
5657

5758
RequestContext context = new RequestContext(

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ManagedIdentityApplication.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import org.slf4j.LoggerFactory;
77

8+
import java.util.List;
89
import java.util.concurrent.CompletableFuture;
910

1011
/**
@@ -56,6 +57,7 @@ static IEnvironmentVariables getEnvironmentVariables() {
5657
public ManagedIdentityId getManagedIdentityId() {
5758
return this.managedIdentityId;
5859
}
60+
5961
@Override
6062
public CompletableFuture<IAuthenticationResult> acquireTokenForManagedIdentity(ManagedIdentityParameters managedIdentityParameters)
6163
throws Exception {
@@ -86,6 +88,7 @@ public static class Builder extends AbstractApplicationBase.Builder<Builder> {
8688

8789
private String resource;
8890
private ManagedIdentityId managedIdentityId;
91+
private List<String> clientCapabilities;
8992

9093
private Builder(ManagedIdentityId managedIdentityId) {
9194
super(managedIdentityId.getIdType() == ManagedIdentityIdType.SYSTEM_ASSIGNED ?
@@ -99,9 +102,22 @@ public Builder resource(String resource) {
99102
return self();
100103
}
101104

105+
/**
106+
* Informs the token issuer that the application is able to perform complex authentication actions.
107+
* For example, "cp1" means that the application is able to perform conditional access evaluation,
108+
* because the application has been setup to parse WWW-Authenticate headers associated with a 401 response from the protected APIs,
109+
* and to retry the request with claims API.
110+
*
111+
* @param clientCapabilities a list of capabilities (e.g., ["cp1"]) recognized by the token service.
112+
* @return instance of Builder of ManagedIdentityApplication.
113+
*/
114+
public Builder clientCapabilities(List<String> clientCapabilities) {
115+
this.clientCapabilities = clientCapabilities;
116+
return self();
117+
}
118+
102119
@Override
103120
public ManagedIdentityApplication build() {
104-
105121
return new ManagedIdentityApplication(this);
106122
}
107123

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ManagedIdentityParameters.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ public class ManagedIdentityParameters implements IAcquireTokenParameters {
1414

1515
String resource;
1616
boolean forceRefresh;
17+
String claims;
1718

18-
private ManagedIdentityParameters(String resource, boolean forceRefresh) {
19+
private ManagedIdentityParameters(String resource, boolean forceRefresh, String claims) {
1920
this.resource = resource;
2021
this.forceRefresh = forceRefresh;
22+
this.claims = claims;
2123
}
2224

2325
@Override
@@ -27,7 +29,17 @@ public Set<String> scopes() {
2729

2830
@Override
2931
public ClaimsRequest claims() {
30-
return null;
32+
if (claims == null || claims.isEmpty()) {
33+
return null;
34+
}
35+
36+
try {
37+
return ClaimsRequest.formatAsClaimsRequest(claims);
38+
} catch (Exception ex) {
39+
// Log the exception if the claims JSON is invalid
40+
throw new MsalClientException("Failed to parse claims JSON: " + ex.getMessage(),
41+
AuthenticationErrorCode.INVALID_JSON);
42+
}
3143
}
3244

3345
@Override
@@ -69,6 +81,7 @@ public String resource() {
6981
public static class ManagedIdentityParametersBuilder {
7082
private String resource;
7183
private boolean forceRefresh;
84+
private String claims;
7285

7386
ManagedIdentityParametersBuilder() {
7487
}
@@ -83,8 +96,25 @@ public ManagedIdentityParametersBuilder forceRefresh(boolean forceRefresh) {
8396
return this;
8497
}
8598

99+
/**
100+
* Instructs the SDK to bypass any token caches and to request new tokens with an additional claims challenge.
101+
* The claims challenge string is opaque to applications and should not be parsed.
102+
* The claims challenge string is issued either by the STS as part of an error response or by the resource,
103+
* as part of an HTTP 401 response, in the WWW-Authenticate header.
104+
* For more details see https://learn.microsoft.com/entra/identity-platform/app-resilience-continuous-access-evaluation?tabs=dotnet
105+
*
106+
* @param claims a valid JSON string representing additional claims
107+
* @return this builder instance
108+
*/
109+
public ManagedIdentityParametersBuilder claims(String claims) {
110+
ParameterValidationUtils.validateNotBlank("claims", claims);
111+
112+
this.claims = claims;
113+
return this;
114+
}
115+
86116
public ManagedIdentityParameters build() {
87-
return new ManagedIdentityParameters(this.resource, this.forceRefresh);
117+
return new ManagedIdentityParameters(this.resource, this.forceRefresh, this.claims);
88118
}
89119

90120
public String toString() {

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/ManagedIdentityTestDataProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,11 @@ public static Stream<Arguments> createDataGetSource() {
114114
Arguments.of(ManagedIdentitySourceType.IMDS, "", ManagedIdentitySourceType.DEFAULT_TO_IMDS),
115115
Arguments.of(ManagedIdentitySourceType.SERVICE_FABRIC, ManagedIdentityTests.serviceFabricEndpoint, ManagedIdentitySourceType.SERVICE_FABRIC));
116116
}
117+
118+
public static Stream<Arguments> createInvalidClaimsData() {
119+
return Stream.of(
120+
Arguments.of("invalid json format"),
121+
Arguments.of("{\"access_token\": }")
122+
);
123+
}
117124
}

0 commit comments

Comments
 (0)