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

org.graylog2.security.AuthenticationConfig Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog2.security;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import org.graylog2.security.realm.AccessTokenAuthenticator;
import org.graylog2.security.realm.LdapUserAuthenticator;
import org.graylog2.security.realm.PasswordAuthenticator;
import org.graylog2.security.realm.RootAccountRealm;
import org.graylog2.security.realm.SessionAuthenticator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@AutoValue
public abstract class AuthenticationConfig {

    @JsonProperty("realm_order")
    public abstract List realmOrder();

    @JsonProperty("disabled_realms")
    public abstract Set disabledRealms();


    @JsonCreator
    public static AuthenticationConfig create(@JsonProperty("realm_order") List order,
                                              @JsonProperty("disabled_realms") Set disabledRealms) {
        return builder()
                .realmOrder(order)
                .disabledRealms(disabledRealms)
                .build();
    }

    public static AuthenticationConfig defaultInstance() {
        return builder()
                // the built-in default order of authenticators
                .realmOrder(ImmutableList.of(
                        SessionAuthenticator.NAME,
                        AccessTokenAuthenticator.NAME,
                        LdapUserAuthenticator.NAME,
                        PasswordAuthenticator.NAME,
                        RootAccountRealm.NAME))
                .disabledRealms(Collections.emptySet())
                .build();
    }

    public AuthenticationConfig withRealms(final Set availableRealms) {
        final List newOrder = new ArrayList<>();

        // Check if realm actually exists.
        realmOrder().stream()
                .filter(availableRealms::contains)
                .forEach(newOrder::add);

        // Add availableRealms which are not in the config yet to the end.
        availableRealms.stream()
                .filter(realm -> !newOrder.contains(realm))
                .sorted(String.CASE_INSENSITIVE_ORDER)
                .forEach(newOrder::add);

        return toBuilder().realmOrder(newOrder).build();
    }

    public abstract Builder toBuilder();

    private static Builder builder() {
        return new AutoValue_AuthenticationConfig.Builder();
    }

    @AutoValue.Builder
    public static abstract class Builder {

        public abstract Builder realmOrder(List order);

        public abstract Builder disabledRealms(Set disabledRealms);

        public abstract AuthenticationConfig build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy