AuthUtils { @NonNull static Intent parseTokenUriToIntent(@NonNull Uri uri) throws LoginAuthenticationException { final long expiresIn; try { expiresIn = Long.valueOf(uri.getQueryParameter(KEY_EXPIRATION_TIME)); } catch (NumberFormatException ex) { throw new LoginAuthenticationException(AuthenticationError.INVALID_RESPONSE); } final String accessToken = uri.getQueryParameter(KEY_TOKEN); final String refreshToken = uri.getQueryParameter(KEY_REFRESH_TOKEN); final String scope = uri.getQueryParameter(KEY_SCOPES); final String tokenType = uri.getQueryParameter(KEY_TOKEN_TYPE); if (TextUtils.isEmpty(accessToken) || TextUtils.isEmpty(scope) || TextUtils.isEmpty(tokenType)) { throw new LoginAuthenticationException(AuthenticationError.INVALID_RESPONSE); } Intent data = new Intent(); data.putExtra(LoginManager.EXTRA_ACCESS_TOKEN, accessToken); data.putExtra(LoginManager.EXTRA_REFRESH_TOKEN, refreshToken); data.putExtra(LoginManager.EXTRA_SCOPE, scope); data.putExtra(LoginManager.EXTRA_EXPIRES_IN, expiresIn); data.putExtra(LoginManager.EXTRA_TOKEN_TYPE, tokenType); return data; }  static boolean isRedirectUriRegistered(@NonNull Activity activity, @NonNull Uri uri); @NonNull static String scopeCollectionToString(@NonNull Collection<Scope> scopes); static String customScopeCollectionToString(@NonNull Collection<String> scopes); static String mergeScopeStrings(String... scopes);  }
@Test public void generateAccessTokenFromUrl_whenNullFragment_shouldThrowInvalidResponseError() { String redirectUri = "http: try { AuthUtils.parseTokenUriToIntent(Uri.parse(redirectUri)); fail("Should throw an exception"); } catch (LoginAuthenticationException e) { assertEquals(AuthenticationError.INVALID_RESPONSE, e.getAuthenticationError()); } }
@Test public void generateAccessTokenFromUrl_whenValidErrorInQueryParameter_shouldThrowAuthenticationError() { String redirectUri = "http: try { AuthUtils.parseTokenUriToIntent(Uri.parse(redirectUri)); fail("Should throw an exception"); } catch (LoginAuthenticationException e) { assertEquals(AuthenticationError.INVALID_RESPONSE, e.getAuthenticationError()); } }
@Test public void generateAccessTokenFromUrl_whenInvalidErrorInQueryParameter_shouldThrowAuthenticationError() { String redirectUri = "http: try { AuthUtils.parseTokenUriToIntent(Uri.parse(redirectUri)); fail("Should throw an exception"); } catch (LoginAuthenticationException e) { assertEquals(AuthenticationError.INVALID_RESPONSE, e.getAuthenticationError()); } }
@Test public void generateAccessTokenFromUrl_whenNoToken_shouldThrowAuthenticationError() { String redirectUrl = "http: try { AuthUtils.parseTokenUriToIntent(Uri.parse(redirectUrl)); fail("Should throw an exception"); } catch (LoginAuthenticationException e) { assertEquals(AuthenticationError.INVALID_RESPONSE, e.getAuthenticationError()); } }
@Test public void generateAccessTokenFromUrl_whenNoExpirationTime_shouldThrowAuthenticationError() { String redirectUrl = "http: try { AuthUtils.parseTokenUriToIntent(Uri.parse(redirectUrl)); fail("Should throw an exception"); } catch (LoginAuthenticationException e) { assertEquals(AuthenticationError.INVALID_RESPONSE, e.getAuthenticationError()); } }
@Test public void generateAccessTokenFromUrl_whenNoScopes_shouldThrowAuthenticationError() { String redirectUrl = "http: + "&expires_in=" + EXPIRATION_TIME; try { AuthUtils.parseTokenUriToIntent(Uri.parse(redirectUrl)); fail("Should throw an exception"); } catch (LoginAuthenticationException e) { assertEquals(AuthenticationError.INVALID_RESPONSE, e.getAuthenticationError()); } }
@Test public void generateAccessTokenFromUrl_whenBadExpirationTime_shouldThrowAuthenticationError() { String redirectUrl = "http: + "&scope=history"; try { AuthUtils.parseTokenUriToIntent(Uri.parse(redirectUrl)); fail("Should throw an exception"); } catch (LoginAuthenticationException e) { assertEquals(AuthenticationError.INVALID_RESPONSE, e.getAuthenticationError()); } }
@Test public void generateAccessTokenFromUrl_whenBadScopes_shouldThrowAuthenticationError() { String redirectUrl = "http: EXPIRATION_TIME + "&scope=history notAScopeAtAll"; try { AuthUtils.parseTokenUriToIntent(Uri.parse(redirectUrl)); fail("Should throw an exception"); } catch (LoginAuthenticationException e) { assertEquals(AuthenticationError.INVALID_RESPONSE, e.getAuthenticationError()); } }