Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.keycloak.adapters.wildfly;
  
  
 import java.util.Set;

Author(s):
Bill Burke
Version:
$Revision: 1 $
 
 public class SecurityInfoHelper {
     public static void propagateSessionInfo(KeycloakAccount account) {
         Subject subject = new Subject();
         Set<Principalprincipals = subject.getPrincipals();
         principals.add(account.getPrincipal());
         Group[] roleSets = getRoleSets(account.getRoles());
         for (int g = 0; g < roleSets.lengthg++) {
             Group group = roleSets[g];
             String name = group.getName();
             Group subjectGroup = createGroup(nameprincipals);
             if (subjectGroup instanceof NestableGroup) {
                 /* A NestableGroup only allows Groups to be added to it so we
                 need to add a SimpleGroup to subjectRoles to contain the roles
                 */
                 SimpleGroup tmp = new SimpleGroup("Roles");
                 subjectGroup.addMember(tmp);
                 subjectGroup = tmp;
             }
             // Copy the group members to the Subject group
             Enumeration<? extends Principalmembers = group.members();
             while (members.hasMoreElements()) {
                 Principal role = (Principalmembers.nextElement();
                 subjectGroup.addMember(role);
             }
         }
         // add the CallerPrincipal group if none has been added in getRoleSets
         Group callerGroup = new SimpleGroup(.);
         callerGroup.addMember(account.getPrincipal());
         principals.add(callerGroup);
         org.jboss.security.SecurityContext sc = SecurityContextAssociation.getSecurityContext();
         Principal userPrincipal = getPrincipal(subject);
         sc.getUtil().createSubjectInfo(userPrincipalaccountsubject);
     }

    
Get the Principal given the authenticated Subject. Currently the first subject that is not of type Group is considered or the single subject inside the CallerPrincipal group.

Parameters:
subject
Returns:
the authenticated subject
 
     protected static Principal getPrincipal(Subject subject) {
         Principal principal = null;
         Principal callerPrincipal = null;
         if (subject != null) {
             Set<Principalprincipals = subject.getPrincipals();
             if (principals != null && !principals.isEmpty()) {
                 for (Principal p : principals) {
                     if (!(p instanceof Group) && principal == null) {
                         principal = p;
                     }
                     if (p instanceof Group) {
                         Group g = Group.class.cast(p);
                         if (g.getName().equals(.) && callerPrincipal == null) {
                             Enumeration<? extends Principale = g.members();
                             if (e.hasMoreElements())
                                 callerPrincipal = e.nextElement();
                         }
                     }
                 }
             }
         }
         return callerPrincipal == null ? principal : callerPrincipal;
     }
 
     protected static Group createGroup(String nameSet<Principalprincipals) {
         Group roles = null;
         Iterator<Principaliter = principals.iterator();
         while (iter.hasNext()) {
             Object next = iter.next();
             if ((next instanceof Group) == false)
                 continue;
             Group grp = (Groupnext;
             if (grp.getName().equals(name)) {
                 roles = grp;
                 break;
             }
         }
        // If we did not find a group create one
        if (roles == null) {
            roles = new SimpleGroup(name);
            principals.add(roles);
        }
        return roles;
    }
    protected static Group[] getRoleSets(Collection<StringroleSet) {
        SimpleGroup roles = new SimpleGroup("Roles");
        Group[] roleSets = {roles};
        for (String role : roleSet) {
            roles.addMember(new SimplePrincipal(role));
        }
        return roleSets;
    }
New to GrepCode? Check out our FAQ X