KeycloakServiceClient { public KeycloakTokenResponse getIdentityProviderToken(String oauthProvider) throws ForbiddenException, BadRequestException, IOException, NotFoundException, ServerException, UnauthorizedException { String url = UriBuilder.fromUri(keycloakSettings.get().get(AUTH_SERVER_URL_SETTING)) .path("/realms/{realm}/broker/{provider}/token") .build(keycloakSettings.get().get(REALM_SETTING), oauthProvider) .toString(); try { String response = doRequest(url, HttpMethod.GET, null); return DtoFactory.getInstance() .createDtoFromJson(toJson(response), KeycloakTokenResponse.class); } catch (BadRequestException e) { if (assotiateUserPattern.matcher(e.getMessage()).matches()) { throw new UnauthorizedException(e.getMessage()); } throw e; } } @Inject  KeycloakServiceClient(KeycloakSettings keycloakSettings, JwtParser jwtParser); String getAccountLinkingURL(
      String token, String oauthProvider, String redirectAfterLogin); KeycloakTokenResponse getIdentityProviderToken(String oauthProvider);  }
@Test public void shouldReturnToken() throws Exception { String token = "token123"; String scope = "test_scope"; String tokenType = "test_type"; keycloakService = new KeycloakService(token, scope, tokenType, null); KeycloakTokenResponse response = keycloakServiceClient.getIdentityProviderToken("github"); assertNotNull(response); assertEquals(response.getAccessToken(), token); assertEquals(response.getScope(), scope); assertEquals(response.getTokenType(), tokenType); }
@Test( expectedExceptions = BadRequestException.class, expectedExceptionsMessageRegExp = "Invalid token.") public void shouldThrowBadRequestException() throws Exception { keycloakService = new KeycloakService(null, null, null, new BadRequestException("Invalid token.")); keycloakServiceClient.getIdentityProviderToken("github"); }
@Test( expectedExceptions = ForbiddenException.class, expectedExceptionsMessageRegExp = "Forbidden.") public void shouldThrowForbiddenException() throws Exception { keycloakService = new KeycloakService(null, null, null, new ForbiddenException("Forbidden.")); keycloakServiceClient.getIdentityProviderToken("github"); }
@Test( expectedExceptions = UnauthorizedException.class, expectedExceptionsMessageRegExp = "Unauthorized.") public void shouldThrowUnauthorizedException() throws Exception { keycloakService = new KeycloakService(null, null, null, new UnauthorizedException("Unauthorized.")); keycloakServiceClient.getIdentityProviderToken("github"); }
@Test( expectedExceptions = NotFoundException.class, expectedExceptionsMessageRegExp = "Not found.") public void shouldThrowNotFoundException() throws Exception { keycloakService = new KeycloakService(null, null, null, new NotFoundException("Not found.")); keycloakServiceClient.getIdentityProviderToken("github"); }
@Test( expectedExceptions = UnauthorizedException.class, expectedExceptionsMessageRegExp = "User (.+) is not associated with identity provider (.+).") public void shouldThrowUnauthorizedExceptionWhenNoProviderLink() throws Exception { keycloakService = new KeycloakService( null, null, null, new BadRequestException( "User 1234-5678-90 is not associated with identity provider gitlab.")); keycloakServiceClient.getIdentityProviderToken("github"); }