|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.microsoft.aad.msal4j; |
| 5 | + |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.UUID; |
| 9 | + |
| 10 | +import static com.microsoft.aad.msal4j.ParameterValidationUtils.validateNotBlank; |
| 11 | +import static com.microsoft.aad.msal4j.ParameterValidationUtils.validateNotNull; |
| 12 | + |
| 13 | +/** |
| 14 | + * Object containing parameters for the User Federated Identity Credential (user_fic) flow. |
| 15 | + * This is used for Leg 3 of the agent identity protocol, where a federated identity credential |
| 16 | + * (obtained from Leg 2) is exchanged for a user-scoped token. |
| 17 | + * <p> |
| 18 | + * Can be used as parameter to |
| 19 | + * {@link ConfidentialClientApplication#acquireToken(UserFederatedIdentityCredentialParameters)} |
| 20 | + */ |
| 21 | +public class UserFederatedIdentityCredentialParameters implements IAcquireTokenParameters { |
| 22 | + |
| 23 | + private Set<String> scopes; |
| 24 | + private String username; |
| 25 | + private UUID userObjectId; |
| 26 | + private String assertion; |
| 27 | + private boolean forceRefresh; |
| 28 | + private ClaimsRequest claims; |
| 29 | + private Map<String, String> extraHttpHeaders; |
| 30 | + private Map<String, String> extraQueryParameters; |
| 31 | + private String tenant; |
| 32 | + |
| 33 | + private UserFederatedIdentityCredentialParameters( |
| 34 | + Set<String> scopes, |
| 35 | + String username, |
| 36 | + UUID userObjectId, |
| 37 | + String assertion, |
| 38 | + boolean forceRefresh, |
| 39 | + ClaimsRequest claims, |
| 40 | + Map<String, String> extraHttpHeaders, |
| 41 | + Map<String, String> extraQueryParameters, |
| 42 | + String tenant) { |
| 43 | + this.scopes = scopes; |
| 44 | + this.username = username; |
| 45 | + this.userObjectId = userObjectId; |
| 46 | + this.assertion = assertion; |
| 47 | + this.forceRefresh = forceRefresh; |
| 48 | + this.claims = claims; |
| 49 | + this.extraHttpHeaders = extraHttpHeaders; |
| 50 | + this.extraQueryParameters = extraQueryParameters; |
| 51 | + this.tenant = tenant; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Builder for {@link UserFederatedIdentityCredentialParameters} using a UPN (User Principal Name). |
| 56 | + * |
| 57 | + * @param scopes scopes application is requesting access to |
| 58 | + * @param username the UPN of the target user (e.g., "user@contoso.com") |
| 59 | + * @param assertion the federated identity credential assertion (JWT) obtained from Leg 2 |
| 60 | + * @return builder that can be used to construct UserFederatedIdentityCredentialParameters |
| 61 | + */ |
| 62 | + public static UserFederatedIdentityCredentialParametersBuilder builder( |
| 63 | + Set<String> scopes, String username, String assertion) { |
| 64 | + validateNotNull("scopes", scopes); |
| 65 | + validateNotBlank("username", username); |
| 66 | + validateNotBlank("assertion", assertion); |
| 67 | + |
| 68 | + return new UserFederatedIdentityCredentialParametersBuilder() |
| 69 | + .scopes(scopes) |
| 70 | + .username(username) |
| 71 | + .assertion(assertion); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Builder for {@link UserFederatedIdentityCredentialParameters} using a user Object ID. |
| 76 | + * |
| 77 | + * @param scopes scopes application is requesting access to |
| 78 | + * @param userObjectId the Object ID (OID) of the target user |
| 79 | + * @param assertion the federated identity credential assertion (JWT) obtained from Leg 2 |
| 80 | + * @return builder that can be used to construct UserFederatedIdentityCredentialParameters |
| 81 | + */ |
| 82 | + public static UserFederatedIdentityCredentialParametersBuilder builder( |
| 83 | + Set<String> scopes, UUID userObjectId, String assertion) { |
| 84 | + validateNotNull("scopes", scopes); |
| 85 | + validateNotNull("userObjectId", userObjectId); |
| 86 | + validateNotBlank("assertion", assertion); |
| 87 | + |
| 88 | + return new UserFederatedIdentityCredentialParametersBuilder() |
| 89 | + .scopes(scopes) |
| 90 | + .userObjectId(userObjectId) |
| 91 | + .assertion(assertion); |
| 92 | + } |
| 93 | + |
| 94 | + public Set<String> scopes() { |
| 95 | + return this.scopes; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return the UPN of the target user, or null if user was identified by Object ID |
| 100 | + */ |
| 101 | + public String username() { |
| 102 | + return this.username; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return the Object ID of the target user, or null if user was identified by UPN |
| 107 | + */ |
| 108 | + public UUID userObjectId() { |
| 109 | + return this.userObjectId; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return the federated identity credential assertion (JWT) |
| 114 | + */ |
| 115 | + public String assertion() { |
| 116 | + return this.assertion; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return whether to bypass the token cache and force a fresh token request |
| 121 | + */ |
| 122 | + public boolean forceRefresh() { |
| 123 | + return this.forceRefresh; |
| 124 | + } |
| 125 | + |
| 126 | + public ClaimsRequest claims() { |
| 127 | + return this.claims; |
| 128 | + } |
| 129 | + |
| 130 | + public Map<String, String> extraHttpHeaders() { |
| 131 | + return this.extraHttpHeaders; |
| 132 | + } |
| 133 | + |
| 134 | + public Map<String, String> extraQueryParameters() { |
| 135 | + return this.extraQueryParameters; |
| 136 | + } |
| 137 | + |
| 138 | + public String tenant() { |
| 139 | + return this.tenant; |
| 140 | + } |
| 141 | + |
| 142 | + public static class UserFederatedIdentityCredentialParametersBuilder { |
| 143 | + private Set<String> scopes; |
| 144 | + private String username; |
| 145 | + private UUID userObjectId; |
| 146 | + private String assertion; |
| 147 | + private boolean forceRefresh; |
| 148 | + private ClaimsRequest claims; |
| 149 | + private Map<String, String> extraHttpHeaders; |
| 150 | + private Map<String, String> extraQueryParameters; |
| 151 | + private String tenant; |
| 152 | + |
| 153 | + UserFederatedIdentityCredentialParametersBuilder() { |
| 154 | + } |
| 155 | + |
| 156 | + UserFederatedIdentityCredentialParametersBuilder scopes(Set<String> scopes) { |
| 157 | + this.scopes = scopes; |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + UserFederatedIdentityCredentialParametersBuilder username(String username) { |
| 162 | + this.username = username; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + UserFederatedIdentityCredentialParametersBuilder userObjectId(UUID userObjectId) { |
| 167 | + this.userObjectId = userObjectId; |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + UserFederatedIdentityCredentialParametersBuilder assertion(String assertion) { |
| 172 | + this.assertion = assertion; |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Forces MSAL to refresh the token from the identity provider even if a cached token is available. |
| 178 | + * |
| 179 | + * @param forceRefresh true to bypass the cache; otherwise false. Default is false. |
| 180 | + * @return the builder |
| 181 | + */ |
| 182 | + public UserFederatedIdentityCredentialParametersBuilder forceRefresh(boolean forceRefresh) { |
| 183 | + this.forceRefresh = forceRefresh; |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Claims to be requested through the OIDC claims request parameter. |
| 189 | + */ |
| 190 | + public UserFederatedIdentityCredentialParametersBuilder claims(ClaimsRequest claims) { |
| 191 | + this.claims = claims; |
| 192 | + return this; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Adds additional headers to the token request. |
| 197 | + */ |
| 198 | + public UserFederatedIdentityCredentialParametersBuilder extraHttpHeaders(Map<String, String> extraHttpHeaders) { |
| 199 | + this.extraHttpHeaders = extraHttpHeaders; |
| 200 | + return this; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Adds additional parameters to the token request. |
| 205 | + */ |
| 206 | + public UserFederatedIdentityCredentialParametersBuilder extraQueryParameters(Map<String, String> extraQueryParameters) { |
| 207 | + this.extraQueryParameters = extraQueryParameters; |
| 208 | + return this; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Overrides the tenant value in the authority URL for this request. |
| 213 | + */ |
| 214 | + public UserFederatedIdentityCredentialParametersBuilder tenant(String tenant) { |
| 215 | + this.tenant = tenant; |
| 216 | + return this; |
| 217 | + } |
| 218 | + |
| 219 | + public UserFederatedIdentityCredentialParameters build() { |
| 220 | + return new UserFederatedIdentityCredentialParameters( |
| 221 | + this.scopes, this.username, this.userObjectId, this.assertion, |
| 222 | + this.forceRefresh, this.claims, this.extraHttpHeaders, |
| 223 | + this.extraQueryParameters, this.tenant); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments