Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ELY-2413] Re-authentication after reboot, even though HttpSession are persisted #1747

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import static org.wildfly.common.Assert.checkNotNullParam;
import static org.wildfly.common.Assert.checkNotEmptyParam;

import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.TreeSet;
import java.util.function.Consumer;

import org.wildfly.common.Assert;
Expand Down Expand Up @@ -130,6 +132,30 @@ public boolean isEmpty() {
};
}

/**
* Returns a set (immutable) containing roles from a roles collection.
*
* @param roles collection (not {@code null})
* @return the set of role names (must not be {@code null})
*/
static Set<String> toSet(Roles roles) {
Assert.checkNotNullParam("roles", roles);
Iterator<String> iterator = roles.iterator();
if (!iterator.hasNext()) {
return Collections.emptySet();
}
String role = iterator.next();
if (!iterator.hasNext()) {
return Collections.singleton(role);
}
Set<String> result = new TreeSet<>();
result.add(role);
while (iterator.hasNext()) {
result.add(iterator.next());
}
return Collections.unmodifiableSet(result);
}

/**
* Construct a role set consisting of a single role.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@

import java.io.Serializable;
import java.security.Principal;
import java.util.Collections;
import java.util.Set;

import org.wildfly.security.auth.server.SecurityIdentity;
import org.wildfly.security.authz.Roles;

/**
* Represents a cached identity, managed by an {@link IdentityCache}.
Expand All @@ -41,6 +44,7 @@ public final class CachedIdentity implements Serializable {
private final boolean programmatic;
private final String name;
private final transient SecurityIdentity securityIdentity;
private final Set<String> roles;

/**
* Creates a new instance based on the given <code>mechanismName</code> and <code>securityIdentity</code>.
Expand All @@ -64,11 +68,36 @@ public CachedIdentity(String mechanismName, boolean programmatic, Principal prin
this(mechanismName, programmatic, null, principal);
}

/**
* Creates a new instance based on the given <code>mechanismName</code> and <code>principal</code>.
*
* @param mechanismName the name of the authentication mechanism used to authenticate/authorize the identity
* @param programmatic indicates if this identity was created as a result of programmatic authentication
* @param principal the principal of this cached identity
* @param roles the roles assigned to this cached identity
*/
public CachedIdentity(String mechanismName, boolean programmatic, Principal principal, Set<String> roles) {
this(mechanismName, programmatic, null, principal, roles);
}

private CachedIdentity(String mechanismName, boolean programmatic, SecurityIdentity securityIdentity, Principal principal) {
this.mechanismName = checkNotNullParam("mechanismName", mechanismName);
this.programmatic = programmatic;
this.name = checkNotNullParam("name", checkNotNullParam("principal", principal).getName());
this.securityIdentity = securityIdentity;
if (securityIdentity != null && securityIdentity.getPrincipal() != null) {
this.roles = Roles.toSet(securityIdentity.getRoles());
} else {
this.roles = Collections.emptySet();
}
}

private CachedIdentity(String mechanismName, boolean programmatic, SecurityIdentity securityIdentity, Principal principal, Set<String> roles) {
this.mechanismName = checkNotNullParam("mechanismName", mechanismName);
this.programmatic = programmatic;
this.name = checkNotNullParam("name", checkNotNullParam("principal", principal).getName());
this.securityIdentity = securityIdentity;
this.roles = roles;
}

/**
Expand Down Expand Up @@ -107,6 +136,19 @@ public boolean isProgrammatic() {
return programmatic;
}

/**
* Returns the roles associated with the cached identity.
*
* @return the roles associated with the cached identity.
*/
public Set<String> getRoles() {
if (this.securityIdentity != null) {
return Roles.toSet(this.securityIdentity.getRoles());
} else {
return this.roles;
}
}

@Override
public String toString() {
return "CachedIdentity{" + mechanismName + ", '" + name + "', " + securityIdentity + ", " + programmatic + "}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.wildfly.security.auth.callback.EvidenceVerifyCallback;
import org.wildfly.security.auth.callback.IdentityCredentialCallback;
import org.wildfly.security.auth.server.SecurityIdentity;
import org.wildfly.security.authz.Roles;
import org.wildfly.security.credential.Credential;
import org.wildfly.security.credential.PasswordCredential;
import org.wildfly.security.evidence.PasswordGuessEvidence;
Expand Down Expand Up @@ -112,6 +113,10 @@ protected SecurityIdentity mockSecurityIdentity(Principal p) {
public Principal getPrincipal() {
return p;
}
@Mock
public Roles getRoles() {
return Roles.NONE;
}
}.getMockInstance();
}

Expand Down