Skip to content

Commit

Permalink
Fixed local webserver auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Oct 13, 2024
1 parent b7f504e commit b498ec0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public static class MsaCode extends AbstractStep.FirstStepResult {
@EqualsAndHashCode.Exclude
StepMsaToken.MsaToken msaToken; // Used in device code flow

@ApiStatus.Internal
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
@PackagePrivate
@NonFinal
@ToString.Exclude
@EqualsAndHashCode.Exclude
String customRedirectUri; // Used in local webserver flow

@Override
public boolean isExpired() {
return true; // MsaCode can only be used one time and can't be refreshed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ protected LocalWebServer execute(final ILogger logger, final HttpClient httpClie

try (final ServerSocket localServer = new ServerSocket(0)) {
final int localPort = localServer.getLocalPort();
final String customRedirectUri = this.applicationDetails.getRedirectUri() + ":" + localPort;

final URL authenticationUrl = new URLWrapper(this.applicationDetails.getOAuthEnvironment().getAuthorizeUrl()).wrapQuery()
.addQueries(this.applicationDetails.getOAuthParameters())
.setQuery("redirect_uri", this.applicationDetails.getRedirectUri() + ":" + localPort)
.setQuery("redirect_uri", customRedirectUri)
.setQuery("prompt", "select_account")
.apply().toURL();

final LocalWebServer localWebServer = new LocalWebServer(
authenticationUrl.toString(),
localPort
localPort,
customRedirectUri
);
logger.info(this, "Created local webserver MSA authentication URL: " + localWebServer.getAuthenticationUrl());
localWebServerCallback.callback.accept(localWebServer);
Expand All @@ -71,6 +73,7 @@ public static class LocalWebServer extends AbstractStep.InitialInput {

String authenticationUrl;
int port;
String customRedirectUri;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ protected MsaCode execute(final ILogger logger, final HttpClient httpClient, fin

try {
final MsaCode msaCode = msaCodeFuture.get(this.timeout, TimeUnit.MILLISECONDS);
msaCode.customRedirectUri = localWebServer.getCustomRedirectUri();
httpServer.stop(0);
logger.info(this, "Got MSA Code");
return msaCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ private MsaToken execute(final ILogger logger, final HttpClient httpClient, fina
postData.put("refresh_token", codeOrRefreshToken);
} else if (type.equals("authorization_code")) {
postData.put("code", codeOrRefreshToken);
postData.put("redirect_uri", this.applicationDetails.getRedirectUri());
if (msaCode.customRedirectUri != null) {
postData.put("redirect_uri", msaCode.customRedirectUri);
} else {
postData.put("redirect_uri", this.applicationDetails.getRedirectUri());
}
} else {
throw new IllegalArgumentException("Invalid type: " + type);
}
Expand Down

0 comments on commit b498ec0

Please sign in to comment.