Skip to content

Commit f21faca

Browse files
author
Santiago Gonzalez
committed
PR feedback
1 parent 05183e0 commit f21faca

File tree

9 files changed

+42
-44
lines changed

9 files changed

+42
-44
lines changed

README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ To run this sample, you'll need:
4848

4949
To successfully use this sample, you need a working installation of [Java](https://openjdk.java.net/install/) and [Maven](https://maven.apache.org/).
5050

51-
### Step 2: Clone or download this repository your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then **switch directory**.
52-
Change your portal session to the desired Azure AD tenant.
53-
1. In the portal menu, select the **Azure Active Directory** service, and then select **App registrations**.
54-
51+
### Step 2: Clone or download this repository
5552

5653
From your shell or command line:
5754

@@ -69,7 +66,7 @@ As a first step you'll need to:
6966

7067
1. Sign in to the [Azure portal](https://portal.azure.com) using either a work or school account or a personal Microsoft account.
7168
1. If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory. Change your portal session to the desired Azure AD tenant.
72-
In the portal menu, select the Azure Active Directory service, and then select App registrations.
69+
1. In the portal menu, select the Azure Active Directory service, and then select App registrations.
7370
> In the next steps, you might need the tenant name (or directory name) or the tenant ID (or directory ID). These are presented in the **Properties** of the Azure Active Directory window respectively as *Name* and *Directory ID*
7471
7572
#### Register the Web Api app (Java-webapi)

msal-obo-sample/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<dependency>
2020
<groupId>com.microsoft.azure</groupId>
2121
<artifactId>msal4j</artifactId>
22-
<version>1.9.1</version>
22+
<version>1.10.0</version>
2323
</dependency>
2424
<dependency>
2525
<groupId>org.json</groupId>

msal-obo-sample/src/main/java/com/microsoft/azure/msalobosample/OboAuthProvider.java

+10-18
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import com.microsoft.aad.msal4j.ClientCredentialFactory;
88
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
99
import com.microsoft.aad.msal4j.IAuthenticationResult;
10-
import com.microsoft.aad.msal4j.MsalException;
1110
import com.microsoft.aad.msal4j.OnBehalfOfParameters;
12-
import com.microsoft.aad.msal4j.SilentParameters;
1311
import com.microsoft.aad.msal4j.UserAssertion;
1412
import com.microsoft.graph.authentication.BaseAuthenticationProvider;
1513
import org.jetbrains.annotations.NotNull;
@@ -51,11 +49,11 @@ public CompletableFuture<String> getAuthorizationTokenAsync(@Nonnull URL url) {
5149

5250
// Gets incoming access token and generates cache key. The cache key will be used to store
5351
// the tokens for the incoming request.
54-
String authToken = this.getAuthToken();
52+
String authToken = this.getAccessTokenFromRequest();
5553
String cacheKey = Hashing.sha256().hashString(authToken, StandardCharsets.UTF_8).toString();
5654

5755
IAuthenticationResult authResult;
58-
ConfidentialClientApplication application = null;
56+
ConfidentialClientApplication application;
5957
try {
6058
application = ConfidentialClientApplication
6159
.builder(clientId, ClientCredentialFactory.createFromSecret(secret))
@@ -67,21 +65,15 @@ public CompletableFuture<String> getAuthorizationTokenAsync(@Nonnull URL url) {
6765
application.tokenCache().deserialize(cachedTokens);
6866
}
6967

70-
SilentParameters silentParameters =
71-
SilentParameters.builder(Collections.singleton(scope))
68+
OnBehalfOfParameters parameters =
69+
OnBehalfOfParameters.builder(Collections.singleton(scope),
70+
new UserAssertion(authToken))
7271
.build();
73-
authResult = application.acquireTokenSilently(silentParameters).join();
72+
authResult = application.acquireToken(parameters).join();
73+
7474
} catch (Exception ex) {
75-
if (ex.getCause() instanceof MsalException) {
76-
OnBehalfOfParameters parameters =
77-
OnBehalfOfParameters.builder(Collections.singleton(scope),
78-
new UserAssertion(authToken))
79-
.build();
80-
authResult = application.acquireToken(parameters).join();
81-
} else {
82-
throw new AuthException(String.format("Error acquiring token from AAD: %s", ex.getMessage()),
83-
ex.getCause());
84-
}
75+
throw new AuthException(String.format("Error acquiring token from AAD: %s", ex.getMessage()),
76+
ex.getCause());
8577
}
8678

8779
cacheManager.getCache("tokens").put(cacheKey, application.tokenCache().serialize());
@@ -93,7 +85,7 @@ public CompletableFuture<String> getAuthorizationTokenAsync(@Nonnull URL url) {
9385
* be exchanged for an access token to access Microsoft Graph, on behalf of the user that is
9486
* signed in the web application.
9587
*/
96-
private String getAuthToken() {
88+
private String getAccessTokenFromRequest() {
9789
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
9890

9991
String res = null;

msal-web-sample/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<dependency>
2020
<groupId>com.microsoft.azure</groupId>
2121
<artifactId>msal4j</artifactId>
22-
<version>1.9.1</version>
22+
<version>1.10.0</version>
2323
</dependency>
2424
<dependency>
2525
<groupId>com.nimbusds</groupId>

msal-web-sample/src/main/java/com/microsoft/azure/msalwebsample/AuthFilter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void doFilterInternal(HttpServletRequest httpRequest, HttpServletResponse
7474
// Attempt to refresh tokens and session
7575
IAuthenticationResult result = authHelper.getAuthResultBySilentFlow(
7676
httpRequest,
77-
authHelper.getApiDefaultScope());
77+
authHelper.getOboDefaultScope());
7878
authHelper.setSessionPrincipal(httpRequest, result);
7979
}
8080
} catch (MsalException msalException) {

msal-web-sample/src/main/java/com/microsoft/azure/msalwebsample/AuthHelper.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AuthHelper {
2929
private String clientSecret;
3030
private String authority;
3131
private String redirectUri;
32-
private String apiDefaultScope;
32+
private String oboDefaultScope;
3333
private String logoutRedirectUrl;
3434

3535
@Autowired
@@ -41,7 +41,7 @@ public void init() {
4141
authority = configuration.getAuthority();
4242
clientSecret = configuration.getSecretKey();
4343
redirectUri = configuration.getRedirectUri();
44-
apiDefaultScope = configuration.getApiDefaultScope();
44+
oboDefaultScope = configuration.getOboDefaultScope();
4545
logoutRedirectUrl = configuration.getLogoutRedirectUri();
4646
}
4747

@@ -115,7 +115,7 @@ String getRedirectUrl(String state, String nonce) {
115115
AuthorizationRequestUrlParameters parameters =
116116
AuthorizationRequestUrlParameters
117117
.builder(redirectUri,
118-
new HashSet<>(Arrays.asList(apiDefaultScope)))
118+
new HashSet<>(Arrays.asList(oboDefaultScope)))
119119
.responseMode(ResponseMode.FORM_POST)
120120
.prompt(Prompt.SELECT_ACCOUNT)
121121
.state(state)
@@ -166,8 +166,8 @@ static boolean containsAuthenticationCode(HttpServletRequest httpRequest) {
166166
return isPostRequest && (containsErrorData || containsCode || containIdToken);
167167
}
168168

169-
public String getApiDefaultScope() {
170-
return apiDefaultScope;
169+
public String getOboDefaultScope() {
170+
return oboDefaultScope;
171171
}
172172

173173
public String getLogoutRedirectUrl() {

msal-web-sample/src/main/java/com/microsoft/azure/msalwebsample/AuthPageController.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,29 @@ public ModelAndView securePage(HttpServletRequest httpRequest) throws ParseExcep
4343
}
4444

4545
@RequestMapping("/msal4jsample/sign_out")
46-
public void signOut(HttpServletRequest httpRequest, HttpServletResponse response) throws IOException {
46+
public void signOut(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
4747
httpRequest.getSession().invalidate();
4848

49-
response.sendRedirect(AuthHelper.END_SESSION_ENDPOINT +
49+
httpResponse.sendRedirect(AuthHelper.END_SESSION_ENDPOINT +
5050
"?post_logout_redirect_uri=" + URLEncoder.encode(authHelper.getLogoutRedirectUrl(), "UTF-8"));
5151
}
5252

5353
@RequestMapping("/obo_api")
54-
public ModelAndView callOboApi(HttpServletRequest httpRequest) throws Exception {
55-
ModelAndView mav = new ModelAndView("auth_page");
56-
setAccountInfo(mav, httpRequest);
54+
public ModelAndView callOboApi(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception {
5755

58-
IAuthenticationResult result = authHelper.getAuthResultBySilentFlow(httpRequest, authHelper.configuration.apiDefaultScope);
59-
String oboApiCallResult = callOboService(result.accessToken());
56+
ModelAndView mav = new ModelAndView("auth_page");
57+
String oboApiCallResult = null;
58+
try {
59+
setAccountInfo(mav, httpRequest);
60+
61+
IAuthenticationResult result = authHelper.getAuthResultBySilentFlow(httpRequest, authHelper.configuration.oboDefaultScope);
62+
oboApiCallResult = callOboService(result.accessToken());
63+
} catch (Exception ex) {
64+
authHelper.removePrincipalFromSession(httpRequest);
65+
httpResponse.setStatus(500);
66+
httpRequest.setAttribute("error", ex.getMessage());
67+
httpRequest.getRequestDispatcher("/error").forward(httpRequest, httpResponse);
68+
}
6069

6170
mav.addObject("obo_api_call_res", oboApiCallResult);
6271
return mav;

msal-web-sample/src/main/java/com/microsoft/azure/msalwebsample/BasicConfiguration.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class BasicConfiguration {
1414
String redirectUri;
1515
String logoutRedirectUri;
1616
String secretKey;
17-
String apiDefaultScope;
17+
String oboDefaultScope;
1818

1919
public String getClientId() {
2020
return clientId;
@@ -39,8 +39,8 @@ public String getSecretKey() {
3939
return secretKey;
4040
}
4141

42-
public String getApiDefaultScope() {
43-
return apiDefaultScope;
42+
public String getOboDefaultScope() {
43+
return oboDefaultScope;
4444
}
4545

4646
public void setClientId(String clientId) {
@@ -59,8 +59,8 @@ public void setSecretKey(String secretKey) {
5959
this.secretKey = secretKey;
6060
}
6161

62-
public void setApiDefaultScope(String apiDefaultScope) {
63-
this.apiDefaultScope = apiDefaultScope;
62+
public void setOboDefaultScope(String oboDefaultScope) {
63+
this.oboDefaultScope = oboDefaultScope;
6464
}
6565

6666
public void setLogoutRedirectUri(String logoutRedirectUri) {

msal-web-sample/src/main/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ aad.redirectUri=http://localhost:8080/msal4jsample/secure/aad
66
aad.logoutRedirectUri=http://localhost:8080
77

88
# AAD scopes
9-
aad.apiDefaultScope=Enter_the_Api_Scope_Here
9+
aad.oboDefaultScope=Enter_the_Api_Scope_Here
1010

1111
# Change the port to 8443 if running HTTPS. Also update port in the redirect/logoutRedirectUri above
1212
server.port=8080

0 commit comments

Comments
 (0)