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

io.prestosql.spi.security.ConnectorIdentity Maven / Gradle / Ivy

There is a newer version: 350
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.prestosql.spi.security;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static java.util.Objects.requireNonNull;

public class ConnectorIdentity
{
    private final String user;
    private final Set groups;
    private final Optional principal;
    private final Optional role;
    private final Map extraCredentials;

    @Deprecated
    public ConnectorIdentity(String user, Optional principal, Optional role)
    {
        this(user, principal, role, emptyMap());
    }

    @Deprecated
    public ConnectorIdentity(String user, Optional principal, Optional role, Map extraCredentials)
    {
        this(user, emptySet(), principal, role, extraCredentials);
    }

    private ConnectorIdentity(
            String user,
            Set groups,
            Optional principal,
            Optional role,
            Map extraCredentials)
    {
        this.user = requireNonNull(user, "user is null");
        this.groups = Set.copyOf(requireNonNull(groups, "groups is null"));
        this.principal = requireNonNull(principal, "principal is null");
        this.role = requireNonNull(role, "role is null");
        this.extraCredentials = Map.copyOf(requireNonNull(extraCredentials, "extraCredentials is null"));
    }

    public String getUser()
    {
        return user;
    }

    public Set getGroups()
    {
        return groups;
    }

    public Optional getPrincipal()
    {
        return principal;
    }

    public Optional getRole()
    {
        return role;
    }

    public Map getExtraCredentials()
    {
        return extraCredentials;
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder("ConnectorIdentity{");
        sb.append("user='").append(user).append('\'');
        sb.append(", groups=").append(groups);
        principal.ifPresent(principal -> sb.append(", principal=").append(principal));
        role.ifPresent(role -> sb.append(", role=").append(role));
        sb.append(", extraCredentials=").append(extraCredentials.keySet());
        sb.append('}');
        return sb.toString();
    }

    public static ConnectorIdentity ofUser(String user)
    {
        return new Builder(user).build();
    }

    public static Builder forUser(String user)
    {
        return new Builder(user);
    }

    public static class Builder
    {
        private final String user;
        private Set groups = emptySet();
        private Optional principal = Optional.empty();
        private Optional role = Optional.empty();
        private Map extraCredentials = new HashMap<>();

        private Builder(String user)
        {
            this.user = requireNonNull(user, "user is null");
        }

        public Builder withGroups(Set groups)
        {
            this.groups = Set.copyOf(requireNonNull(groups, "groups is null"));
            return this;
        }

        public Builder withPrincipal(Principal principal)
        {
            return withPrincipal(Optional.of(requireNonNull(principal, "principal is null")));
        }

        public Builder withPrincipal(Optional principal)
        {
            this.principal = requireNonNull(principal, "principal is null");
            return this;
        }

        public Builder withRole(SelectedRole role)
        {
            return withRole(Optional.of(requireNonNull(role, "role is null")));
        }

        public Builder withRole(Optional role)
        {
            this.role = requireNonNull(role, "role is null");
            return this;
        }

        public Builder withExtraCredentials(Map extraCredentials)
        {
            this.extraCredentials = new HashMap<>(requireNonNull(extraCredentials, "extraCredentials is null"));
            return this;
        }

        public ConnectorIdentity build()
        {
            return new ConnectorIdentity(user, groups, principal, role, extraCredentials);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy