From e8b53faff6cbb3c03b828f6a357ce17134eb2fd2 Mon Sep 17 00:00:00 2001 From: Avery-Dunn <62066438+Avery-Dunn@users.noreply.github.com> Date: Tue, 30 Jun 2020 08:44:28 -0700 Subject: [PATCH] Update auth code URL builder (#52) --- msal-java-webapp-sample/pom.xml | 2 +- .../azure/msalwebsample/AuthHelper.java | 33 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/msal-java-webapp-sample/pom.xml b/msal-java-webapp-sample/pom.xml index 8f112c8..6264cdf 100644 --- a/msal-java-webapp-sample/pom.xml +++ b/msal-java-webapp-sample/pom.xml @@ -23,7 +23,7 @@ com.microsoft.azure msal4j - 1.4.0 + 1.6.1 com.nimbusds diff --git a/msal-java-webapp-sample/src/main/java/com/microsoft/azure/msalwebsample/AuthHelper.java b/msal-java-webapp-sample/src/main/java/com/microsoft/azure/msalwebsample/AuthHelper.java index 09c53d3..8911900 100644 --- a/msal-java-webapp-sample/src/main/java/com/microsoft/azure/msalwebsample/AuthHelper.java +++ b/msal-java-webapp-sample/src/main/java/com/microsoft/azure/msalwebsample/AuthHelper.java @@ -156,23 +156,22 @@ void sendAuthRedirect(HttpServletRequest httpRequest, HttpServletResponse httpRe String getAuthorizationCodeUrl(String claims, String scope, String registeredRedirectURL, String state, String nonce) throws UnsupportedEncodingException { - String urlEncodedScopes = scope == null ? - URLEncoder.encode("openid offline_access profile", "UTF-8") : - URLEncoder.encode("openid offline_access profile" + " " + scope, "UTF-8"); - - - String authorizationCodeUrl = authority + "oauth2/v2.0/authorize?" + - "response_type=code&" + - "response_mode=query&" + - "redirect_uri=" + URLEncoder.encode(registeredRedirectURL, "UTF-8") + - "&client_id=" + clientId + - "&scope=" + urlEncodedScopes + - (StringUtils.isEmpty(claims) ? "" : "&claims=" + claims) + - "&prompt=select_account" + - "&state=" + state - + "&nonce=" + nonce; - - return authorizationCodeUrl; + String urlEncodedScopes = scope == null ? "" : URLEncoder.encode(scope, "UTF-8"); + + PublicClientApplication pca = PublicClientApplication.builder(clientId).build(); + + AuthorizationRequestUrlParameters parameters = + AuthorizationRequestUrlParameters + .builder(registeredRedirectURL, + Collections.singleton(urlEncodedScopes)) + .responseMode(ResponseMode.QUERY) + .prompt(Prompt.SELECT_ACCOUNT) + .state(state) + .nonce(nonce) + .claimsChallenge(claims) + .build(); + + return pca.getAuthorizationRequestUrl(parameters).toString(); } private IAuthenticationResult getAuthResultByAuthCode(