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

org.finos.legend.engine.postgres.AuthenticationContext Maven / Gradle / Ivy

There is a newer version: 4.66.0
Show newest version
/*
 * Licensed to Crate.io GmbH ("Crate") under one or more contributor
 * license agreements.  See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.  Crate licenses
 * this file to you 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.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial agreement.
 */

package org.finos.legend.engine.postgres;

import java.io.Closeable;
import org.finos.legend.engine.postgres.auth.AuthenticationMethod;
import org.finos.legend.engine.postgres.auth.AuthenticationMethodType;
import org.finos.legend.engine.postgres.auth.SecureString;
import org.finos.legend.engine.shared.core.identity.Identity;
import org.slf4j.Logger;

class AuthenticationContext implements Closeable
{
    private SecureString password;
    private final String userName;
    private final Logger logger;

    private final AuthenticationMethod authMethod;
    private final ConnectionProperties connProperties;

    /**
     * Create a context that holds information for authenticating a user using a certain
     * authentication method. The context instance is created after reading the startup body of the
     * newly established connection. The password is optional and can be provided as a char[] when the
     * message handler of the protocol implementation obtains the password from the client.
     *
     * @param authMethod     The method that is used for authentication. {@link AuthenticationMethod}
     * @param connProperties Additional connection properties
     * @param userName       The name of the user to authenticate.
     * @param logger         The logger instance from {@link PostgresWireProtocol}
     */
    AuthenticationContext(AuthenticationMethod authMethod, ConnectionProperties connProperties,
                          String userName, Logger logger)
    {
        this.authMethod = authMethod;
        this.connProperties = connProperties;
        this.userName = userName;
        this.logger = logger;
        this.password = null;
    }


    Identity authenticate()
    {
        Identity user = authMethod.authenticate(userName, password, connProperties);
        if (user != null)
        {
            logger.trace("Authentication succeeded user \"{}\" and method \"{}\".", user.getName(),
                    authMethod.name());
        }
        return user;
    }

    AuthenticationMethodType getAuthenticationMethodType()
    {
        return authMethod.name();
    }

    void setSecurePassword(char[] secureString)
    {
        this.password = new SecureString(secureString);
    }



    /**
     * Close method should be called as soon as possible in order to clear out the password char[].
     * Once close was called, {@link #authenticate()} would fail due to empty password.
     */
    @Override
    public void close()
    {
        if (password != null)
        {
            password.close();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy