All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hyperfoil.tools.horreum.svc.CachedSecurityIdentity Maven / Gradle / Ivy

There is a newer version: 0.15.3
Show newest version
package io.hyperfoil.tools.horreum.svc;

import java.security.Permission;
import java.security.Principal;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import io.quarkus.security.credential.Credential;
import io.quarkus.security.identity.SecurityIdentity;
import io.smallrye.mutiny.Uni;

public class CachedSecurityIdentity implements SecurityIdentity {
    public static final CachedSecurityIdentity ANONYMOUS = new CachedSecurityIdentity(null, Collections.emptySet(),
            Collections.emptySet(), Collections.emptyMap());
    private final Principal principal;
    private final Set roles;
    private final Set credentials;
    private final Map attributes;

    public CachedSecurityIdentity(Principal principal, Set roles, Set credentials,
            Map attributes) {
        this.principal = principal;
        this.roles = roles;
        this.credentials = credentials;
        this.attributes = attributes;
    }

    public CachedSecurityIdentity(SecurityIdentity other) {
        this(other.getPrincipal(), other.getRoles(), other.getCredentials(), other.getAttributes());
    }

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

    @Override
    public boolean isAnonymous() {
        return false;
    }

    @Override
    public Set getRoles() {
        return roles;
    }

    @Override
    public boolean hasRole(String s) {
        return roles.contains(s);
    }

    @Override
    public  T getCredential(Class type) {
        return credentials.stream().filter(type::isInstance).map(type::cast).findAny().orElse(null);
    }

    @Override
    public Set getCredentials() {
        return credentials;
    }

    @SuppressWarnings("unchecked")
    @Override
    public  T getAttribute(String s) {
        return (T) attributes.get(s);
    }

    @Override
    public Map getAttributes() {
        return attributes;
    }

    @Override
    public Uni checkPermission(Permission permission) {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy