Search in sources :

Example 1 with KeycloakAccount

use of org.keycloak.adapters.spi.KeycloakAccount in project keycloak by keycloak.

the class JBossWebPrincipalFactory method createPrincipal.

@Override
public GenericPrincipal createPrincipal(Realm realm, final Principal identity, final Set<String> roleSet) {
    KeycloakAccount account = new KeycloakAccount() {

        @Override
        public Principal getPrincipal() {
            return identity;
        }

        @Override
        public Set<String> getRoles() {
            return roleSet;
        }
    };
    Subject subject = new Subject();
    Set<Principal> principals = subject.getPrincipals();
    principals.add(identity);
    Group[] roleSets = getRoleSets(roleSet);
    for (int g = 0; g < roleSets.length; g++) {
        Group group = roleSets[g];
        String name = group.getName();
        Group subjectGroup = createGroup(name, principals);
        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 Principal> members = group.members();
        while (members.hasMoreElements()) {
            Principal role = (Principal) members.nextElement();
            subjectGroup.addMember(role);
        }
    }
    // add the CallerPrincipal group if none has been added in getRoleSets
    Group callerGroup = new SimpleGroup(SecurityConstants.CALLER_PRINCIPAL_GROUP);
    callerGroup.addMember(identity);
    principals.add(callerGroup);
    SecurityContext sc = SecurityContextAssociation.getSecurityContext();
    Principal userPrincipal = getPrincipal(subject);
    sc.getUtil().createSubjectInfo(userPrincipal, account, subject);
    List<String> rolesAsStringList = new ArrayList<>(roleSet);
    try {
        return (GenericPrincipal) jbossWebPrincipalConstructor.newInstance(realm, userPrincipal.getName(), null, rolesAsStringList, userPrincipal, null, account, null, subject);
    } catch (Throwable t) {
        throw new RuntimeException("Failed to create JBossGenericPrincipal", t);
    }
}
Also used : SimpleGroup(org.jboss.security.SimpleGroup) NestableGroup(org.jboss.security.NestableGroup) Group(java.security.acl.Group) ArrayList(java.util.ArrayList) KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount) SimpleGroup(org.jboss.security.SimpleGroup) Subject(javax.security.auth.Subject) NestableGroup(org.jboss.security.NestableGroup) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) JBossGenericPrincipal(org.jboss.as.web.security.JBossGenericPrincipal) SecurityContext(org.jboss.security.SecurityContext) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) JBossGenericPrincipal(org.jboss.as.web.security.JBossGenericPrincipal) Principal(java.security.Principal) SimplePrincipal(org.jboss.security.SimplePrincipal)

Example 2 with KeycloakAccount

use of org.keycloak.adapters.spi.KeycloakAccount in project keycloak by keycloak.

the class KeycloakLoginModule method login.

@SuppressWarnings("unchecked")
@Override
public boolean login() throws LoginException {
    log.debug("KeycloakLoginModule.login()");
    if (super.login()) {
        log.debug("super.login()==true");
        return true;
    }
    Object credential = getCredential();
    if (credential != null && (credential instanceof KeycloakAccount)) {
        log.debug("Found Account");
        KeycloakAccount account = (KeycloakAccount) credential;
        roleSet = account.getRoles();
        identity = account.getPrincipal();
        sharedState.put("javax.security.auth.login.name", identity);
        sharedState.put("javax.security.auth.login.password", credential);
        loginOk = true;
        return true;
    }
    // username and password has been supplied to a web auth.
    return false;
}
Also used : KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount)

Example 3 with KeycloakAccount

use of org.keycloak.adapters.spi.KeycloakAccount in project keycloak by keycloak.

the class KeycloakAuthenticationProviderTest method setUp.

@Before
public void setUp() throws Exception {
    Principal principal = mock(Principal.class);
    RefreshableKeycloakSecurityContext securityContext = mock(RefreshableKeycloakSecurityContext.class);
    KeycloakAccount account = new SimpleKeycloakAccount(principal, roles, securityContext);
    token = new KeycloakAuthenticationToken(account, false);
    interactiveToken = new KeycloakAuthenticationToken(account, true);
}
Also used : KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) Principal(java.security.Principal) Before(org.junit.Before)

Example 4 with KeycloakAccount

use of org.keycloak.adapters.spi.KeycloakAccount in project keycloak by keycloak.

the class SimpleHttpFacadeTest method setup.

@Before
public void setup() {
    SecurityContext springSecurityContext = SecurityContextHolder.createEmptyContext();
    SecurityContextHolder.setContext(springSecurityContext);
    Set<String> roles = Sets.newSet("user");
    Principal principal = mock(Principal.class);
    RefreshableKeycloakSecurityContext keycloakSecurityContext = mock(RefreshableKeycloakSecurityContext.class);
    KeycloakAccount account = new SimpleKeycloakAccount(principal, roles, keycloakSecurityContext);
    KeycloakAuthenticationToken token = new KeycloakAuthenticationToken(account, false);
    springSecurityContext.setAuthentication(token);
}
Also used : KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) SecurityContext(org.springframework.security.core.context.SecurityContext) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) Principal(java.security.Principal) Before(org.junit.Before)

Example 5 with KeycloakAccount

use of org.keycloak.adapters.spi.KeycloakAccount in project keycloak by keycloak.

the class OIDCFilterSessionStore method buildWrapper.

public HttpServletRequestWrapper buildWrapper() {
    HttpSession session = request.getSession(false);
    KeycloakAccount account = null;
    if (session != null) {
        account = (KeycloakAccount) session.getAttribute(KeycloakAccount.class.getName());
        if (account == null) {
            account = (KeycloakAccount) request.getAttribute(KeycloakAccount.class.getName());
        }
    }
    if (account == null) {
        account = (KeycloakAccount) request.getAttribute(KeycloakAccount.class.getName());
    }
    return buildWrapper(session, account);
}
Also used : HttpSession(javax.servlet.http.HttpSession) KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount) OidcKeycloakAccount(org.keycloak.adapters.OidcKeycloakAccount)

Aggregations

KeycloakAccount (org.keycloak.adapters.spi.KeycloakAccount)7 Principal (java.security.Principal)3 RefreshableKeycloakSecurityContext (org.keycloak.adapters.RefreshableKeycloakSecurityContext)3 SimpleKeycloakAccount (org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount)3 KeycloakAuthenticationToken (org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken)3 HttpSession (javax.servlet.http.HttpSession)2 Before (org.junit.Before)2 OidcKeycloakAccount (org.keycloak.adapters.OidcKeycloakAccount)2 SecurityContext (org.springframework.security.core.context.SecurityContext)2 Group (java.security.acl.Group)1 ArrayList (java.util.ArrayList)1 Subject (javax.security.auth.Subject)1 GenericPrincipal (org.apache.catalina.realm.GenericPrincipal)1 JBossGenericPrincipal (org.jboss.as.web.security.JBossGenericPrincipal)1 NestableGroup (org.jboss.security.NestableGroup)1 SecurityContext (org.jboss.security.SecurityContext)1 SimpleGroup (org.jboss.security.SimpleGroup)1 SimplePrincipal (org.jboss.security.SimplePrincipal)1 KeycloakSecurityContext (org.keycloak.KeycloakSecurityContext)1 SamlSession (org.keycloak.adapters.saml.SamlSession)1