|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This code is licensed under the MIT License. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files(the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions : |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | + |
| 24 | +package com.microsoft.aad.msal4j; |
| 25 | + |
| 26 | +import labapi.FederationProvider; |
| 27 | +import labapi.LabResponse; |
| 28 | +import labapi.LabUserProvider; |
| 29 | +import labapi.NationalCloud; |
| 30 | +import org.testng.Assert; |
| 31 | +import org.testng.annotations.BeforeClass; |
| 32 | +import org.testng.annotations.Test; |
| 33 | + |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +public class AcquireTokenSilentIT { |
| 38 | + private LabUserProvider labUserProvider; |
| 39 | + |
| 40 | + @BeforeClass |
| 41 | + public void setUp() { |
| 42 | + labUserProvider = LabUserProvider.getInstance(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void acquireTokenSilent_OrganizationAuthority_TokenRefreshed() throws Exception { |
| 47 | + |
| 48 | + // When using common, organization, or consumer tenants, cache has no way |
| 49 | + // of determining which access token to return therefore token is always refreshed |
| 50 | + IPublicClientApplication pca = getPublicClientApplicationWithTokensInCache(); |
| 51 | + |
| 52 | + IAccount account = pca.getAccounts().join().iterator().next(); |
| 53 | + SilentParameters parameters = SilentParameters.builder( |
| 54 | + Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), |
| 55 | + account).build(); |
| 56 | + |
| 57 | + IAuthenticationResult result = pca.acquireTokenSilently(parameters).get(); |
| 58 | + |
| 59 | + Assert.assertNotNull(result); |
| 60 | + Assert.assertNotNull(result.accessToken()); |
| 61 | + Assert.assertNotNull(result.idToken()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void acquireTokenSilent_LabAuthority_TokenNotRefreshed() throws Exception { |
| 66 | + // Access token should be returned from cache, and not using refresh token |
| 67 | + |
| 68 | + LabResponse labResponse = labUserProvider.getDefaultUser( |
| 69 | + NationalCloud.AZURE_CLOUD, |
| 70 | + false); |
| 71 | + String password = labUserProvider.getUserPassword(labResponse.getUser()); |
| 72 | + String labAuthority = TestConstants.MICROSOFT_AUTHORITY_HOST + labResponse.getUser().getTenantId(); |
| 73 | + |
| 74 | + PublicClientApplication pca = new PublicClientApplication.Builder( |
| 75 | + labResponse.getAppId()). |
| 76 | + authority(labAuthority). |
| 77 | + build(); |
| 78 | + |
| 79 | + IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters. |
| 80 | + builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), |
| 81 | + labResponse.getUser().getUpn(), |
| 82 | + password.toCharArray()) |
| 83 | + .build()) |
| 84 | + .get(); |
| 85 | + |
| 86 | + IAccount account = pca.getAccounts().join().iterator().next(); |
| 87 | + SilentParameters parameters = SilentParameters.builder( |
| 88 | + Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account). |
| 89 | + build(); |
| 90 | + |
| 91 | + IAuthenticationResult acquireSilentResult = pca.acquireTokenSilently(parameters).get(); |
| 92 | + |
| 93 | + Assert.assertNotNull(acquireSilentResult.accessToken()); |
| 94 | + Assert.assertNotNull(result.idToken()); |
| 95 | + // Check that access and id tokens are coming from cache |
| 96 | + Assert.assertEquals(result.accessToken(), acquireSilentResult.accessToken()); |
| 97 | + Assert.assertEquals(result.idToken(), acquireSilentResult.idToken()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void acquireTokenSilent_ForceRefresh() throws Exception { |
| 102 | + |
| 103 | + LabResponse labResponse = labUserProvider.getDefaultUser( |
| 104 | + NationalCloud.AZURE_CLOUD, |
| 105 | + false); |
| 106 | + String password = labUserProvider.getUserPassword(labResponse.getUser()); |
| 107 | + |
| 108 | + PublicClientApplication pca = new PublicClientApplication.Builder( |
| 109 | + labResponse.getAppId()). |
| 110 | + authority(TestConstants.ORGANIZATIONS_AUTHORITY). |
| 111 | + build(); |
| 112 | + |
| 113 | + IAuthenticationResult result = pca.acquireToken(UserNamePasswordParameters. |
| 114 | + builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), |
| 115 | + labResponse.getUser().getUpn(), |
| 116 | + password.toCharArray()) |
| 117 | + .build()) |
| 118 | + .get(); |
| 119 | + |
| 120 | + IAccount account = pca.getAccounts().join().iterator().next(); |
| 121 | + SilentParameters parameters = SilentParameters.builder( |
| 122 | + Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account). |
| 123 | + forceRefresh(true). |
| 124 | + build(); |
| 125 | + |
| 126 | + IAuthenticationResult resultAfterRefresh = pca.acquireTokenSilently(parameters).get(); |
| 127 | + |
| 128 | + Assert.assertNotNull(resultAfterRefresh); |
| 129 | + Assert.assertNotNull(resultAfterRefresh.accessToken()); |
| 130 | + Assert.assertNotNull(resultAfterRefresh.idToken()); |
| 131 | + // Check that new refresh and id tokens are being returned |
| 132 | + Assert.assertNotEquals(result.accessToken(), resultAfterRefresh.accessToken()); |
| 133 | + Assert.assertNotEquals(result.idToken(), resultAfterRefresh.idToken()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void acquireTokenSilent_MultipleAccountsInCache_UseCorrectAccount() throws Exception { |
| 138 | + |
| 139 | + IPublicClientApplication pca = getPublicClientApplicationWithTokensInCache(); |
| 140 | + |
| 141 | + // get lab user for different account |
| 142 | + LabResponse labResponse = labUserProvider.getAdfsUser( |
| 143 | + FederationProvider.ADFSV4, |
| 144 | + true, |
| 145 | + false); |
| 146 | + String password = labUserProvider.getUserPassword(labResponse.getUser()); |
| 147 | + |
| 148 | + // acquire token for different account |
| 149 | + pca.acquireToken(UserNamePasswordParameters. |
| 150 | + builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), |
| 151 | + labResponse.getUser().getUpn(), |
| 152 | + password.toCharArray()) |
| 153 | + .build()) |
| 154 | + .get(); |
| 155 | + |
| 156 | + Set<IAccount> accounts = pca.getAccounts().join(); |
| 157 | + IAccount account = accounts.stream().filter( |
| 158 | + x -> x.username().equalsIgnoreCase( |
| 159 | + labResponse.getUser().getUpn())).findFirst().orElse(null); |
| 160 | + |
| 161 | + SilentParameters parameters = SilentParameters.builder( |
| 162 | + Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), account). |
| 163 | + forceRefresh(true). |
| 164 | + build(); |
| 165 | + |
| 166 | + IAuthenticationResult result = pca.acquireTokenSilently(parameters).get(); |
| 167 | + |
| 168 | + Assert.assertNotNull(result); |
| 169 | + Assert.assertNotNull(result.accessToken()); |
| 170 | + Assert.assertNotNull(result.idToken()); |
| 171 | + Assert.assertEquals(result.account().username(), labResponse.getUser().getUpn()); |
| 172 | + } |
| 173 | + |
| 174 | + private IPublicClientApplication getPublicClientApplicationWithTokensInCache() |
| 175 | + throws Exception { |
| 176 | + LabResponse labResponse = labUserProvider.getDefaultUser( |
| 177 | + NationalCloud.AZURE_CLOUD, |
| 178 | + false); |
| 179 | + String password = labUserProvider.getUserPassword(labResponse.getUser()); |
| 180 | + |
| 181 | + PublicClientApplication pca = new PublicClientApplication.Builder( |
| 182 | + labResponse.getAppId()). |
| 183 | + authority(TestConstants.ORGANIZATIONS_AUTHORITY). |
| 184 | + build(); |
| 185 | + |
| 186 | + pca.acquireToken(UserNamePasswordParameters. |
| 187 | + builder(Collections.singleton(TestConstants.GRAPH_DEFAULT_SCOPE), |
| 188 | + labResponse.getUser().getUpn(), |
| 189 | + password.toCharArray()) |
| 190 | + .build()) |
| 191 | + .get(); |
| 192 | + return pca; |
| 193 | + } |
| 194 | +} |
0 commit comments