5.3 Login with Password

The loginWithPassword function is the instance function that initialises your access to Bayun. The function takes the following parameters :

Let's say an employee has loginId username@bayunsystems.com.

  • activity : Activity Context.

  • companyName : Unique name of the company/tenant the authenticating employee belongs to, e.g. “bayunsystems.com”. This should be chosen using exactly the same methodology that was used during user registration via registerEmployeeWithPassword. Note that in some cases the email domain of the user could be different from the domain of the tenant this user belongs to. In such a case, the domain-name part of the tenant is what should be used as the companyName parameter.

  • companyEmployeeId : EmployeeId unique within the company, e.g. "username@bayunsystems.com". This should also be chosen using exactly the same methodology that was used during user registration via registerEmployeeWithPassword. Note that while just the "username" portion might suffice in some cases, it is preferable to use the full loginId for consistency.

  • password : Password of the user. Used to keep user secret keys protected. Never stored or transmitted by BayunSDK in clear. If the developer wishes, it can be a cryptographic hash of the password instead of the cleartext password itself. Bayun just needs a unique secret known to the user only, or something unique generated from it, for keeping the user lockboxes protected in such a way that nobody other than the user has access to it (similar to how iPhone does it with user’s device PIN).

  • autoCreateEmployee : Determines whether or not an employee should be created automatically on Bayun’s system if it does not already exist within the given company. If set to true, an attempt is made to authenticate against an existing employee account first, but if there is no such employee within the given company, a new one is created instead with the supplied credentials. This provides an easy integration option for the developer to use a single login call in an existing application for the simpler use-cases, rather than having to integrate separately with more involved registration & authorization flow along-with the separate login flow. Use this feature only when Bayun’s auth mechanism is being used as shadow auth for your app’s own authentication, using the same user password. And make sure the user has already been successfully authenticated to your own app’s authentication mechanism, before calling Bayun’s loginWithPassword function with autoCreateEmployee set to true.

  • authorizeEmployeeCallback : Block to be executed if employee public key authorization is pending, returns employeePublicKey.

  • securityQuestionsCallback : Most developers can just leave it null for default functionality. It is used for taking answers of Security Questions from the User when extra security with two-factor authorization is enabled. By default, the SDK uses Dialog to take User’s input for the answers of the Security Questions, if two-factor authorization is enabled for the user trying to authenticate. The developer can optionally provide a custom UI block for taking User’s input, to match with the look-and-feel of the app, instead of relying on the default dialog. If non-null, this block will need to take user answers to the security questions as an input and call validateSecurityQuestions API method in the SDK. The callback is triggered when two-factor authorization is enabled for the user authenticating with Bayun. The Security Questions and QuestionIds are returned through data of the callback, in the form of List<SecurityQuestion> .

  • passphraseCallback : Optional block if passphrase is enabled. Most developers can just leave it null for default functionality. It is used for taking user passphrase input for extra security when passphrase is explicitly enabled by the user. By default, the SDK uses Dialog to take user input for passphrase if it is enabled for a user. However the developer can optionally provide a custom UI block to match with the look-and-feel of the app instead of relying on the default dialog. If non-null, this block will need to take user passphrase as input and call Bayun validatePassphrase API for Passphrase validation.

  • successCallback : Success block to be executed after successful employee login.

  • failureCallback : Failure block to be executed if employee login fails, returns BayunError.

Validate Security Questions

Use validateSecurityQuestions function to validate the security questions' answers.

The function takes the following parameters

  • answers : Security questions' answers of type List<SecurityAnswer>.

  • authorizeEmployeeCallback : Block to be executed if employee public key authorization is pending, returns employeePublicKey.

  • successCallback : Success block to be executed after successful Security Questions' Answers validation.

  • failureCallback : Failure block to be executed if user Security Questions' Answers validation fails, returns BayunError.

Validate Passphrase

Use validatePassphrase function to validate the passphrase.

The function takes the following parameters :

  • passphrase : Passphrase to validate.

  • authorizeEmployeeCallback : Block to be executed if employee public key authorization is pending, returns employeePublicKey.

  • successCallback : Success block to be executed after successful user passphrase validation.

  • failureCallback : Failure block to be executed if user passphrase validation fails, returns BayunError.

BayunCore class should be inited on server using App Secret which has role Authorization to be able to authorize an employee.

String companyName = "bayunsystems.com"; //company portion from loginId
String companyEmployeeId = "username"; //username portion from loginId
String password = "<password>";  //user input
boolean autoCreateEmployee = true;
Activity activity = this;

Handler.Callback authorizeEmployeeCallback = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
         Log.d(TAG, "Authorization of employeePublicKey is Pending.")
         String employeePublicKey = message.getData().getString("employeePublicKey", "");
         return false;
    }
};

Handler.Callback securityQuestionsCallback = new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
        Bundle bundle = msg.getData();
        
        //securityQuestionsArray is a list of Security Question Objects with questionId, questionText 
        ArrayList<SecurityQuestion> securityQuestionsArray = (ArrayList<SecurityQuestion>)msg.getData().getSerializable("securityQuestions");
        
       //Show custom UI to take user input for the answers.
       //Call validateSecurityQuestions function with the user provided answers.
        ArrayList<SecurityAnswer> answers = new ArrayList<>();

        SecurityAnswer securityAnswer1 = new SecurityAnswer(securityQuestionsArray.get(0).getQuestionId(),"<answer1>".toCharArray());
        SecurityAnswer securityAnswer2 = new SecurityAnswer(securityQuestionsArray.get(1).getQuestionId(),"<answer2>".toCharArray());
        SecurityAnswer securityAnswer3 = new SecurityAnswer(securityQuestionsArray.get(2).getQuestionId(),"<answer3>".toCharArray());
        SecurityAnswer securityAnswer4 = new SecurityAnswer(securityQuestionsArray.get(3).getQuestionId(),"<answer4>".toCharArray());
        SecurityAnswer securityAnswer5 = new SecurityAnswer(securityQuestionsArray.get(4).getQuestionId(),"<answer5>".toCharArray());
        answers.add(securityAnswer1);
        answers.add(securityAnswer2);
        answers.add(securityAnswer3);
        answers.add(securityAnswer4);
        answers.add(securityAnswer5);
        
        
        Handler.Callback authorizeEmployeeCallback = new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
             Log.d(TAG, "Authorization of employeePublicKey is Pending.")
             String employeePublicKey = message.getData().getString("employeePublicKey", "");
             return false;
            }
        };

        Handler.Callback successCallback = new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                Log.d(TAG, "Loggrd in with Bayun successfully.");
                return false;
            }
        };

        Handler.Callback failureCallback = new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                String error = message.getData().getString("BayunError", "");
                Log.d(TAG, "One or more answers are incorrect.");     
                return false;
            }
        };        

        bayunCore.validateSecurityQuestions(answers, authorizeEmployeeCallback, successCallback, failureCallback);
        return false;
     }
};
                               
Handler.Callback passphraseCallback = new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
        //Show custom UI to take user input for the passphrase.
        String passpharse ="<passpharse>";
        
        Handler.Callback authorizeEmployeeCallback = new Handler.Callback() {
        @Override
            public boolean handleMessage(Message message) {
                Log.d(TAG, "Authorization of employeePublicKey is Pending.")
                String employeePublicKey = message.getData().getString("employeePublicKey", "");
                return false;
                }
        };

        Handler.Callback successCallback = new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                Log.d(TAG, "Passphrase is validated and Logged in with Bayun successfully.");
                return false;
            }
        };

        Handler.Callback failureCallback = new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                String error = message.getData().getString("BayunError", "");
                Log.d(TAG, "Passphrase validation failed with error.");     
                return false;
            }
        };
        //Call validatePassphrase function with the user provided passphrase.
        bayunCore.validatePassphrase(passpharse, authorizeEmployeeCallback, successCallback, failureCallback);
        return false;
        }
 };

Handler.Callback successCallback = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        Log.d(TAG, "Logged in with Bayun successfully.");
        return false;
    }
};

Handler.Callback failureCallback = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String error = message.getData().getString("BayunError", "");
        Log.d(TAG, "Login failed with error.");     
        return false;
    }
};

bayunCore.loginWithPassword(activity,
                            companyName,
                            companyEmployeeId,
                            password,
                            autoCreateEmployee,
                            authorizeEmployeeCallback,
                            securityQuestionsCallback,
                            passphraseCallback,
                            successCallback,
                            failureCallback);

Last updated