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

io.micronaut.security.authentication.Authentication Maven / Gradle / Ivy

There is a newer version: 4.11.1
Show newest version
/*
 * Copyright 2017-2023 original authors
 *
 * 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
 *
 * https://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.micronaut.security.authentication;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import java.io.Serializable;
import java.security.Principal;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/**
 * Represents the state of an authentication.
 *
 * @author James Kleeh
 * @since 1.0
 */
@JsonDeserialize(as = ClientAuthentication.class)
public interface Authentication extends Principal, Serializable {

    /**
     * In order to correctly implement the {@link Serializable} specification, this map
     * should be {@literal Map}, however that would place a burden on
     * those not requiring serialization, forcing their values to conform to that spec.
     *
     * This is left intentionally as Object in order to meet both use cases and those
     * requiring serialization must ensure all values in the map implement {@link Serializable}.
     *
     * @return Any additional attributes in the authentication
     */
    @NonNull
    Map getAttributes();

    /**
     * @return Any roles associated with the authentication
     */
    @NonNull
    default Collection getRoles() {
        return Collections.emptyList();
    }

    /**
     * Builds an {@link Authentication} instance for the user.
     * @param username User's name
     * @return An {@link Authentication} for the User
     */
    @NonNull
    static Authentication build(@NonNull String username) {
        return Authentication.build(username, null, null);
    }

    /**
     * Builds an {@link Authentication} instance for the user.
     * @param username User's name
     * @param roles User's roles
     * @return An {@link Authentication} for the User
     */
    @NonNull
    static Authentication build(@NonNull String username,
                                @NonNull Collection roles) {
        return Authentication.build(username, roles, null);
    }


    /**
     * Builds an {@link Authentication} instance for the user.
     * @param username User's name
     * @param attributes User's attributes
     * @return An {@link Authentication} for the User
     */
    @NonNull
    static Authentication build(@NonNull String username,
                                @NonNull Map attributes) {
        return new ServerAuthentication(username, null, attributes);
    }

    /**
     * Builds an {@link Authentication} instance for the user.
     * @param username User's name
     * @param roles User's roles
     * @param attributes User's attributes
     * @return An {@link Authentication} for the User
     */
    @NonNull
    static Authentication build(@NonNull String username,
                                @Nullable Collection roles,
                                @Nullable Map attributes) {
        return new ServerAuthentication(username, roles, attributes);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy