Skip to content

Commit 1ef1df5

Browse files
authored
Merge pull request #107 from AzureAD/dev
0.6.0-preview release
2 parents 22e4113 + 6652d7f commit 1ef1df5

File tree

93 files changed

+2523
-1857
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+2523
-1857
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,33 @@ The MSAL library for Java gives your app the ability to begin using the Microsof
1111

1212

1313
## Versions
14-
Current version - 0.5.0-preview
14+
Current version - 0.6.0-preview
1515

1616
You can find the changes for each version in the [change log](https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/master/changelog.txt).
1717

1818
The library is currently in preview. During the preview we reserve the right to make changes to the API, cache format, and other mechanisms of this library without notice which you will be required to take along with bug fixes or feature improvements
19+
20+
### Maven
21+
22+
```
23+
<dependency>
24+
<groupId>com.microsoft.azure</groupId>
25+
<artifactId>msal4j</artifactId>
26+
<version>0.6.0-preview</version>
27+
</dependency>
28+
```
29+
### Gradle
30+
31+
```
32+
compile group: 'com.microsoft.azure', name: 'msal4j', version: '0.6.0-preview'
33+
```
34+
1935
## Contribution
2036

2137
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
2238
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
2339
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
2440

25-
2641
## Build and Run
2742

2843
Refer [this page](https://github.com/AzureAD/microsoft-authentication-library-for-java/wiki/Maven)

pom.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.microsoft.azure</groupId>
55
<artifactId>msal4j</artifactId>
6-
<version>0.5.0-preview</version>
6+
<version>0.6.0-preview</version>
77
<packaging>jar</packaging>
88
<name>msal4j</name>
99
<description>
@@ -55,8 +55,8 @@
5555
</dependency>
5656
<dependency>
5757
<groupId>org.apache.commons</groupId>
58-
<artifactId>commons-lang3</artifactId>
59-
<version>3.9</version>
58+
<artifactId>commons-text</artifactId>
59+
<version>1.7</version>
6060
</dependency>
6161
<dependency>
6262
<groupId>org.projectlombok</groupId>
@@ -98,7 +98,7 @@
9898
<dependency>
9999
<groupId>org.apache.httpcomponents</groupId>
100100
<artifactId>httpclient</artifactId>
101-
<version>4.5</version>
101+
<version>4.5.9</version>
102102
</dependency>
103103
<dependency>
104104
<groupId>com.microsoft.azure</groupId>
@@ -133,6 +133,7 @@
133133
</dependencies>
134134

135135
<build>
136+
<sourceDirectory>${project.build.directory}/delombok</sourceDirectory>
136137
<plugins>
137138
<plugin>
138139
<groupId>org.projectlombok</groupId>

src/integrationtest/java/com.microsoft.aad.msal4j/AcquireTokenSilentIT.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,50 @@ private IPublicClientApplication getPublicClientApplicationWithTokensInCache()
171171
.get();
172172
return pca;
173173
}
174+
175+
@Test
176+
private void acquireTokenSilent_usingCommonAuthority_returnCachedAt() throws Exception {
177+
acquireTokenSilent_returnCachedTokens(TestConstants.ORGANIZATIONS_AUTHORITY);
178+
}
179+
180+
@Test
181+
private void acquireTokenSilent_usingTenantSpecificAuthority_returnCachedAt() throws Exception {
182+
LabResponse labResponse = labUserProvider.getDefaultUser(
183+
NationalCloud.AZURE_CLOUD,
184+
false);
185+
String tenantSpecificAuthority = TestConstants.MICROSOFT_AUTHORITY_HOST + labResponse.getUser().getTenantId();
186+
187+
acquireTokenSilent_returnCachedTokens(tenantSpecificAuthority);
188+
}
189+
190+
void acquireTokenSilent_returnCachedTokens(String authority) throws Exception {
191+
192+
LabResponse labResponse = labUserProvider.getDefaultUser(
193+
NationalCloud.AZURE_CLOUD,
194+
false);
195+
String password = labUserProvider.getUserPassword(labResponse.getUser());
196+
197+
PublicClientApplication pca = new PublicClientApplication.Builder(
198+
labResponse.getAppId()).
199+
authority(authority).
200+
build();
201+
202+
IAuthenticationResult interactiveAuthResult = pca.acquireToken(UserNamePasswordParameters.
203+
builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE),
204+
labResponse.getUser().getUpn(),
205+
password.toCharArray())
206+
.build())
207+
.get();
208+
209+
Assert.assertNotNull(interactiveAuthResult);
210+
211+
IAuthenticationResult silentAuthResult = pca.acquireTokenSilently(
212+
SilentParameters.builder(
213+
Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), interactiveAuthResult.account())
214+
.build())
215+
.get();
216+
217+
Assert.assertNotNull(silentAuthResult);
218+
Assert.assertEquals(interactiveAuthResult.accessToken(), silentAuthResult.accessToken());
219+
}
174220
}

src/integrationtest/java/com.microsoft.aad.msal4j/AuthorizationCodeIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private IAuthenticationResult acquireTokenInteractiveB2C(LabResponse labResponse
247247
String authCode) {
248248
IAuthenticationResult result;
249249
try{
250-
IClientCredential credential = ClientCredentialFactory.create("=]Y)_A7LX`]6\"]_PoD!)Lo24");
250+
IClientCredential credential = ClientCredentialFactory.create("");
251251
ConfidentialClientApplication cca = ConfidentialClientApplication.builder(
252252
labResponse.getAppId(),
253253
credential)

src/main/java/com/microsoft/aad/msal4j/AadInstanceDiscovery.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import java.net.URL;
1010
import java.util.Arrays;
11+
import java.util.Collections;
12+
import java.util.Set;
1113
import java.util.TreeSet;
1214
import java.util.concurrent.ConcurrentHashMap;
1315

@@ -35,6 +37,15 @@ class AadInstanceDiscovery {
3537

3638
static ConcurrentHashMap<String, InstanceDiscoveryMetadataEntry> cache = new ConcurrentHashMap<>();
3739

40+
static Set<String> getAliases(String host){
41+
if(cache.containsKey(host)){
42+
return cache.get(host).aliases();
43+
}
44+
else{
45+
return Collections.singleton(host);
46+
}
47+
}
48+
3849
private static String getAuthorizeEndpoint(String host, String tenant) {
3950
return AUTHORIZE_ENDPOINT_TEMPLATE.
4051
replace("{host}", host).

src/main/java/com/microsoft/aad/msal4j/AccountCacheEntity.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ String getKey() {
5959
return String.join(Constants.CACHE_KEY_SEPARATOR, keyParts).toLowerCase();
6060
}
6161

62-
static AccountCacheEntity create(String clientInfoStr, String environment, IdToken idToken, String policy) {
62+
static AccountCacheEntity create(String clientInfoStr, Authority requestAuthority, IdToken idToken, String policy) {
6363

6464
AccountCacheEntity account = new AccountCacheEntity();
6565
account.authorityType(MSSTS_ACCOUNT_TYPE);
6666
account.clientInfoStr = clientInfoStr;
6767
account.homeAccountId(policy != null ?
6868
account.clientInfo().toAccountIdentifier() + Constants.CACHE_KEY_SEPARATOR + policy :
6969
account.clientInfo().toAccountIdentifier());
70-
account.environment(environment);
70+
account.environment(requestAuthority.host());
71+
account.realm(requestAuthority.tenant());
7172

7273
if (idToken != null) {
73-
account.realm(idToken.tenantIdentifier);
7474
String localAccountId = !StringHelper.isBlank(idToken.objectIdentifier)
7575
? idToken.objectIdentifier : idToken.subject;
7676
account.localAccountId(localAccountId);
@@ -81,8 +81,8 @@ static AccountCacheEntity create(String clientInfoStr, String environment, IdTok
8181
return account;
8282
}
8383

84-
static AccountCacheEntity create(String clientInfoStr, String environment, IdToken idToken){
85-
return create(clientInfoStr, environment, idToken, null);
84+
static AccountCacheEntity create(String clientInfoStr, Authority requestAuthority, IdToken idToken){
85+
return create(clientInfoStr, requestAuthority, idToken, null);
8686
}
8787

8888
IAccount toAccount(){

src/main/java/com/microsoft/aad/msal4j/AcquireTokenSilentSupplier.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,35 @@ AuthenticationResult execute() throws Exception {
2828
requestAuthority,
2929
silentRequest.parameters().scopes(),
3030
clientApplication.clientId());
31-
return StringHelper.isBlank(res.accessToken()) ? null : res;
3231
}
32+
else {
33+
res = clientApplication.tokenCache.getCachedAuthenticationResult(
34+
silentRequest.parameters().account(),
35+
requestAuthority,
36+
silentRequest.parameters().scopes(),
37+
clientApplication.clientId());
3338

34-
res = clientApplication.tokenCache.getCachedAuthenticationResult(
35-
silentRequest.parameters().account(),
36-
requestAuthority,
37-
silentRequest.parameters().scopes(),
38-
clientApplication.clientId());
39-
40-
if (!silentRequest.parameters().forceRefresh() && !StringHelper.isBlank(res.accessToken())) {
41-
return res;
42-
}
39+
if (silentRequest.parameters().forceRefresh() || StringHelper.isBlank(res.accessToken())) {
4340

44-
if (!StringHelper.isBlank(res.refreshToken())) {
45-
RefreshTokenRequest refreshTokenRequest = new RefreshTokenRequest(
46-
RefreshTokenParameters.builder(silentRequest.parameters().scopes(), res.refreshToken()).build(),
47-
silentRequest.application(),
48-
silentRequest.requestContext());
41+
if (!StringHelper.isBlank(res.refreshToken())) {
42+
RefreshTokenRequest refreshTokenRequest = new RefreshTokenRequest(
43+
RefreshTokenParameters.builder(silentRequest.parameters().scopes(), res.refreshToken()).build(),
44+
silentRequest.application(),
45+
silentRequest.requestContext());
4946

50-
AcquireTokenByAuthorizationGrantSupplier acquireTokenByAuthorisationGrantSupplier =
51-
new AcquireTokenByAuthorizationGrantSupplier(clientApplication, refreshTokenRequest, requestAuthority);
47+
AcquireTokenByAuthorizationGrantSupplier acquireTokenByAuthorisationGrantSupplier =
48+
new AcquireTokenByAuthorizationGrantSupplier(clientApplication, refreshTokenRequest, requestAuthority);
5249

53-
return acquireTokenByAuthorisationGrantSupplier.execute();
54-
} else {
55-
return null;
50+
res = acquireTokenByAuthorisationGrantSupplier.execute();
51+
}
52+
else{
53+
res = null;
54+
}
55+
}
56+
}
57+
if(res == null || StringHelper.isBlank(res.accessToken())){
58+
throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, AuthenticationErrorCode.CACHE_MISS);
5659
}
60+
return res;
5761
}
5862
}

src/main/java/com/microsoft/aad/msal4j/AuthenticationErrorCode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public class AuthenticationErrorCode {
4141
*/
4242
public final static String USER_REALM_DISCOVERY_FAILED = "user_realm_discovery_failed";
4343

44+
/**
45+
* Not found in the cache
46+
*/
47+
public final static String CACHE_MISS = "cache_miss";
48+
4449
/**
4550
* Unknown error occurred
4651
*/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.aad.msal4j;
5+
6+
public class AuthenticationErrorMessage {
7+
8+
/**
9+
* Token not found it the cache
10+
*/
11+
public final static String NO_TOKEN_IN_CACHE = "Token not found it the cache";
12+
}

src/main/java/com/microsoft/aad/msal4j/RemoveAccountRunnable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.microsoft.aad.msal4j;
55

6+
import java.util.Set;
67
import java.util.concurrent.CompletionException;
78

89
class RemoveAccountRunnable implements Runnable {
@@ -21,11 +22,10 @@ class RemoveAccountRunnable implements Runnable {
2122
@Override
2223
public void run() {
2324
try {
24-
InstanceDiscoveryMetadataEntry instanceDiscoveryData =
25-
AadInstanceDiscovery.cache.get(clientApplication.authenticationAuthority.host());
25+
Set<String> aliases = AadInstanceDiscovery.getAliases(clientApplication.authenticationAuthority.host());
2626

2727
clientApplication.tokenCache.removeAccount
28-
(clientApplication.clientId(), account, instanceDiscoveryData.aliases());
28+
(clientApplication.clientId(), account, aliases);
2929

3030
} catch (Exception ex) {
3131
clientApplication.log.error(

0 commit comments

Comments
 (0)