//------------------------------------------------------------------------------
// <copyright file="Credentials.java" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <summary>
//      This class provides the ability for the user to specify a username and password during runtime.
// </summary>
//------------------------------------------------------------------------------

package com.microsoft.hpc.scheduler.session;
//moved from namespace Microsoft.Hpc

import com.microsoft.hpc.SessionStartInfoContract;

/**
 * This class provides the ability for the user to specify a username and password during runtime.
 *
 * <p>This is NOT the recommended way to authenticate a session. Your console may not be visible to java. Use
 * <code>HpcJava.setUsernamePassword(String, String)</code> to set username and password dynamically</p>
 * 
 * @see HpcJava#setUsernamePassword(String, String)
 */
public class Credentials
{
    //The only thing Java can do is prompt for credentials...
    
    /**
     * This function will prompt for the user for the username and password and set it to all new SessionStartInfo and SessionAttachInfo variables
     * For this function to work, two conditions must be true: if interface mode is set to true and this program was initialized with a console.
     * 
     * @param fConsole Interface mode
     * @param info SessionStartInfo to be authenticated
     */
    static Boolean promptForCredentials(Boolean fConsole, SessionStartInfo info)
    {
        if(fConsole && System.console() != null)
        {
            String username = null;
            char[] password = null;
            char[] password2 = null;
            
            for(int i = 0; i < 3; i++)
            {
                System.out.print("Enter username: ");
                username = System.console().readLine();
                System.out.print("Enter password: ");
                password = System.console().readPassword();
                System.out.print("Enter password again for verification: ");
                password2 = System.console().readPassword();
                
                if(!password.equals(password2))
                {
                    System.out.println("Passwords do not match");
                    username = null;
                }
                else
                {
                    HpcJava.setUsernamePassword(username, new String(password));
                    com.microsoft.hpc.ObjectFactory of = new com.microsoft.hpc.ObjectFactory();
                    info.setUsername(of.createSessionStartInfoContractUsername(username));
                    info.setPassword(of.createSessionStartInfoContractPassword(new String(password)));
                    break;
                }
                
            }
            
            if(username == null){
                System.out.println("Invalid username");
                return false;
            }
            return true;
        }
        return false;
    }
}
