Skip to content

Add support for DC and flight SDK request parameters #2313

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

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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 @@ -67,6 +67,9 @@ public class TokenCommandParameters extends CommandParameters {
@Expose()
private final boolean forceRefresh;

@Expose()
private final String dc;

private final String loginHint;

private final List<Map.Entry<String, String>> extraOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ protected final AuthorizationRequest.Builder initializeAuthorizationRequestBuild

if (builder instanceof MicrosoftStsAuthorizationRequest.Builder) {
((MicrosoftStsAuthorizationRequest.Builder) builder).setApplicationIdentifier(parameters.getApplicationIdentifier());

if (parameters.getDc() != null) {
((MicrosoftStsAuthorizationRequest.Builder) builder).setDc(parameters.getDc());
}

if (parameters.getFlightInformation() != null && !parameters.getFlightInformation().isEmpty()) {
((MicrosoftStsAuthorizationRequest.Builder) builder).setFlightParameters(parameters.getFlightInformation());
}
}

final Set<String> scopes = parameters.getScopes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public abstract class MicrosoftAuthorizationRequest<T extends MicrosoftAuthoriza
@Accessors(prefix = "m")
private transient final String mPkceCodeVerifier;

@Getter
@Accessors(prefix = "m")
private final String mDc;

/**
* The version of the calling library.
*/
Expand Down Expand Up @@ -154,6 +158,8 @@ protected MicrosoftAuthorizationRequest(@SuppressWarnings(WarningType.rawtype_wa
mPkceCodeChallenge = challenge.getCodeChallenge();
mPkceCodeVerifier = challenge.getCodeVerifier();

mDc = builder.mDc;

mMultipleCloudAware = builder.mMultipleCloudAware;
mLibraryVersion = builder.mLibraryVersion;
mLibraryName = builder.mLibraryName;
Expand Down Expand Up @@ -183,6 +189,7 @@ public abstract static class Builder<B extends MicrosoftAuthorizationRequest.Bui
private UUID mCorrelationId;
private String mLoginHint;
private PkceChallenge mPkceChallenge;
private String mDc;
private PreferredAuthMethod mPreferredAuthMethod;

public Builder() {
Expand Down Expand Up @@ -232,6 +239,11 @@ public B setPkceChallenge(@NonNull final PkceChallenge pkceChallenge) {
return self();
}

public B setDc(String dc) {
mDc = dc;
return self();
}

public abstract B self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public class MicrosoftStsAuthorizationRequest extends MicrosoftAuthorizationRequ
protected transient AzureActiveDirectorySlice mSlice;

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
@Getter
@Accessors(prefix = "m")
protected transient Map<String, String> mFlightParameters;

// TODO private transient InstanceDiscoveryMetadata mInstanceDiscoveryMetadata;
Expand Down Expand Up @@ -256,6 +258,12 @@ public URI getAuthorizationRequestAsHttpRequest() throws ClientException {
final CommonURIBuilder builder = new CommonURIBuilder(super.getAuthorizationRequestAsHttpRequest());
builder.addParametersIfAbsent(mFlightParameters);

// DC passed as request command parameter
if (!StringUtil.isNullOrEmpty(getDc())) {
builder.addParameterIfAbsent(AzureActiveDirectorySlice.DC_PARAMETER, getDc());
}

// Slice passed as configuration parameter
if (mSlice != null) {
if (!StringUtil.isNullOrEmpty(mSlice.getSlice())) {
builder.addParameterIfAbsent(AzureActiveDirectorySlice.SLICE_PARAMETER, mSlice.getSlice());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ public MicrosoftStsTokenRequest createTokenRequest(@NonNull final MicrosoftStsAu
setTokenEndpoint(getCloudSpecificTokenEndpoint(response));
}

// If DC or flight parameters are supplied by the developer, the token endpoint URL should
// be updated to contain these query parameters.
if (request.getDc() != null || (request.getFlightParameters() != null && !request.getFlightParameters().isEmpty())) {
updateTokenEndpoint(request.getDc(), request.getFlightParameters());
}

final MicrosoftStsTokenRequest tokenRequest = new MicrosoftStsTokenRequest();
tokenRequest.setCodeVerifier(request.getPkceCodeVerifier());
tokenRequest.setCode(response.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
import com.microsoft.identity.common.java.telemetry.Telemetry;
import com.microsoft.identity.common.java.telemetry.TelemetryEventStrings;
import com.microsoft.identity.common.java.telemetry.events.UiShownEvent;
import com.microsoft.identity.common.java.util.CommonURIBuilder;
import com.microsoft.identity.common.java.util.IClockSkewManager;
import com.microsoft.identity.common.java.util.ObjectMapper;
import com.microsoft.identity.common.java.util.StringUtil;
import com.microsoft.identity.common.java.util.CommonURIBuilder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand All @@ -70,6 +70,7 @@

import javax.net.ssl.HttpsURLConnection;

import edu.umd.cs.findbugs.annotations.Nullable;
import lombok.NonNull;

import static com.microsoft.identity.common.java.AuthenticationConstants.AAD.CLIENT_REQUEST_ID;
Expand Down Expand Up @@ -290,6 +291,27 @@ protected final void setTokenEndpoint(final String tokenEndpoint) throws ClientE
}
}

protected final void updateTokenEndpoint(@Nullable String dc, @Nullable Map<String, String> flightParameters) throws ClientException {
if (dc != null || (flightParameters != null && !flightParameters.isEmpty())) {
try {
final CommonURIBuilder commonUriBuilder = new CommonURIBuilder(mTokenEndpoint);
if (!StringUtil.isNullOrEmpty(dc)) {
commonUriBuilder.setParameter(AzureActiveDirectorySlice.DC_PARAMETER, dc);
}

if (flightParameters != null && !flightParameters.isEmpty()) {
for (Map.Entry<String, String> entry : flightParameters.entrySet()) {
commonUriBuilder.setParameter(entry.getKey(), entry.getValue());
}
}

mTokenEndpoint = commonUriBuilder.build().toString();
} catch (final URISyntaxException e) {
throw new ClientException(ClientException.MALFORMED_URL, e.getMessage(), e);
}
}
}

public String getAuthorityFromTokenEndpoint() {
return mTokenEndpoint.toLowerCase(Locale.ROOT).replace("oauth2/v2.0/token", "");
}
Expand Down