protected ClientResponseFailure mockForbiddenResponse() { ClientResponseFailure error = mock(ClientResponseFailure.class); ClientResponse response = mock(ClientResponse.class); Response.Status responseStatus = Response.Status.FORBIDDEN; when(error.getResponse()).thenReturn(response); when(response.getResponseStatus()).thenReturn(responseStatus); return error; }
protected ClientResponseFailure mockForbiddenResponse() { ClientResponseFailure error = mock(ClientResponseFailure.class); ClientResponse response = mock(ClientResponse.class); Response.Status responseStatus = Response.Status.FORBIDDEN; when(error.getResponse()).thenReturn(response); when(response.getResponseStatus()).thenReturn(responseStatus); return error; }
protected void consumeRealm(final Consumer<RealmResource> consumer) { try { consumer.accept(getRealmResource()); } catch (ClientResponseFailure e) { if (STATUS_NOT_AUTHORIZED == e.getResponse().getResponseStatus().getStatusCode()) { throw new RealmManagementNotAuthorizedException(getKeyCloakInstance().getRealm()); } else { throw new SecurityManagementException(e); } } }
protected void consumeRealm(final Consumer<RealmResource> consumer) { try { consumer.accept(getRealmResource()); } catch (ClientResponseFailure e) { if (STATUS_NOT_AUTHORIZED == e.getResponse().getResponseStatus().getStatusCode()) { throw new RealmManagementNotAuthorizedException(getKeyCloakInstance().getRealm()); } else { throw new SecurityManagementException(e); } } }
protected RoleRepresentation getRoleRepresentation(String name, RoleResource roleResource) { if (roleResource != null) { try { return roleResource.toRepresentation(); } catch (NotFoundException e) { throw new GroupNotFoundException(name); } catch (ClientResponseFailure clientResponseFailure) { int status = clientResponseFailure.getResponse().getResponseStatus().getStatusCode(); if (404 == status) { throw new GroupNotFoundException(name); } } catch (Exception e) { throw new SecurityManagementException(e); } } throw new GroupNotFoundException(name); }
protected RoleRepresentation getRoleRepresentation(String name, RoleResource roleResource) { if (roleResource != null) { try { return roleResource.toRepresentation(); } catch (NotFoundException e) { throw new GroupNotFoundException(name); } catch (ClientResponseFailure clientResponseFailure) { int status = clientResponseFailure.getResponse().getResponseStatus().getStatusCode(); if (404 == status) { throw new GroupNotFoundException(name); } } catch (Exception e) { throw new SecurityManagementException(e); } } throw new GroupNotFoundException(name); }
/** * Get resources */ @Test(dependsOnMethods = {"getOneResource"}) public void getResources() throws Exception { showTitle("getResources"); List<String> resources = null; try { resources = getResourceService().getResourceList("Bearer " + pat.getAccessToken(), ""); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); throw ex; } assertNotNull(resources, "Resources is null"); assertTrue(resources.contains(this.resourceId), "Resource list doesn't contain added resource"); }
protected RuntimeException handleException(final Exception e) { if (e instanceof ClientResponseFailure) { final ClientResponseFailure crf = ((ClientResponseFailure) e); final ClientResponse response = crf.getResponse(); final int status = response.getStatus(); switch (status) { case HttpURLConnection.HTTP_BAD_REQUEST: return new BadRequestException(e); case HttpURLConnection.HTTP_INTERNAL_ERROR: return new InternalServerErrorException(e); case HttpURLConnection.HTTP_UNAUTHORIZED: return new UnauthorisedException(e); case 426: return new UpgradeException(e); case HttpURLConnection.HTTP_NOT_FOUND: return new NotFoundException(e); case HttpURLConnection.HTTP_FORBIDDEN: return new ForbiddenException(e); default: return crf; } } else if (e instanceof ProviderException) { return (ProviderException) e; } else if (e instanceof RuntimeException) { return (RuntimeException) e; } else { return new RuntimeException(e); } } }
String entity = (String) ex.getResponse().getEntity(String.class); System.out.println(entity); assertEquals(ex.getResponse().getStatus(), Response.Status.FORBIDDEN.getStatusCode(), "Unexpected response status");
/** * Test for registering permissions for resource */ @Test public void testRegisterPermissionForInvalidResource() throws Exception { showTitle("testRegisterPermissionForInvalidResource"); UmaPermission permission = new UmaPermission(); permission.setResourceId(this.registerResourceTest.resourceId + "1"); permission.setScopes(Arrays.asList("http://photoz.example.com/dev/scopes/view", "http://photoz.example.com/dev/scopes/all")); PermissionTicket ticket = null; try { ticket = getPermissionService().registerPermission( "Bearer " + this.registerResourceTest.pat.getAccessToken(), UmaPermissionList.instance(permission)); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); assertTrue(ex.getResponse().getStatus() != Response.Status.CREATED.getStatusCode() && ex.getResponse().getStatus() != Response.Status.OK.getStatusCode() , "Unexpected response status"); } assertNull(ticket, "Resource permission is not null"); } }
/** * Test UMA resource description modification with invalid PAT */ @Test(dependsOnMethods = {"modifyResource"}) public void testModifyResourceWithInvalidPat() throws Exception { showTitle("testModifyResourceWithInvalidPat"); UmaResourceResponse resourceStatus = null; try { UmaResource resource = new UmaResource(); resource.setName("Photo Album 4"); resource.setIconUri("http://www.example.com/icons/flower.png"); resource.setScopes(Arrays.asList("http://photoz.example.com/dev/scopes/view", "http://photoz.example.com/dev/scopes/all")); resourceStatus = getResourceService().updateResource("Bearer " + pat.getAccessToken() + "_invalid", this.resourceId + "_invalid", resource); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); assertEquals(ex.getResponse().getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "Unexpected response status"); } assertNull(resourceStatus, "Resource status is not null"); }
/** * Delete resource */ @Test(dependsOnMethods = {"getResources"}) public void deleteResource() throws Exception { showTitle("testDeleteResource"); try { getResourceService().deleteResource("Bearer " + pat.getAccessToken(), this.resourceId); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); throw ex; } } }
/** * Test non existing UMA resource description modification */ @Test(dependsOnMethods = {"modifyResource"}) public void modifyNotExistingResource() throws Exception { showTitle("modifyNotExistingResource"); try { UmaResource resource = new UmaResource(); resource.setName("Photo Album 3"); resource.setIconUri("http://www.example.com/icons/flower.png"); resource.setScopes(Arrays.asList("http://photoz.example.com/dev/scopes/view", "http://photoz.example.com/dev/scopes/all")); getResourceService().updateResource("Bearer " + pat.getAccessToken(), "fake_resource_id", resource); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); int status = ex.getResponse().getStatus(); assertTrue(status != Response.Status.OK.getStatusCode(), "Unexpected response status"); } }
/** * RP requests RPT with ticket and gets needs_info error (not all claims are provided, so redirect to claims-gathering endpoint) */ @Test(dependsOnMethods = {"rsRegisterPermissions"}) @Parameters({"umaPatClientId", "umaPatClientSecret"}) public void requestRptAndGetNeedsInfo(String umaPatClientId, String umaPatClientSecret) throws Exception { showTitle("requestRptAndGetNeedsInfo"); try { tokenService.requestRpt( "Basic " + encodeCredentials(umaPatClientId, umaPatClientSecret), GrantType.OXAUTH_UMA_TICKET.getValue(), permissionFlowTest.ticket, null, null, null, null, null); } catch (ClientResponseFailure ex) { // expected need_info error : // sample: {"error":"need_info","ticket":"c024311b-f451-41db-95aa-cd405f16eed4","required_claims":[{"issuer":["https://localhost:8443"],"name":"country","claim_token_format":["http://openid.net/specs/openid-connect-core-1_0.html#IDToken"],"claim_type":"string","friendly_name":"country"},{"issuer":["https://localhost:8443"],"name":"city","claim_token_format":["http://openid.net/specs/openid-connect-core-1_0.html#IDToken"],"claim_type":"string","friendly_name":"city"}],"redirect_user":"https://localhost:8443/restv1/uma/gather_claimsgathering_id=sampleClaimsGathering&&?gathering_id=sampleClaimsGathering&&"} String entity = (String) ex.getResponse().getEntity(String.class); System.out.println(entity); assertEquals(ex.getResponse().getStatus(), Response.Status.FORBIDDEN.getStatusCode(), "Unexpected response status"); needInfo = Util.createJsonMapper().readValue(entity, UmaNeedInfoResponse.class); assert_(needInfo); return; } throw new AssertionError("need_info error was not returned"); }
/** * Get resource */ @Test(dependsOnMethods = {"modifyResource"}) public void getOneResource() throws Exception { showTitle("getOneResource"); try { UmaResourceWithId resource = getResourceService().getResource("Bearer " + pat.getAccessToken(), this.resourceId); assertEquals(resource.getType(), "myType"); UmaResourceWithId resourceWithExpression = getResourceService().getResource("Bearer " + pat.getAccessToken(), this.resourceIdWithScopeExpression); assertEquals(resourceWithExpression.getScopeExpression(), MODIFY_SCOPE_EXPRESSION); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); throw ex; } }
/** * Test for getting meta data configuration */ @Test @Parameters({"umaMetaDataUrl"}) public void testGetUmaMetaDataConfiguration(final String umaMetaDataUrl) throws Exception { showTitle("testGetUmaMetaDataConfiguration"); UmaMetadataService metaDataConfigurationService = UmaClientFactory.instance().createMetadataService(umaMetaDataUrl, clientExecutor(true)); // Get meta data UmaMetadata c = null; try { c = metaDataConfigurationService.getMetadata(); } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); throw ex; } UmaTestUtil.assert_(c); }
public Object extractEntity(ClientRequestContext context, Object... args) { try { context.getClientResponse().checkFailureStatus(); } catch (ClientResponseFailure ce) { // If ClientResponseFailure do a copy of the response and then release the connection, // we need to use the copy here and not the original response context.getErrorHandler().clientErrorHandling((BaseClientResponse) ce.getResponse(), ce); } catch (RuntimeException e) { context.getErrorHandler().clientErrorHandling(context.getClientResponse(), e); } if (release) context.getClientResponse().releaseConnection(); return null; } };
public Object extractEntity(ClientRequestContext context, Object... args) { try { context.getClientResponse().checkFailureStatus(); } catch (ClientResponseFailure ce) { // If ClientResponseFailure do a copy of the response and then release the connection, // we need to use the copy here and not the original response context.getErrorHandler().clientErrorHandling((BaseClientResponse) ce.getResponse(), ce); } catch (RuntimeException e) { context.getErrorHandler().clientErrorHandling(context.getClientResponse(), e); } if (release) context.getClientResponse().releaseConnection(); return null; } };
public String registerResource(List<String> scopes) throws Exception { try { UmaResource resource = new UmaResource(); resource.setName("Photo Album"); resource.setIconUri("http://www.example.com/icons/flower.png"); resource.setScopes(scopes); resource.setType("myType"); UmaResourceResponse resourceStatus = getResourceService().addResource("Bearer " + pat.getAccessToken(), resource); UmaTestUtil.assert_(resourceStatus); this.resourceId = resourceStatus.getId(); return this.resourceId; } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); throw ex; } }
public String registerResourceWithScopeExpression(String scopeExpression) throws Exception { try { UmaResource resource = new UmaResource(); resource.setName("Photo Album"); resource.setIconUri("http://www.example.com/icons/flower.png"); resource.setScopeExpression(scopeExpression); resource.setType("myType"); UmaResourceResponse resourceStatus = getResourceService().addResource("Bearer " + pat.getAccessToken(), resource); UmaTestUtil.assert_(resourceStatus); this.resourceIdWithScopeExpression = resourceStatus.getId(); return this.resourceIdWithScopeExpression; } catch (ClientResponseFailure ex) { System.err.println(ex.getResponse().getEntity(String.class)); throw ex; } }