role description
This commit is contained in:
parent
6fdff819a1
commit
4a40ec71c9
10 changed files with 89 additions and 20 deletions
|
@ -18,7 +18,7 @@ public class RealmRepresentation {
|
||||||
protected boolean cookieLoginAllowed;
|
protected boolean cookieLoginAllowed;
|
||||||
protected String privateKey;
|
protected String privateKey;
|
||||||
protected String publicKey;
|
protected String publicKey;
|
||||||
protected Set<String> roles;
|
protected List<RoleRepresentation> roles;
|
||||||
protected List<RequiredCredentialRepresentation> requiredCredentials;
|
protected List<RequiredCredentialRepresentation> requiredCredentials;
|
||||||
protected List<UserRepresentation> users;
|
protected List<UserRepresentation> users;
|
||||||
protected List<RoleMappingRepresentation> roleMappings;
|
protected List<RoleMappingRepresentation> roleMappings;
|
||||||
|
@ -146,11 +146,11 @@ public class RealmRepresentation {
|
||||||
this.accessCodeLifespan = accessCodeLifespan;
|
this.accessCodeLifespan = accessCodeLifespan;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getRoles() {
|
public List<RoleRepresentation> getRoles() {
|
||||||
return roles;
|
return roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRoles(Set<String> roles) {
|
public void setRoles(List<RoleRepresentation> roles) {
|
||||||
this.roles = roles;
|
this.roles = roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ public class ResourceRepresentation {
|
||||||
protected boolean surrogateAuthRequired;
|
protected boolean surrogateAuthRequired;
|
||||||
protected boolean useRealmMappings;
|
protected boolean useRealmMappings;
|
||||||
protected List<CredentialRepresentation> credentials;
|
protected List<CredentialRepresentation> credentials;
|
||||||
protected Set<String> roles;
|
protected List<RoleRepresentation> roles;
|
||||||
protected List<RoleMappingRepresentation> roleMappings;
|
protected List<RoleMappingRepresentation> roleMappings;
|
||||||
protected List<ScopeMappingRepresentation> scopeMappings;
|
protected List<ScopeMappingRepresentation> scopeMappings;
|
||||||
|
|
||||||
|
@ -44,17 +44,17 @@ public class ResourceRepresentation {
|
||||||
this.surrogateAuthRequired = surrogateAuthRequired;
|
this.surrogateAuthRequired = surrogateAuthRequired;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getRoles() {
|
public List<RoleRepresentation> getRoles() {
|
||||||
return roles;
|
return roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRoles(Set<String> roles) {
|
public void setRoles(List<RoleRepresentation> roles) {
|
||||||
this.roles = roles;
|
this.roles = roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResourceRepresentation role(String role) {
|
public ResourceRepresentation role(String role, String description) {
|
||||||
if (this.roles == null) this.roles = new HashSet<String>();
|
if (this.roles == null) this.roles = new ArrayList<RoleRepresentation>();
|
||||||
this.roles.add(role);
|
this.roles.add(new RoleRepresentation(role, description));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
34
core/src/main/java/org/keycloak/representations/idm/RoleRepresentation.java
Executable file
34
core/src/main/java/org/keycloak/representations/idm/RoleRepresentation.java
Executable file
|
@ -0,0 +1,34 @@
|
||||||
|
package org.keycloak.representations.idm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||||
|
* @version $Revision: 1 $
|
||||||
|
*/
|
||||||
|
public class RoleRepresentation {
|
||||||
|
protected String name;
|
||||||
|
protected String description;
|
||||||
|
|
||||||
|
public RoleRepresentation() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public RoleRepresentation(String name, String description) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,6 +27,10 @@
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"roles" : [
|
||||||
|
{ "name" : "user", "description" : "User privileges" },
|
||||||
|
{ "name" : "admin", "description" : "Administrator privileges" }
|
||||||
|
],
|
||||||
"roleMappings" : [
|
"roleMappings" : [
|
||||||
{
|
{
|
||||||
"username" : "bburke@redhat.com",
|
"username" : "bburke@redhat.com",
|
||||||
|
|
|
@ -5,6 +5,7 @@ import org.keycloak.representations.idm.RealmRepresentation;
|
||||||
import org.keycloak.representations.idm.RequiredCredentialRepresentation;
|
import org.keycloak.representations.idm.RequiredCredentialRepresentation;
|
||||||
import org.keycloak.representations.idm.ResourceRepresentation;
|
import org.keycloak.representations.idm.ResourceRepresentation;
|
||||||
import org.keycloak.representations.idm.RoleMappingRepresentation;
|
import org.keycloak.representations.idm.RoleMappingRepresentation;
|
||||||
|
import org.keycloak.representations.idm.RoleRepresentation;
|
||||||
import org.keycloak.representations.idm.ScopeMappingRepresentation;
|
import org.keycloak.representations.idm.ScopeMappingRepresentation;
|
||||||
import org.keycloak.representations.idm.UserRepresentation;
|
import org.keycloak.representations.idm.UserRepresentation;
|
||||||
import org.keycloak.services.models.RealmModel;
|
import org.keycloak.services.models.RealmModel;
|
||||||
|
@ -23,6 +24,7 @@ import org.picketlink.idm.model.User;
|
||||||
|
|
||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import java.io.Serializable;
|
||||||
import java.security.KeyPair;
|
import java.security.KeyPair;
|
||||||
import java.security.KeyPairGenerator;
|
import java.security.KeyPairGenerator;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
@ -38,6 +40,9 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||||
*/
|
*/
|
||||||
public class RealmManager {
|
public class RealmManager {
|
||||||
private static AtomicLong counter = new AtomicLong(1);
|
private static AtomicLong counter = new AtomicLong(1);
|
||||||
|
public static final String RESOURCE_ROLE = "KEYCLOAK_RESOURCE";
|
||||||
|
public static final String OAUTH_CLIENT_ROLE = "KEYCLOAK_OAUTH_CLIENT";
|
||||||
|
public static final String WILDCARD_ROLE = "*";
|
||||||
|
|
||||||
public static String generateId() {
|
public static String generateId() {
|
||||||
return counter.getAndIncrement() + "-" + System.currentTimeMillis();
|
return counter.getAndIncrement() + "-" + System.currentTimeMillis();
|
||||||
|
@ -71,7 +76,8 @@ public class RealmManager {
|
||||||
SimpleAgent agent = new SimpleAgent(RealmModel.REALM_AGENT_ID);
|
SimpleAgent agent = new SimpleAgent(RealmModel.REALM_AGENT_ID);
|
||||||
idm.add(agent);
|
idm.add(agent);
|
||||||
RealmModel realm = new RealmModel(newRealm, identitySession);
|
RealmModel realm = new RealmModel(newRealm, identitySession);
|
||||||
idm.add(new SimpleRole("*"));
|
idm.add(new SimpleRole(WILDCARD_ROLE));
|
||||||
|
idm.add(new SimpleRole(RESOURCE_ROLE));
|
||||||
return realm;
|
return realm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,8 +151,9 @@ public class RealmManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rep.getRoles() != null) {
|
if (rep.getRoles() != null) {
|
||||||
for (String roleString : rep.getRoles()) {
|
for (RoleRepresentation roleRep : rep.getRoles()) {
|
||||||
SimpleRole role = new SimpleRole(roleString.trim());
|
SimpleRole role = new SimpleRole(roleRep.getName());
|
||||||
|
if (roleRep.getDescription() != null) role.setAttribute(new Attribute<String>("description", roleRep.getDescription()));
|
||||||
newRealm.getIdm().add(role);
|
newRealm.getIdm().add(role);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,6 +193,7 @@ public class RealmManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void createResources(RealmRepresentation rep, RealmModel realm, Map<String, User> userMap) {
|
protected void createResources(RealmRepresentation rep, RealmModel realm, Map<String, User> userMap) {
|
||||||
|
Role loginRole = realm.getIdm().getRole(RealmManager.RESOURCE_ROLE);
|
||||||
for (ResourceRepresentation resourceRep : rep.getResources()) {
|
for (ResourceRepresentation resourceRep : rep.getResources()) {
|
||||||
ResourceModel resource = realm.addResource(resourceRep.getName());
|
ResourceModel resource = realm.addResource(resourceRep.getName());
|
||||||
resource.setManagementUrl(resourceRep.getAdminUrl());
|
resource.setManagementUrl(resourceRep.getAdminUrl());
|
||||||
|
@ -202,11 +210,13 @@ public class RealmManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
userMap.put(resourceUser.getLoginName(), resourceUser);
|
userMap.put(resourceUser.getLoginName(), resourceUser);
|
||||||
|
realm.getIdm().grantRole(resourceUser, loginRole);
|
||||||
|
|
||||||
|
|
||||||
if (resourceRep.getRoles() != null) {
|
if (resourceRep.getRoles() != null) {
|
||||||
for (String roleString : resourceRep.getRoles()) {
|
for (RoleRepresentation roleRep : resourceRep.getRoles()) {
|
||||||
SimpleRole role = new SimpleRole(roleString.trim());
|
SimpleRole role = new SimpleRole(roleRep.getName());
|
||||||
|
if (roleRep.getDescription() != null) role.setAttribute(new Attribute<String>("description", roleRep.getDescription()));
|
||||||
resource.getIdm().add(role);
|
resource.getIdm().add(role);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,8 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Meant to be a per-request object
|
||||||
|
*
|
||||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||||
* @version $Revision: 1 $
|
* @version $Revision: 1 $
|
||||||
*/
|
*/
|
||||||
|
@ -57,6 +59,7 @@ public class RealmModel {
|
||||||
protected IdentitySession identitySession;
|
protected IdentitySession identitySession;
|
||||||
protected volatile transient PublicKey publicKey;
|
protected volatile transient PublicKey publicKey;
|
||||||
protected volatile transient PrivateKey privateKey;
|
protected volatile transient PrivateKey privateKey;
|
||||||
|
protected IdentityManager idm;
|
||||||
|
|
||||||
public RealmModel(Realm realm, IdentitySession session) {
|
public RealmModel(Realm realm, IdentitySession session) {
|
||||||
this.realm = realm;
|
this.realm = realm;
|
||||||
|
@ -65,7 +68,8 @@ public class RealmModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
public IdentityManager getIdm() {
|
public IdentityManager getIdm() {
|
||||||
return identitySession.createIdentityManager(realm);
|
if (idm == null) idm = identitySession.createIdentityManager(realm);
|
||||||
|
return idm;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateRealm() {
|
public void updateRealm() {
|
||||||
|
|
|
@ -25,6 +25,7 @@ public class ResourceModel {
|
||||||
protected ResourceRelationship agent;
|
protected ResourceRelationship agent;
|
||||||
protected RealmModel realm;
|
protected RealmModel realm;
|
||||||
protected IdentitySession identitySession;
|
protected IdentitySession identitySession;
|
||||||
|
protected IdentityManager idm;
|
||||||
|
|
||||||
public ResourceModel(Tier tier, ResourceRelationship agent, RealmModel realm, IdentitySession session) {
|
public ResourceModel(Tier tier, ResourceRelationship agent, RealmModel realm, IdentitySession session) {
|
||||||
this.tier = tier;
|
this.tier = tier;
|
||||||
|
@ -34,7 +35,8 @@ public class ResourceModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
public IdentityManager getIdm() {
|
public IdentityManager getIdm() {
|
||||||
return identitySession.createIdentityManager(tier);
|
if (idm == null) idm = identitySession.createIdentityManager(tier);
|
||||||
|
return idm;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateResource() {
|
public void updateResource() {
|
||||||
|
|
|
@ -13,12 +13,14 @@ import org.keycloak.representations.SkeletonKeyToken;
|
||||||
import org.keycloak.services.JspRequestParameters;
|
import org.keycloak.services.JspRequestParameters;
|
||||||
import org.keycloak.services.managers.AccessCodeEntry;
|
import org.keycloak.services.managers.AccessCodeEntry;
|
||||||
import org.keycloak.services.managers.AuthenticationManager;
|
import org.keycloak.services.managers.AuthenticationManager;
|
||||||
|
import org.keycloak.services.managers.RealmManager;
|
||||||
import org.keycloak.services.managers.ResourceAdminManager;
|
import org.keycloak.services.managers.ResourceAdminManager;
|
||||||
import org.keycloak.services.managers.TokenManager;
|
import org.keycloak.services.managers.TokenManager;
|
||||||
import org.keycloak.services.models.RealmModel;
|
import org.keycloak.services.models.RealmModel;
|
||||||
import org.keycloak.services.models.RequiredCredentialModel;
|
import org.keycloak.services.models.RequiredCredentialModel;
|
||||||
import org.keycloak.services.models.ResourceModel;
|
import org.keycloak.services.models.ResourceModel;
|
||||||
import org.picketlink.idm.IdentitySession;
|
import org.picketlink.idm.IdentitySession;
|
||||||
|
import org.picketlink.idm.model.Role;
|
||||||
import org.picketlink.idm.model.User;
|
import org.picketlink.idm.model.User;
|
||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
|
@ -389,12 +391,19 @@ public class TokenService {
|
||||||
identitySession.close();
|
identitySession.close();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
Role resourceRole = realm.getIdm().getRole(RealmManager.RESOURCE_ROLE);
|
||||||
|
Role oauthClientRole = realm.getIdm().getRole(RealmManager.OAUTH_CLIENT_ROLE);
|
||||||
|
if (!realm.getIdm().hasRole(client, resourceRole) && !realm.getIdm().hasRole(client, oauthClientRole)) {
|
||||||
|
securityFailureForward("Login requester not allowed to request login.");
|
||||||
|
identitySession.close();
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
User user = authManager.authenticateIdentityCookie(realm, uriInfo, headers);
|
User user = authManager.authenticateIdentityCookie(realm, uriInfo, headers);
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
return redirectAccessCode(scopeParam, state, redirect, client, user);
|
return redirectAccessCode(scopeParam, state, redirect, client, user);
|
||||||
}
|
}
|
||||||
// todo make sure client is allowed to request a login
|
|
||||||
|
|
||||||
forwardToLoginForm(redirect, clientId, scopeParam, state);
|
forwardToLoginForm(redirect, clientId, scopeParam, state);
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -166,7 +166,7 @@ public class AdapterTest {
|
||||||
idm.add(new SimpleRole("admin"));
|
idm.add(new SimpleRole("admin"));
|
||||||
idm.add(new SimpleRole("user"));
|
idm.add(new SimpleRole("user"));
|
||||||
List<Role> roles = realmModel.getRoles();
|
List<Role> roles = realmModel.getRoles();
|
||||||
Assert.assertEquals(3, roles.size());
|
Assert.assertEquals(4, roles.size());
|
||||||
SimpleUser user = new SimpleUser("bburke");
|
SimpleUser user = new SimpleUser("bburke");
|
||||||
idm.add(user);
|
idm.add(user);
|
||||||
Role role = idm.getRole("user");
|
Role role = idm.getRole("user");
|
||||||
|
|
|
@ -62,7 +62,10 @@
|
||||||
"resources" : [
|
"resources" : [
|
||||||
{
|
{
|
||||||
"name" : "Application",
|
"name" : "Application",
|
||||||
"roles" : ["admin", "user"],
|
"roles" : [
|
||||||
|
{ "name" : "admin" },
|
||||||
|
{ "name" : "user" }
|
||||||
|
],
|
||||||
"roleMappings" : [
|
"roleMappings" : [
|
||||||
{
|
{
|
||||||
"username" : "wburke",
|
"username" : "wburke",
|
||||||
|
@ -82,7 +85,10 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name" : "OtherApp",
|
"name" : "OtherApp",
|
||||||
"roles" : ["admin", "user"],
|
"roles" : [
|
||||||
|
{ "name" : "admin" },
|
||||||
|
{ "name" : "user" }
|
||||||
|
],
|
||||||
"roleMappings" : [
|
"roleMappings" : [
|
||||||
{
|
{
|
||||||
"username" : "wburke",
|
"username" : "wburke",
|
||||||
|
|
Loading…
Reference in a new issue