use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.
the class ResourcesRestServiceTest method createAuthzClient.
private AuthzClient createAuthzClient(ClientRepresentation client) {
Map<String, Object> credentials = new HashMap<>();
credentials.put("secret", "secret");
return AuthzClient.create(new Configuration(suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth", testRealm().toRepresentation().getRealm(), client.getClientId(), credentials, httpClient));
}
use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.
the class EntitlementAPITest method testClientToClientPermissionRequest.
@Test
public void testClientToClientPermissionRequest() throws Exception {
ClientResource client = getClient(getRealm(), RESOURCE_SERVER_TEST);
AuthorizationResource authorization = client.authorization();
JSPolicyRepresentation policy = new JSPolicyRepresentation();
policy.setName(KeycloakModelUtils.generateId());
policy.setCode("$evaluation.grant();");
authorization.policies().js().create(policy).close();
ResourceRepresentation resource = new ResourceRepresentation();
resource.setName("Sensors");
try (Response response = authorization.resources().create(resource)) {
response.readEntity(ResourceRepresentation.class);
}
ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
permission.setName("View Sensor");
permission.addPolicy(policy.getName());
authorization.permissions().resource().create(permission).close();
ClientRepresentation otherClient = new ClientRepresentation();
otherClient.setClientId("serviceB");
otherClient.setServiceAccountsEnabled(true);
otherClient.setSecret("secret");
otherClient.setPublicClient(false);
getRealm().clients().create(otherClient);
Map<String, Object> credentials = new HashMap<>();
credentials.put("secret", "secret");
AuthzClient authzClient = AuthzClient.create(new Configuration(suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth", getRealm().toRepresentation().getRealm(), otherClient.getClientId(), credentials, getAuthzClient(AUTHZ_CLIENT_CONFIG).getConfiguration().getHttpClient()));
AuthorizationRequest request = new AuthorizationRequest();
request.setAudience(RESOURCE_SERVER_TEST);
AuthorizationResponse response = authzClient.authorization().authorize(request);
assertNotNull(response.getToken());
// Refresh token should not be present
assertNull(response.getRefreshToken());
}
use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.
the class EntitlementAPITest method getAuthzClient.
private AuthzClient getAuthzClient(String configFile) {
if (authzClient == null) {
Configuration configuration;
try {
configuration = JsonSerialization.readValue(httpsAwareConfigurationStream(getClass().getResourceAsStream("/authorization-test/" + configFile)), Configuration.class);
} catch (IOException e) {
throw new RuntimeException("Failed to read configuration", e);
}
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setValidateAfterInactivity(10);
connectionManager.setMaxTotal(10);
HttpClient client = HttpClients.custom().setConnectionManager(connectionManager).build();
authzClient = AuthzClient.create(new Configuration(configuration.getAuthServerUrl(), configuration.getRealm(), configuration.getResource(), configuration.getCredentials(), client));
}
return authzClient;
}
use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.
the class MyResourcesTest method createAuthzClient.
private AuthzClient createAuthzClient(ClientRepresentation client) {
Map<String, Object> credentials = new HashMap<>();
credentials.put("secret", "secret");
httpClient = HttpClientBuilder.create().build();
return AuthzClient.create(new Configuration(suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth", testRealm.getRealm(), client.getClientId(), credentials, httpClient));
}
use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.
the class EntitlementAPITest method testPermissionsAcrossResourceServers.
@Test
public void testPermissionsAcrossResourceServers() throws Exception {
String rsAId;
try (Response response = getRealm().clients().create(ClientBuilder.create().clientId("rs-a").secret("secret").serviceAccount().authorizationServicesEnabled(true).build())) {
rsAId = ApiUtil.getCreatedId(response);
}
String rsBId;
try (Response response = getRealm().clients().create(ClientBuilder.create().clientId("rs-b").secret("secret").serviceAccount().authorizationServicesEnabled(true).build())) {
rsBId = ApiUtil.getCreatedId(response);
}
ClientResource rsB = getRealm().clients().get(rsBId);
rsB.authorization().resources().create(new ResourceRepresentation("Resource A"));
JSPolicyRepresentation grantPolicy = new JSPolicyRepresentation();
grantPolicy.setName("Grant Policy");
grantPolicy.setCode("$evaluation.grant();");
rsB.authorization().policies().js().create(grantPolicy);
ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
permission.setName("Resource A Permission");
permission.addResource("Resource A");
permission.addPolicy(grantPolicy.getName());
rsB.authorization().permissions().resource().create(permission);
AuthzClient authzClient = getAuthzClient(AUTHZ_CLIENT_CONFIG);
Configuration config = authzClient.getConfiguration();
config.setResource("rs-a");
authzClient = AuthzClient.create(config);
AccessTokenResponse accessTokenResponse = authzClient.obtainAccessToken();
AccessToken accessToken = toAccessToken(accessTokenResponse.getToken());
config.setResource("rs-b");
AuthorizationRequest request = new AuthorizationRequest();
request.addPermission("Resource A");
AuthorizationResponse response = authzClient.authorization(accessTokenResponse.getToken()).authorize(request);
assertNotNull(response.getToken());
Collection<Permission> permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
assertEquals(1, permissions.size());
assertEquals("Resource A", permissions.iterator().next().getResourceName());
}
Aggregations