Search in sources :

Example 21 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse in project azure-activedirectory-library-for-android by AzureAD.

the class AcquireTokenSilentHandlerTest method testRefreshTokenWebRequestHasError.

/**
 * Acquire token uses refresh token, but web request returns error with an empty body.
 */
@Test
public void testRefreshTokenWebRequestHasError() throws IOException {
    FileMockContext mockContext = new FileMockContext(getContext());
    ITokenCacheStore mockCache = getCacheForRefreshToken(TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
    final String resource = "resource";
    final String clientId = "clientId";
    final AuthenticationRequest authenticationRequest = getAuthenticationRequest(VALID_AUTHORITY, resource, clientId, false);
    authenticationRequest.setUserIdentifierType(UserIdentifierType.UniqueId);
    authenticationRequest.setUserId(TEST_IDTOKEN_USERID);
    final AcquireTokenSilentHandler acquireTokenSilentHandler = getAcquireTokenHandler(mockContext, authenticationRequest, mockCache);
    // inject mocked web request handler
    final IWebRequestHandler mockedWebRequestHandler = Mockito.mock(WebRequestHandler.class);
    Mockito.when(mockedWebRequestHandler.sendPost(Mockito.any(URL.class), Mockito.<String, String>anyMap(), Mockito.any(byte[].class), Mockito.anyString())).thenReturn(new HttpWebResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, "{\"error\":\"interaction_required\" ,\"error_description\":\"Windows device is not in required device state\"}", new HashMap<String, List<String>>()));
    acquireTokenSilentHandler.setWebRequestHandler(mockedWebRequestHandler);
    try {
        acquireTokenSilentHandler.getAccessToken();
        fail("Expect exception");
    } catch (final AuthenticationException authenticationException) {
        assertTrue(authenticationException.getCode() == ADALError.AUTH_FAILED_NO_TOKEN);
        assertTrue(authenticationException.getCause() instanceof AuthenticationException);
        final AuthenticationException throwable = (AuthenticationException) authenticationException.getCause();
        assertTrue(throwable.getCode() == ADALError.SERVER_ERROR);
        assertNotNull(authenticationException.getHttpResponseBody());
        assertEquals(authenticationException.getServiceStatusCode(), HttpURLConnection.HTTP_INTERNAL_ERROR);
    }
    clearCache(mockCache);
}
Also used : HashMap(java.util.HashMap) IWebRequestHandler(com.microsoft.identity.common.adal.internal.net.IWebRequestHandler) URL(java.net.URL) HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test)

Example 22 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse in project azure-activedirectory-library-for-android by AzureAD.

the class AcquireTokenSilentHandlerTest method testMRRTExistInPreferredLocation.

@Test
public void testMRRTExistInPreferredLocation() throws IOException, JSONException {
    final FileMockContext mockContext = new FileMockContext(getContext());
    final ITokenCacheStore mockedCache = new DefaultTokenCacheStore(getContext());
    clearCache(mockedCache);
    updateAuthorityMetadataCache();
    // insert token with authority as preferred cache
    final String resource = "resource";
    final String clientId = "clientId";
    final String preferredCacheAuthority = "https://preferred.cache/test.onmicrosoft.com";
    final String mrrtForPreferredCache = "rt with preferred cache";
    final TokenCacheItem mrrtTokenCacheItem = Util.getTokenCacheItem(preferredCacheAuthority, null, clientId, TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
    mrrtTokenCacheItem.setRefreshToken(mrrtForPreferredCache);
    mrrtTokenCacheItem.setIsMultiResourceRefreshToken(true);
    saveTokenIntoCache(mockedCache, mrrtTokenCacheItem);
    final String testHostAuthority = "https://test.host/test.onmicrosoft.com";
    final TokenCacheItem mrrtWithTestHost = Util.getTokenCacheItem(testHostAuthority, null, clientId, TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
    mrrtWithTestHost.setRefreshToken("rt with test host");
    saveTokenIntoCache(mockedCache, mrrtWithTestHost);
    final AuthenticationRequest authenticationRequest = getAuthenticationRequest(testHostAuthority, resource, clientId, false);
    authenticationRequest.setUserIdentifierType(UserIdentifierType.UniqueId);
    authenticationRequest.setUserId(TEST_IDTOKEN_USERID);
    final AcquireTokenSilentHandler acquireTokenSilentHandler = getAcquireTokenHandler(mockContext, authenticationRequest, mockedCache);
    // inject mocked web request handler
    final IWebRequestHandler mockedWebRequestHandler = Mockito.mock(WebRequestHandler.class);
    Mockito.when(mockedWebRequestHandler.sendPost(Mockito.any(URL.class), Mockito.<String, String>anyMap(), AdditionalMatchers.aryEq(Util.getPostMessage(mrrtForPreferredCache, clientId, resource)), Mockito.anyString())).thenReturn(new HttpWebResponse(HttpURLConnection.HTTP_OK, Util.getSuccessTokenResponse(false, false), null));
    acquireTokenSilentHandler.setWebRequestHandler(mockedWebRequestHandler);
    try {
        final AuthenticationResult result = acquireTokenSilentHandler.getAccessToken();
        assertNotNull(result);
        assertNotNull(result.getAccessToken());
    } catch (final AuthenticationException e) {
        fail();
    }
    Mockito.verify(mockedWebRequestHandler, Mockito.times(1)).sendPost(Mockito.any(URL.class), Mockito.<String, String>anyMap(), AdditionalMatchers.aryEq(Util.getPostMessage(mrrtForPreferredCache, clientId, resource)), Mockito.anyString());
    // verify token items
    assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForMRRT(preferredCacheAuthority, clientId, TEST_IDTOKEN_USERID)));
    assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForMRRT(preferredCacheAuthority, clientId, TEST_IDTOKEN_UPN)));
    clearCache(mockedCache);
}
Also used : IWebRequestHandler(com.microsoft.identity.common.adal.internal.net.IWebRequestHandler) URL(java.net.URL) HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test)

Example 23 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse in project azure-activedirectory-library-for-android by AzureAD.

the class AcquireTokenSilentHandlerTest method testRegularRT.

// Verify if regular RT exists, if the RT is not MRRT, we only redeem token with the regular RT.
@Test
public void testRegularRT() throws IOException, JSONException {
    FileMockContext mockContext = new FileMockContext(getContext());
    final ITokenCacheStore mockedCache = new DefaultTokenCacheStore(getContext());
    final String resource = "resource";
    final String clientId = "clientId";
    // Add regular RT in the cache, RT is not MRRT
    final TokenCacheItem regularTokenCacheItem = Util.getTokenCacheItem(VALID_AUTHORITY, resource, clientId, TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
    final String regularRT = "Regular RT";
    regularTokenCacheItem.setRefreshToken(regularRT);
    saveTokenIntoCache(mockedCache, regularTokenCacheItem);
    // Add MRRT in the cache for different clientid
    final String mrrtClientId = "clientId2";
    final TokenCacheItem mrrtTokenCacheItem = Util.getTokenCacheItem(VALID_AUTHORITY, resource, mrrtClientId, TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
    final String mrrt = "MRRT Refresh Token";
    mrrtTokenCacheItem.setRefreshToken(mrrt);
    mrrtTokenCacheItem.setResource(null);
    mrrtTokenCacheItem.setFamilyClientId("familyClientId");
    mrrtTokenCacheItem.setIsMultiResourceRefreshToken(true);
    saveTokenIntoCache(mockedCache, mrrtTokenCacheItem);
    final AuthenticationRequest authenticationRequest = getAuthenticationRequest(VALID_AUTHORITY, resource, clientId, false);
    authenticationRequest.setUserIdentifierType(UserIdentifierType.UniqueId);
    authenticationRequest.setUserId(TEST_IDTOKEN_USERID);
    final AcquireTokenSilentHandler acquireTokenSilentHandler = getAcquireTokenHandler(mockContext, authenticationRequest, mockedCache);
    // inject mocked web request handler
    final IWebRequestHandler mockedWebRequestHandler = Mockito.mock(WebRequestHandler.class);
    // Token redeem with RT fail with invalid_grant.
    final byte[] postMessage = Util.getPostMessage(regularRT, clientId, resource);
    Mockito.when(mockedWebRequestHandler.sendPost(Mockito.any(URL.class), Mockito.<String, String>anyMap(), AdditionalMatchers.aryEq(postMessage), Mockito.anyString())).thenReturn(new HttpWebResponse(HttpURLConnection.HTTP_BAD_REQUEST, Util.getErrorResponseBody("invalid_grant"), null));
    acquireTokenSilentHandler.setWebRequestHandler(mockedWebRequestHandler);
    try {
        final AuthenticationResult authenticationResult = acquireTokenSilentHandler.getAccessToken();
        assertNotNull(authenticationResult);
        assertTrue(authenticationResult.getErrorCode().equalsIgnoreCase("invalid_grant"));
    } catch (AuthenticationException authException) {
        fail("Unexpected Exception");
    }
    ArgumentCaptor<byte[]> webRequestHandlerArgument = ArgumentCaptor.forClass(byte[].class);
    Mockito.verify(mockedWebRequestHandler).sendPost(Mockito.any(URL.class), Mockito.<String, String>anyMap(), webRequestHandlerArgument.capture(), Mockito.anyString());
    assertTrue(Arrays.equals(postMessage, webRequestHandlerArgument.getValue()));
    // verify regular token entry not existed
    assertNull(mockedCache.getItem(CacheKey.createCacheKeyForRTEntry(VALID_AUTHORITY, resource, clientId, TEST_IDTOKEN_USERID)));
    assertNull(mockedCache.getItem(CacheKey.createCacheKeyForRTEntry(VALID_AUTHORITY, resource, clientId, TEST_IDTOKEN_UPN)));
    // verify MRRT entry exist
    assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForMRRT(VALID_AUTHORITY, mrrtClientId, TEST_IDTOKEN_USERID)));
    assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForMRRT(VALID_AUTHORITY, mrrtClientId, TEST_IDTOKEN_UPN)));
    clearCache(mockedCache);
}
Also used : IWebRequestHandler(com.microsoft.identity.common.adal.internal.net.IWebRequestHandler) URL(java.net.URL) HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test)

Example 24 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse in project azure-activedirectory-library-for-android by AzureAD.

the class AcquireTokenSilentHandlerTest method testRTExistedInPreferredCache.

@Test
public void testRTExistedInPreferredCache() throws IOException, JSONException {
    final FileMockContext mockContext = new FileMockContext(getContext());
    final ITokenCacheStore mockedCache = new DefaultTokenCacheStore(getContext());
    clearCache(mockedCache);
    updateAuthorityMetadataCache();
    // insert token with authority as preferred cache
    final String resource = "resource";
    final String clientId = "clientId";
    // Add regular RT item without RT in the cache
    final String preferredCacheAuthority = "https://preferred.cache/test.onmicrosoft.com";
    final String rtForPreferredCache = "rt with preferred cache";
    final TokenCacheItem rtTokenCacheItem = Util.getTokenCacheItem(preferredCacheAuthority, resource, clientId, TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
    rtTokenCacheItem.setRefreshToken(rtForPreferredCache);
    rtTokenCacheItem.setIsMultiResourceRefreshToken(false);
    saveTokenIntoCache(mockedCache, rtTokenCacheItem);
    // insert token with authority as aliased host
    final String testHostAuthority = "https://test.host/test.onmicrosoft.com";
    final TokenCacheItem itemWithTestHost = Util.getTokenCacheItem(testHostAuthority, resource, clientId, TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
    itemWithTestHost.setRefreshToken("rt with test host");
    saveTokenIntoCache(mockedCache, itemWithTestHost);
    final AuthenticationRequest authenticationRequest = getAuthenticationRequest(testHostAuthority, resource, clientId, false);
    authenticationRequest.setUserIdentifierType(UserIdentifierType.UniqueId);
    authenticationRequest.setUserId(TEST_IDTOKEN_USERID);
    final AcquireTokenSilentHandler acquireTokenSilentHandler = getAcquireTokenHandler(mockContext, authenticationRequest, mockedCache);
    // inject mocked web request handler
    final IWebRequestHandler mockedWebRequestHandler = Mockito.mock(WebRequestHandler.class);
    Mockito.when(mockedWebRequestHandler.sendPost(Mockito.any(URL.class), Mockito.<String, String>anyMap(), AdditionalMatchers.aryEq(Util.getPostMessage(rtForPreferredCache, clientId, resource)), Mockito.anyString())).thenReturn(new HttpWebResponse(HttpURLConnection.HTTP_OK, Util.getSuccessTokenResponse(false, false), null));
    acquireTokenSilentHandler.setWebRequestHandler(mockedWebRequestHandler);
    try {
        final AuthenticationResult result = acquireTokenSilentHandler.getAccessToken();
        assertNotNull(result);
        assertNotNull(result.getAccessToken());
    } catch (final AuthenticationException e) {
        fail();
    }
    Mockito.verify(mockedWebRequestHandler, Mockito.times(1)).sendPost(Mockito.any(URL.class), Mockito.<String, String>anyMap(), AdditionalMatchers.aryEq(Util.getPostMessage(rtForPreferredCache, clientId, resource)), Mockito.anyString());
    // verify token items
    assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForRTEntry(preferredCacheAuthority, resource, clientId, TEST_IDTOKEN_USERID)));
    assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForRTEntry(preferredCacheAuthority, resource, clientId, TEST_IDTOKEN_UPN)));
    clearCache(mockedCache);
}
Also used : IWebRequestHandler(com.microsoft.identity.common.adal.internal.net.IWebRequestHandler) URL(java.net.URL) HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test)

Example 25 with HttpWebResponse

use of com.microsoft.identity.common.adal.internal.net.HttpWebResponse in project azure-activedirectory-library-for-android by AzureAD.

the class AcquireTokenSilentHandlerTest method testFRTFailedWithInvalidGrant.

/**
 * Make sure if we have a family token in the cache and we fail to redeem access token with FRT, we correctly fail.
 * Also make sure only FRT token entry is deleted.
 */
@Test
public void testFRTFailedWithInvalidGrant() throws IOException, JSONException {
    FileMockContext mockContext = new FileMockContext(getContext());
    final ITokenCacheStore mockCache = new DefaultTokenCacheStore(mockContext);
    mockCache.removeAll();
    // note: if only FRT exists, cache key will be hard-coded to 1
    final TokenCacheItem frTokenCacheItem = getTokenCacheItemWithFoCI(TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN, AuthenticationConstants.MS_FAMILY_ID);
    saveTokenIntoCache(mockCache, frTokenCacheItem);
    final String resource = "resource";
    final String clientId = "clientId";
    final AuthenticationRequest authenticationRequest = getAuthenticationRequest(VALID_AUTHORITY, resource, clientId, false);
    authenticationRequest.setUserIdentifierType(UserIdentifierType.UniqueId);
    authenticationRequest.setUserId(TEST_IDTOKEN_USERID);
    final AcquireTokenSilentHandler acquireTokenSilentHandler = getAcquireTokenHandler(mockContext, authenticationRequest, mockCache);
    // inject mocked web request handler
    final IWebRequestHandler mockedWebRequestHandler = Mockito.mock(WebRequestHandler.class);
    Mockito.when(mockedWebRequestHandler.sendPost(Mockito.any(URL.class), Mockito.<String, String>anyMap(), Mockito.any(byte[].class), Mockito.anyString())).thenReturn(new HttpWebResponse(HttpURLConnection.HTTP_BAD_REQUEST, Util.getErrorResponseBody("invalid_grant"), null));
    acquireTokenSilentHandler.setWebRequestHandler(mockedWebRequestHandler);
    try {
        final AuthenticationResult authenticationResult = acquireTokenSilentHandler.getAccessToken();
        assertNotNull(authenticationResult);
        assertTrue(authenticationResult.getErrorCode().equalsIgnoreCase("invalid_grant"));
    } catch (AuthenticationException e) {
        fail("Unexpected exception");
    }
    // Verify Cache entry
    // Token request with FRT should delete token cache entry with FRT
    assertNull(mockCache.getItem(CacheKey.createCacheKeyForFRT(VALID_AUTHORITY, AuthenticationConstants.MS_FAMILY_ID, TEST_IDTOKEN_UPN)));
    assertNull(mockCache.getItem(CacheKey.createCacheKeyForFRT(VALID_AUTHORITY, AuthenticationConstants.MS_FAMILY_ID, TEST_IDTOKEN_UPN)));
    clearCache(mockCache);
}
Also used : IWebRequestHandler(com.microsoft.identity.common.adal.internal.net.IWebRequestHandler) URL(java.net.URL) HttpWebResponse(com.microsoft.identity.common.adal.internal.net.HttpWebResponse) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test)

Aggregations

HttpWebResponse (com.microsoft.identity.common.adal.internal.net.HttpWebResponse)35 Test (org.junit.Test)29 URL (java.net.URL)21 IWebRequestHandler (com.microsoft.identity.common.adal.internal.net.IWebRequestHandler)19 SmallTest (androidx.test.filters.SmallTest)16 HashMap (java.util.HashMap)6 WebRequestHandler (com.microsoft.identity.common.adal.internal.net.WebRequestHandler)5 HttpURLConnection (java.net.HttpURLConnection)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Handler (android.os.Handler)1 Gson (com.google.gson.Gson)1 ChallengeResponse (com.microsoft.aad.adal.ChallengeResponseBuilder.ChallengeResponse)1 JWSBuilder (com.microsoft.identity.common.adal.internal.JWSBuilder)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1