use of org.keycloak.representations.idm.authorization.AuthorizationRequest in project keycloak by keycloak.
the class AbstractResourceServerTest method authorize.
protected AuthorizationResponse authorize(String userName, String password, String[] additionalScopes, String rpt, String accessToken, String claimToken, String tokenFormat, PermissionRequest... permissions) {
ProtectionResource protection;
if (userName != null) {
protection = getAuthzClient().protection(userName, password);
} else {
protection = getAuthzClient().protection();
}
String ticket = protection.permission().create(Arrays.asList(permissions)).getTicket();
AuthorizationRequest authorizationRequest = new AuthorizationRequest(ticket);
if (additionalScopes != null) {
StringBuilder builder = new StringBuilder();
for (String scope : additionalScopes) {
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(scope);
}
authorizationRequest.setScope(builder.toString());
}
authorizationRequest.setRpt(rpt);
authorizationRequest.setClaimTokenFormat(tokenFormat);
authorizationRequest.setClaimToken(claimToken);
org.keycloak.authorization.client.resource.AuthorizationResource authorization;
if (userName != null) {
authorization = getAuthzClient().authorization(userName, password);
} else if (accessToken != null) {
authorization = getAuthzClient().authorization(accessToken);
} else {
authorization = getAuthzClient().authorization();
}
return authorization.authorize(authorizationRequest);
}
use of org.keycloak.representations.idm.authorization.AuthorizationRequest in project keycloak by keycloak.
the class AuthorizationAPITest method testAccessTokenWithUmaAuthorization.
public void testAccessTokenWithUmaAuthorization(String authzConfigFile) {
AuthzClient authzClient = getAuthzClient(authzConfigFile);
PermissionRequest request = new PermissionRequest("Resource A");
String ticket = authzClient.protection().permission().create(request).getTicket();
AuthorizationResponse response = authzClient.authorization("marta", "password").authorize(new AuthorizationRequest(ticket));
assertNotNull(response.getToken());
}
use of org.keycloak.representations.idm.authorization.AuthorizationRequest in project keycloak by keycloak.
the class AuthzClientCredentialsTest method testSuccessfulAuthorizationRequest.
@Test
public void testSuccessfulAuthorizationRequest() throws Exception {
AuthzClient authzClient = getAuthzClient("keycloak-with-jwt-authentication.json");
ProtectionResource protection = authzClient.protection();
PermissionRequest request = new PermissionRequest("Default Resource");
PermissionResponse ticketResponse = protection.permission().create(request);
String ticket = ticketResponse.getTicket();
AuthorizationResponse authorizationResponse = authzClient.authorization("marta", "password").authorize(new AuthorizationRequest(ticket));
String rpt = authorizationResponse.getToken();
assertNotNull(rpt);
AccessToken accessToken = new JWSInput(rpt).readJsonContent(AccessToken.class);
AccessToken.Authorization authorization = accessToken.getAuthorization();
assertNotNull(authorization);
List<Permission> permissions = new ArrayList<>(authorization.getPermissions());
assertFalse(permissions.isEmpty());
assertEquals("Default Resource", permissions.get(0).getResourceName());
}
use of org.keycloak.representations.idm.authorization.AuthorizationRequest in project keycloak by keycloak.
the class ClientScopePolicyTest method testWithoutExpectedClientScope.
@Test
public void testWithoutExpectedClientScope() {
// Access Resource A with client scope baz.
AuthzClient authzClient = getAuthzClient();
PermissionRequest request = new PermissionRequest("Resource A");
String ticket = authzClient.protection().permission().create(request).getTicket();
try {
authzClient.authorization("marta", "password", "baz").authorize(new AuthorizationRequest(ticket));
fail("Should fail.");
} catch (AuthorizationDeniedException ignore) {
}
// Access Resource B with client scope foo.
request = new PermissionRequest("Resource B");
ticket = authzClient.protection().permission().create(request).getTicket();
try {
authzClient.authorization("marta", "password", "foo").authorize(new AuthorizationRequest(ticket));
fail("Should fail.");
} catch (AuthorizationDeniedException ignore) {
}
}
Aggregations