Java Code Examples for org.keycloak.models.ClientModel#setDirectAccessGrantsEnabled()
The following examples show how to use
org.keycloak.models.ClientModel#setDirectAccessGrantsEnabled() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FineGrainAdminUnitTest.java From keycloak with Apache License 2.0 | 6 votes |
private static void setupTokenExchange(KeycloakSession session) { RealmModel realm = session.realms().getRealmByName("master"); ClientModel client = session.realms().getClientByClientId("kcinit", realm); if (client != null) { return; } ClientModel kcinit = realm.addClient("kcinit"); kcinit.setEnabled(true); kcinit.addRedirectUri("http://localhost:*"); kcinit.setPublicClient(false); kcinit.setSecret("password"); kcinit.setDirectAccessGrantsEnabled(true); // permission for client to client exchange to "target" client ClientModel adminCli = realm.getClientByClientId(ConfigUtil.DEFAULT_CLIENT); AdminPermissionManagement management = AdminPermissions.management(session, realm); management.clients().setPermissionsEnabled(adminCli, true); ClientPolicyRepresentation clientRep = new ClientPolicyRepresentation(); clientRep.setName("to"); clientRep.addClient(kcinit.getId()); ResourceServer server = management.realmResourceServer(); Policy clientPolicy = management.authz().getStoreFactory().getPolicyStore().create(clientRep, server); management.clients().exchangeToPermission(adminCli).addAssociatedPolicy(clientPolicy); }
Example 2
Source File: MigrateTo9_0_0.java From keycloak with Apache License 2.0 | 5 votes |
protected void addAccountConsoleClient(RealmModel realm) { if (realm.getClientByClientId(Constants.ACCOUNT_CONSOLE_CLIENT_ID) == null) { ClientModel client = KeycloakModelUtils.createClient(realm, Constants.ACCOUNT_CONSOLE_CLIENT_ID); client.setName("${client_" + Constants.ACCOUNT_CONSOLE_CLIENT_ID + "}"); client.setEnabled(true); client.setFullScopeAllowed(false); client.setPublicClient(true); client.setDirectAccessGrantsEnabled(false); client.setRootUrl(Constants.AUTH_BASE_URL_PROP); String baseUrl = "/realms/" + realm.getName() + "/account/"; client.setBaseUrl(baseUrl); client.addRedirectUri(baseUrl + "*"); client.setProtocol("openid-connect"); RoleModel role = realm.getClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).getRole(AccountRoles.MANAGE_ACCOUNT); if (role != null) client.addScopeMapping(role); ProtocolMapperModel audienceMapper = new ProtocolMapperModel(); audienceMapper.setName("audience resolve"); audienceMapper.setProtocol("openid-connect"); audienceMapper.setProtocolMapper("oidc-audience-resolve-mapper"); client.addProtocolMapper(audienceMapper); } }
Example 3
Source File: ClientTokenExchangeSAML2Test.java From keycloak with Apache License 2.0 | 5 votes |
private static void addDirectExchanger(KeycloakSession session) { RealmModel realm = session.realms().getRealmByName(TEST); RoleModel exampleRole = realm.addRole("example"); AdminPermissionManagement management = AdminPermissions.management(session, realm); ClientModel directExchanger = realm.addClient("direct-exchanger"); directExchanger.setName("direct-exchanger"); directExchanger.setClientId("direct-exchanger"); directExchanger.setPublicClient(false); directExchanger.setDirectAccessGrantsEnabled(true); directExchanger.setEnabled(true); directExchanger.setSecret("secret"); directExchanger.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); directExchanger.setFullScopeAllowed(false); // permission for client to client exchange to "target" client management.clients().setPermissionsEnabled(realm.getClientByClientId(SAML_SIGNED_TARGET), true); management.clients().setPermissionsEnabled(realm.getClientByClientId(SAML_ENCRYPTED_TARGET), true); management.clients().setPermissionsEnabled(realm.getClientByClientId(SAML_SIGNED_AND_ENCRYPTED_TARGET), true); management.clients().setPermissionsEnabled(realm.getClientByClientId(SAML_UNSIGNED_AND_UNENCRYPTED_TARGET), true); ClientPolicyRepresentation clientImpersonateRep = new ClientPolicyRepresentation(); clientImpersonateRep.setName("clientImpersonatorsDirect"); clientImpersonateRep.addClient(directExchanger.getId()); ResourceServer server = management.realmResourceServer(); Policy clientImpersonatePolicy = management.authz().getStoreFactory().getPolicyStore().create(clientImpersonateRep, server); management.users().setPermissionsEnabled(true); management.users().adminImpersonatingPermission().addAssociatedPolicy(clientImpersonatePolicy); management.users().adminImpersonatingPermission().setDecisionStrategy(DecisionStrategy.AFFIRMATIVE); UserModel impersonatedUser = session.users().addUser(realm, "impersonated-user"); impersonatedUser.setEnabled(true); session.userCredentialManager().updateCredential(realm, impersonatedUser, UserCredentialModel.password("password")); impersonatedUser.grantRole(exampleRole); }
Example 4
Source File: BrokerLinkAndTokenExchangeTest.java From keycloak with Apache License 2.0 | 5 votes |
public static void setupRealm(KeycloakSession session) { RealmModel realm = session.realms().getRealmByName(CHILD_IDP); ClientModel client = realm.getClientByClientId(ClientApp.DEPLOYMENT_NAME); IdentityProviderModel idp = realm.getIdentityProviderByAlias(PARENT_IDP); Assert.assertNotNull(idp); ClientModel directExchanger = realm.addClient("direct-exchanger"); directExchanger.setClientId("direct-exchanger"); directExchanger.setPublicClient(false); directExchanger.setDirectAccessGrantsEnabled(true); directExchanger.setEnabled(true); directExchanger.setSecret("secret"); directExchanger.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); directExchanger.setFullScopeAllowed(false); AdminPermissionManagement management = AdminPermissions.management(session, realm); management.idps().setPermissionsEnabled(idp, true); ClientPolicyRepresentation clientRep = new ClientPolicyRepresentation(); clientRep.setName("toIdp"); clientRep.addClient(client.getId()); clientRep.addClient(directExchanger.getId()); ResourceServer server = management.realmResourceServer(); Policy clientPolicy = management.authz().getStoreFactory().getPolicyStore().create(clientRep, server); management.idps().exchangeToPermission(idp).addAssociatedPolicy(clientPolicy); // permission for user impersonation for a client ClientPolicyRepresentation clientImpersonateRep = new ClientPolicyRepresentation(); clientImpersonateRep.setName("clientImpersonators"); clientImpersonateRep.addClient(directExchanger.getId()); server = management.realmResourceServer(); Policy clientImpersonatePolicy = management.authz().getStoreFactory().getPolicyStore().create(clientImpersonateRep, server); management.users().setPermissionsEnabled(true); management.users().adminImpersonatingPermission().addAssociatedPolicy(clientImpersonatePolicy); management.users().adminImpersonatingPermission().setDecisionStrategy(DecisionStrategy.AFFIRMATIVE); }
Example 5
Source File: SocialLoginTest.java From keycloak with Apache License 2.0 | 5 votes |
public static void setupClientExchangePermissions(KeycloakSession session) { RealmModel realm = session.realms().getRealmByName(REALM); ClientModel client = session.realms().getClientByClientId(EXCHANGE_CLIENT, realm); // lazy init if (client != null) return; client = realm.addClient(EXCHANGE_CLIENT); client.setSecret("secret"); client.setPublicClient(false); client.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); client.setEnabled(true); client.setDirectAccessGrantsEnabled(true); ClientPolicyRepresentation clientPolicyRep = new ClientPolicyRepresentation(); clientPolicyRep.setName("client-policy"); clientPolicyRep.addClient(client.getId()); AdminPermissionManagement management = AdminPermissions.management(session, realm); management.users().setPermissionsEnabled(true); ResourceServer server = management.realmResourceServer(); Policy clientPolicy = management.authz().getStoreFactory().getPolicyStore().create(clientPolicyRep, server); management.users().adminImpersonatingPermission().addAssociatedPolicy(clientPolicy); management.users().adminImpersonatingPermission().setDecisionStrategy(DecisionStrategy.AFFIRMATIVE); for (IdentityProviderModel idp : realm.getIdentityProviders()) { management.idps().setPermissionsEnabled(idp, true); management.idps().exchangeToPermission(idp).addAssociatedPolicy(clientPolicy); } }
Example 6
Source File: OIDCLoginProtocolFactory.java From keycloak with Apache License 2.0 | 5 votes |
@Override public void setupClientDefaults(ClientRepresentation rep, ClientModel newClient) { if (rep.getRootUrl() != null && (rep.getRedirectUris() == null || rep.getRedirectUris().isEmpty())) { String root = rep.getRootUrl(); if (root.endsWith("/")) root = root + "*"; else root = root + "/*"; newClient.addRedirectUri(root); Set<String> origins = new HashSet<String>(); String origin = UriUtils.getOrigin(root); logger.debugv("adding default client origin: {0}" , origin); origins.add(origin); newClient.setWebOrigins(origins); } if (rep.isBearerOnly() == null && rep.isPublicClient() == null) { newClient.setPublicClient(true); } if (rep.isBearerOnly() == null) newClient.setBearerOnly(false); if (rep.getAdminUrl() == null && rep.getRootUrl() != null) { newClient.setManagementUrl(rep.getRootUrl()); } // Backwards compatibility only if (rep.isDirectGrantsOnly() != null) { ServicesLogger.LOGGER.usingDeprecatedDirectGrantsOnly(); newClient.setStandardFlowEnabled(!rep.isDirectGrantsOnly()); newClient.setDirectAccessGrantsEnabled(rep.isDirectGrantsOnly()); } else { if (rep.isStandardFlowEnabled() == null) newClient.setStandardFlowEnabled(true); if (rep.isDirectAccessGrantsEnabled() == null) newClient.setDirectAccessGrantsEnabled(true); } if (rep.isImplicitFlowEnabled() == null) newClient.setImplicitFlowEnabled(false); if (rep.isPublicClient() == null) newClient.setPublicClient(true); if (rep.isFrontchannelLogout() == null) newClient.setFrontchannelLogout(false); }
Example 7
Source File: RealmManager.java From keycloak with Apache License 2.0 | 5 votes |
public void setupAdminCli(RealmModel realm) { ClientModel adminCli = realm.getClientByClientId(Constants.ADMIN_CLI_CLIENT_ID); if (adminCli == null) { adminCli = KeycloakModelUtils.createClient(realm, Constants.ADMIN_CLI_CLIENT_ID); adminCli.setName("${client_" + Constants.ADMIN_CLI_CLIENT_ID + "}"); adminCli.setEnabled(true); adminCli.setAlwaysDisplayInConsole(false); adminCli.setPublicClient(true); adminCli.setFullScopeAllowed(false); adminCli.setStandardFlowEnabled(false); adminCli.setDirectAccessGrantsEnabled(true); adminCli.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); } }
Example 8
Source File: ClientTokenExchangeTest.java From keycloak with Apache License 2.0 | 4 votes |
public static void setupRealm(KeycloakSession session) { addDirectExchanger(session); RealmModel realm = session.realms().getRealmByName(TEST); RoleModel exampleRole = realm.getRole("example"); AdminPermissionManagement management = AdminPermissions.management(session, realm); ClientModel target = realm.getClientByClientId("target"); assertNotNull(target); RoleModel impersonateRole = management.getRealmManagementClient().getRole(ImpersonationConstants.IMPERSONATION_ROLE); ClientModel clientExchanger = realm.addClient("client-exchanger"); clientExchanger.setClientId("client-exchanger"); clientExchanger.setPublicClient(false); clientExchanger.setDirectAccessGrantsEnabled(true); clientExchanger.setEnabled(true); clientExchanger.setSecret("secret"); clientExchanger.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); clientExchanger.setFullScopeAllowed(false); clientExchanger.addScopeMapping(impersonateRole); clientExchanger.addProtocolMapper(UserSessionNoteMapper.createUserSessionNoteMapper(IMPERSONATOR_ID)); clientExchanger.addProtocolMapper(UserSessionNoteMapper.createUserSessionNoteMapper(IMPERSONATOR_USERNAME)); ClientModel illegal = realm.addClient("illegal"); illegal.setClientId("illegal"); illegal.setPublicClient(false); illegal.setDirectAccessGrantsEnabled(true); illegal.setEnabled(true); illegal.setSecret("secret"); illegal.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); illegal.setFullScopeAllowed(false); ClientModel legal = realm.addClient("legal"); legal.setClientId("legal"); legal.setPublicClient(false); legal.setDirectAccessGrantsEnabled(true); legal.setEnabled(true); legal.setSecret("secret"); legal.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); legal.setFullScopeAllowed(false); ClientModel directLegal = realm.addClient("direct-legal"); directLegal.setClientId("direct-legal"); directLegal.setPublicClient(false); directLegal.setDirectAccessGrantsEnabled(true); directLegal.setEnabled(true); directLegal.setSecret("secret"); directLegal.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); directLegal.setFullScopeAllowed(false); ClientModel directPublic = realm.addClient("direct-public"); directPublic.setClientId("direct-public"); directPublic.setPublicClient(true); directPublic.setDirectAccessGrantsEnabled(true); directPublic.setEnabled(true); directPublic.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); directPublic.setFullScopeAllowed(false); ClientModel directNoSecret = realm.addClient("direct-no-secret"); directNoSecret.setClientId("direct-no-secret"); directNoSecret.setPublicClient(false); directNoSecret.setDirectAccessGrantsEnabled(true); directNoSecret.setEnabled(true); directNoSecret.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); directNoSecret.setFullScopeAllowed(false); // permission for client to client exchange to "target" client ClientPolicyRepresentation clientRep = new ClientPolicyRepresentation(); clientRep.setName("to"); clientRep.addClient(clientExchanger.getId()); clientRep.addClient(legal.getId()); clientRep.addClient(directLegal.getId()); ResourceServer server = management.realmResourceServer(); Policy clientPolicy = management.authz().getStoreFactory().getPolicyStore().create(clientRep, server); management.clients().exchangeToPermission(target).addAssociatedPolicy(clientPolicy); // permission for user impersonation for a client ClientPolicyRepresentation clientImpersonateRep = new ClientPolicyRepresentation(); clientImpersonateRep.setName("clientImpersonators"); clientImpersonateRep.addClient(directLegal.getId()); clientImpersonateRep.addClient(directPublic.getId()); clientImpersonateRep.addClient(directNoSecret.getId()); server = management.realmResourceServer(); Policy clientImpersonatePolicy = management.authz().getStoreFactory().getPolicyStore().create(clientImpersonateRep, server); management.users().setPermissionsEnabled(true); management.users().adminImpersonatingPermission().addAssociatedPolicy(clientImpersonatePolicy); management.users().adminImpersonatingPermission().setDecisionStrategy(DecisionStrategy.AFFIRMATIVE); UserModel user = session.users().addUser(realm, "user"); user.setEnabled(true); session.userCredentialManager().updateCredential(realm, user, UserCredentialModel.password("password")); user.grantRole(exampleRole); user.grantRole(impersonateRole); UserModel bad = session.users().addUser(realm, "bad-impersonator"); bad.setEnabled(true); session.userCredentialManager().updateCredential(realm, bad, UserCredentialModel.password("password")); }
Example 9
Source File: ClientTokenExchangeTest.java From keycloak with Apache License 2.0 | 4 votes |
private static void addDirectExchanger(KeycloakSession session) { RealmModel realm = session.realms().getRealmByName(TEST); RoleModel exampleRole = realm.addRole("example"); AdminPermissionManagement management = AdminPermissions.management(session, realm); ClientModel target = realm.addClient("target"); target.setName("target"); target.setClientId("target"); target.setDirectAccessGrantsEnabled(true); target.setEnabled(true); target.setSecret("secret"); target.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); target.setFullScopeAllowed(false); target.addScopeMapping(exampleRole); ClientModel directExchanger = realm.addClient("direct-exchanger"); directExchanger.setName("direct-exchanger"); directExchanger.setClientId("direct-exchanger"); directExchanger.setPublicClient(false); directExchanger.setDirectAccessGrantsEnabled(true); directExchanger.setEnabled(true); directExchanger.setSecret("secret"); directExchanger.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); directExchanger.setFullScopeAllowed(false); // permission for client to client exchange to "target" client management.clients().setPermissionsEnabled(target, true); ClientPolicyRepresentation clientImpersonateRep = new ClientPolicyRepresentation(); clientImpersonateRep.setName("clientImpersonatorsDirect"); clientImpersonateRep.addClient(directExchanger.getId()); ResourceServer server = management.realmResourceServer(); Policy clientImpersonatePolicy = management.authz().getStoreFactory().getPolicyStore().create(clientImpersonateRep, server); management.users().setPermissionsEnabled(true); management.users().adminImpersonatingPermission().addAssociatedPolicy(clientImpersonatePolicy); management.users().adminImpersonatingPermission().setDecisionStrategy(DecisionStrategy.AFFIRMATIVE); UserModel impersonatedUser = session.users().addUser(realm, "impersonated-user"); impersonatedUser.setEnabled(true); session.userCredentialManager().updateCredential(realm, impersonatedUser, UserCredentialModel.password("password")); impersonatedUser.grantRole(exampleRole); }
Example 10
Source File: RealmManager.java From keycloak with Apache License 2.0 | 4 votes |
private void setupAccountManagement(RealmModel realm) { ClientModel accountClient = realm.getClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID); if (accountClient == null) { accountClient = KeycloakModelUtils.createClient(realm, Constants.ACCOUNT_MANAGEMENT_CLIENT_ID); accountClient.setName("${client_" + Constants.ACCOUNT_MANAGEMENT_CLIENT_ID + "}"); accountClient.setEnabled(true); accountClient.setAlwaysDisplayInConsole(false); accountClient.setFullScopeAllowed(false); accountClient.setRootUrl(Constants.AUTH_BASE_URL_PROP); String baseUrl = "/realms/" + realm.getName() + "/account/"; accountClient.setBaseUrl(baseUrl); accountClient.addRedirectUri(baseUrl + "*"); accountClient.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); for (String role : AccountRoles.ALL) { accountClient.addDefaultRole(role); RoleModel roleModel = accountClient.getRole(role); roleModel.setDescription("${role_" + role + "}"); } RoleModel manageAccountLinks = accountClient.addRole(AccountRoles.MANAGE_ACCOUNT_LINKS); manageAccountLinks.setDescription("${role_" + AccountRoles.MANAGE_ACCOUNT_LINKS + "}"); RoleModel manageAccount = accountClient.getRole(AccountRoles.MANAGE_ACCOUNT); manageAccount.addCompositeRole(manageAccountLinks); RoleModel viewAppRole = accountClient.addRole(AccountRoles.VIEW_APPLICATIONS); viewAppRole.setDescription("${role_" + AccountRoles.VIEW_APPLICATIONS + "}"); RoleModel viewConsentRole = accountClient.addRole(AccountRoles.VIEW_CONSENT); viewConsentRole.setDescription("${role_" + AccountRoles.VIEW_CONSENT + "}"); RoleModel manageConsentRole = accountClient.addRole(AccountRoles.MANAGE_CONSENT); manageConsentRole.setDescription("${role_" + AccountRoles.MANAGE_CONSENT + "}"); manageConsentRole.addCompositeRole(viewConsentRole); ClientModel accountConsoleClient = realm.getClientByClientId(Constants.ACCOUNT_CONSOLE_CLIENT_ID); if (accountConsoleClient == null) { accountConsoleClient = KeycloakModelUtils.createClient(realm, Constants.ACCOUNT_CONSOLE_CLIENT_ID); accountConsoleClient.setName("${client_" + Constants.ACCOUNT_CONSOLE_CLIENT_ID + "}"); accountConsoleClient.setEnabled(true); accountConsoleClient.setAlwaysDisplayInConsole(false); accountConsoleClient.setFullScopeAllowed(false); accountConsoleClient.setPublicClient(true); accountConsoleClient.setDirectAccessGrantsEnabled(false); accountConsoleClient.setRootUrl(Constants.AUTH_BASE_URL_PROP); accountConsoleClient.setBaseUrl(baseUrl); accountConsoleClient.addRedirectUri(baseUrl + "*"); accountConsoleClient.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); accountConsoleClient.addScopeMapping(accountClient.getRole(AccountRoles.MANAGE_ACCOUNT)); ProtocolMapperModel audienceMapper = new ProtocolMapperModel(); audienceMapper.setName(OIDCLoginProtocolFactory.AUDIENCE_RESOLVE); audienceMapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); audienceMapper.setProtocolMapper(AudienceResolveProtocolMapper.PROVIDER_ID); accountConsoleClient.addProtocolMapper(audienceMapper); accountConsoleClient.setAttribute(OIDCConfigAttributes.PKCE_CODE_CHALLENGE_METHOD, "S256"); } } }