use of org.keycloak.representations.idm.ProtocolMapperRepresentation in project keycloak by keycloak.
the class OIDCProtocolMappersTest method testUserRoleToAttributeMappersWithFullScopeDisabled.
/**
* KEYCLOAK-5259
* @throws Exception
*/
@Test
public void testUserRoleToAttributeMappersWithFullScopeDisabled() throws Exception {
// Add mapper for realm roles
ProtocolMapperRepresentation realmMapper = ProtocolMapperUtil.createUserRealmRoleMappingMapper("pref.", "Realm roles mapper", "roles-custom.realm", true, true, true);
ProtocolMapperRepresentation clientMapper = ProtocolMapperUtil.createUserClientRoleMappingMapper("test-app", null, "Client roles mapper", "roles-custom.test-app", true, true, true);
ClientResource client = ApiUtil.findClientResourceByClientId(adminClient.realm("test"), "test-app");
// Disable full-scope-allowed
ClientRepresentation rep = client.toRepresentation();
rep.setFullScopeAllowed(false);
client.update(rep);
ProtocolMappersResource protocolMappers = ApiUtil.findClientResourceByClientId(adminClient.realm("test"), "test-app").getProtocolMappers();
protocolMappers.createMapper(Arrays.asList(realmMapper, clientMapper));
// Login user
OAuthClient.AccessTokenResponse response = browserLogin("password", "test-user@localhost", "password");
IDToken idToken = oauth.verifyIDToken(response.getIdToken());
// Verify attribute is filled
Map<String, Object> roleMappings = (Map<String, Object>) idToken.getOtherClaims().get("roles-custom");
Assert.assertThat(roleMappings.keySet(), containsInAnyOrder("realm", "test-app"));
Assert.assertThat(roleMappings.get("realm"), CoreMatchers.instanceOf(List.class));
Assert.assertThat(roleMappings.get("test-app"), CoreMatchers.instanceOf(List.class));
List<String> realmRoleMappings = (List<String>) roleMappings.get("realm");
List<String> testAppMappings = (List<String>) roleMappings.get("test-app");
assertRoles(realmRoleMappings, // from direct assignment in user definition
"pref.user");
assertRoles(testAppMappings, // from direct assignment in user definition
"customer-user");
// Revert
deleteMappers(protocolMappers);
rep = client.toRepresentation();
rep.setFullScopeAllowed(true);
client.update(rep);
}
use of org.keycloak.representations.idm.ProtocolMapperRepresentation in project keycloak by keycloak.
the class RepresentationToModel method createClientScope.
public static ClientScopeModel createClientScope(KeycloakSession session, RealmModel realm, ClientScopeRepresentation resourceRep) {
logger.debugv("Create client scope: {0}", resourceRep.getName());
ClientScopeModel clientScope = resourceRep.getId() != null ? realm.addClientScope(resourceRep.getId(), resourceRep.getName()) : realm.addClientScope(resourceRep.getName());
if (resourceRep.getName() != null)
clientScope.setName(resourceRep.getName());
if (resourceRep.getDescription() != null)
clientScope.setDescription(resourceRep.getDescription());
if (resourceRep.getProtocol() != null)
clientScope.setProtocol(resourceRep.getProtocol());
if (resourceRep.getProtocolMappers() != null) {
// first, remove all default/built in mappers
clientScope.getProtocolMappersStream().collect(Collectors.toList()).forEach(clientScope::removeProtocolMapper);
for (ProtocolMapperRepresentation mapper : resourceRep.getProtocolMappers()) {
clientScope.addProtocolMapper(toModel(mapper));
}
MigrationUtils.updateProtocolMappers(clientScope);
}
if (resourceRep.getAttributes() != null) {
for (Map.Entry<String, String> entry : resourceRep.getAttributes().entrySet()) {
clientScope.setAttribute(entry.getKey(), entry.getValue());
}
}
return clientScope;
}
use of org.keycloak.representations.idm.ProtocolMapperRepresentation in project keycloak by keycloak.
the class EntitlementAPITest method testProcessMappersForTargetAudience.
@Test
public void testProcessMappersForTargetAudience() throws Exception {
ClientResource publicClient = getClient(getRealm(), PUBLIC_TEST_CLIENT);
ProtocolMapperRepresentation customClaimMapper = new ProtocolMapperRepresentation();
customClaimMapper.setName("custom_claim");
customClaimMapper.setProtocolMapper(HardcodedClaim.PROVIDER_ID);
customClaimMapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
Map<String, String> config = new HashMap<>();
config.put(OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME, "custom_claim");
config.put(HardcodedClaim.CLAIM_VALUE, PUBLIC_TEST_CLIENT);
config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ACCESS_TOKEN, "true");
customClaimMapper.setConfig(config);
publicClient.getProtocolMappers().createMapper(customClaimMapper);
ClientResource client = getClient(getRealm(), RESOURCE_SERVER_TEST);
config.put(HardcodedClaim.CLAIM_VALUE, RESOURCE_SERVER_TEST);
client.getProtocolMappers().createMapper(customClaimMapper);
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)) {
resource = response.readEntity(ResourceRepresentation.class);
}
ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
permission.setName("View Sensor");
permission.addResource(resource.getName());
permission.addPolicy(policy.getName());
authorization.permissions().resource().create(permission).close();
oauth.realm("authz-test");
oauth.clientId(PUBLIC_TEST_CLIENT);
oauth.doLogin("marta", "password");
// Token request
String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
OAuthClient.AccessTokenResponse response = oauth.doAccessTokenRequest(code, null);
AccessToken token = toAccessToken(response.getAccessToken());
assertEquals(PUBLIC_TEST_CLIENT, token.getOtherClaims().get("custom_claim"));
AuthorizationRequest request = new AuthorizationRequest();
request.addPermission("Sensors");
AuthorizationResponse authorizationResponse = getAuthzClient(AUTHZ_CLIENT_CONFIG).authorization(response.getAccessToken()).authorize(request);
token = toAccessToken(authorizationResponse.getToken());
assertEquals(RESOURCE_SERVER_TEST, token.getOtherClaims().get("custom_claim"));
assertEquals(PUBLIC_TEST_CLIENT, token.getIssuedFor());
authorizationResponse = getAuthzClient(AUTHZ_CLIENT_CONFIG).authorization(response.getAccessToken()).authorize(request);
token = toAccessToken(authorizationResponse.getToken());
assertEquals(RESOURCE_SERVER_TEST, token.getOtherClaims().get("custom_claim"));
assertEquals(PUBLIC_TEST_CLIENT, token.getIssuedFor());
}
use of org.keycloak.representations.idm.ProtocolMapperRepresentation in project keycloak by keycloak.
the class ClientMappersSAMLTest method testUserSessionNote.
@Test
public void testUserSessionNote() {
// create
clientMappersPage.mapperTable().createMapper();
setInitialValues("user session note");
createClientMappersPage.form().setMapperType(USER_SESSION_NOTE);
createClientMappersPage.form().save();
assertAlertSuccess();
// check
ProtocolMapperRepresentation found = findClientMapperByName(id, "user session note");
assertNotNull(found);
assertEquals("saml-user-session-note-mapper", found.getProtocolMapper());
}
use of org.keycloak.representations.idm.ProtocolMapperRepresentation in project keycloak by keycloak.
the class ClientMappersSAMLTest method testHardcodedRole.
@Test
public void testHardcodedRole() {
// create
clientMappersPage.mapperTable().createMapper();
setInitialValues("hardcoded role");
createClientMappersPage.form().setMapperType(HARDCODED_ROLE_SAML);
createClientMappersPage.form().selectRole(REALM_ROLE, "offline_access", null);
createClientMappersPage.form().save();
assertAlertSuccess();
// check
ProtocolMapperRepresentation found = findClientMapperByName(id, "hardcoded role");
assertNotNull(found);
assertEquals("saml-hardcode-role-mapper", found.getProtocolMapper());
Map<String, String> config = found.getConfig();
assertEquals(1, config.size());
assertEquals("offline_access", config.get("role"));
}
Aggregations