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

com.microsoft.sqlserver.jdbc.KerbCallback Maven / Gradle / Ivy

The newest version!
/*
 * Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
 * available under the terms of the MIT License. See the LICENSE file in the project root for more information.
 */

package com.microsoft.sqlserver.jdbc;

import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;


/**
 * Provides implemention of the callback handler for Kerberos.
 */
public class KerbCallback implements CallbackHandler {

    private final SQLServerConnection con;
    private String usernameRequested = null;

    KerbCallback(SQLServerConnection con) {
        this.con = con;
    }

    private static String getAnyOf(Callback callback, Properties properties,
            String... names) throws UnsupportedCallbackException {
        for (String name : names) {
            String val = properties.getProperty(name);
            if (val != null && !val.trim().isEmpty()) {
                return val;
            }
        }
        throw new UnsupportedCallbackException(callback,
                "Cannot get any of properties: " + Arrays.toString(names) + " from con properties");
    }

    /**
     * Returns if a name was retrieved By Kerberos.
     *
     * @return null if callback was not called or username was not provided
     */
    public String getUsernameRequested() {
        return usernameRequested;
    }

    @Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (Callback callback : callbacks) {
            if (callback instanceof NameCallback) {
                usernameRequested = getAnyOf(callback, con.activeConnectionProperties, "user",
                        SQLServerDriverStringProperty.USER.name());
                ((NameCallback) callback).setName(usernameRequested);
            } else if (callback instanceof PasswordCallback) {
                String password = getAnyOf(callback, con.activeConnectionProperties, "password",
                        SQLServerDriverStringProperty.PASSWORD.name());
                ((PasswordCallback) callback).setPassword(password.toCharArray());

            } else {
                throw new UnsupportedCallbackException(callback, "Unrecognized Callback type: " + callback.getClass());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy