Saturday 15 July 2017

JAAS: Reading module options

By using module options, we can pass the values directly to the login module. For example, we can define the option, whether we should log the information, while authenticating or not.

How to specify module options?
By using key=value pair syntax, we can specify the module options. the value must be enclosed in double quotes.

Ex:
JaasTutorial{
         com.smaple.login.BasicLoginModule required
                                  debug="true"
                                  cache="enable";
};

Is there any limit on number of module options?
No, you can specify any number of options.

Can I specify system properties?
Yes, you can specify the system properties in the form of ${system.property}

Ex:
JaasTutorial{
         com.smaple.login.BasicLoginModule required
                                  debug="true"
                                  cache="enable"
                                  userHome="${user.home}"
                                  appHome = "${user.home}${/}app";
};

How can I get the module options defined in jaas configuration file?
Login context provides these module options while initializing the login module.
public class BasicLoginModule implements LoginModule {

 @Override
 public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
   Map<String, ?> options) {
  this.callbackHandler = callbackHandler;

  System.out.println("Module options are : ");
  System.out.println("*************************************");
  System.out.println("debug : " + options.get("debug"));
  System.out.println("cache : " + options.get("cache"));
  System.out.println("userHome : " + options.get("userHome"));
  System.out.println("userHome : " + options.get("userHome"));
  System.out.println("*************************************");
 }

 .....
 .....
}

Find the below working application.


jaasAuth.config
JaasTutorial{
 com.smaple.login.BasicLoginModule required
    debug="true"
    cache="enable"
    userHome="${user.home}"
    appHome = "${user.home}${/}app";
};


BasicAuthCallbackHandler.java
package com.sample.handler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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;

public class BasicAuthCallbackHandler implements CallbackHandler{

 
 @Override
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  NameCallback nameCallBack = (NameCallback)callbacks[0];
  PasswordCallback passwordCallback = (PasswordCallback)callbacks[1];
  
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
  System.out.println(nameCallBack.getPrompt());
  nameCallBack.setName(br.readLine());
  
  System.out.println(passwordCallback.getPrompt());
  passwordCallback.setPassword(br.readLine().toCharArray());
  
 }

}


BasicLoginModule.java
package com.smaple.login;

import java.io.IOException;
import java.util.Map;

import javax.security.auth.Subject;
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;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;

public class BasicLoginModule implements LoginModule {

 private String username = "krishna";
 private String password = "krishna";
 CallbackHandler callbackHandler;

 @Override
 public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
   Map<String, ?> options) {
  this.callbackHandler = callbackHandler;

  System.out.println("Module options are : ");
  System.out.println("*************************************");
  System.out.println("debug : " + options.get("debug"));
  System.out.println("cache : " + options.get("cache"));
  System.out.println("userHome : " + options.get("userHome"));
  System.out.println("appHome : " + options.get("appHome"));
  System.out.println("*************************************");
 }

 @Override
 public boolean login() throws LoginException {
  Callback[] callbackArray = new Callback[2];

  callbackArray[0] = new NameCallback("Enter logon id:");
  callbackArray[1] = new PasswordCallback("Enter password:", false);

  try {
   callbackHandler.handle(callbackArray);
  } catch (IOException | UnsupportedCallbackException e) {
   e.printStackTrace();
   throw new LoginException(e.getMessage());
  }

  String logonId = ((NameCallback) callbackArray[0]).getName();
  char[] passwordArr = ((PasswordCallback) callbackArray[1]).getPassword();
  String password = new String(passwordArr);

  if (username.equals(logonId) && this.password.equals(password)) {
   System.out.println("Login successful");
   return true;
  }

  throw new LoginException("Logon failed");
 }

 @Override
 public boolean commit() throws LoginException {
  return true;
 }

 @Override
 public boolean abort() throws LoginException {
  return false;
 }

 @Override
 public boolean logout() throws LoginException {
  return true;
 }

}

Test.java
package com.sample.app;

import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import com.sample.handler.BasicAuthCallbackHandler;

public class Test {

 public static void main(String args[]) {
  System.setProperty("java.security.auth.login.config", "jaasAuth.config");
  LoginContext loginContext = null;

  try {
   loginContext = new LoginContext("JaasTutorial", new BasicAuthCallbackHandler());
  } catch (LoginException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return;
  }

  try {
   loginContext.login();
  } catch (LoginException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}


Output
Module options are : 
*************************************
debug : true
cache : enable
userHome : C:\users\krishna
appHome : C:\users\krishna\app
*************************************
Enter logon id:
krishna
Enter password:
krishna
Login successful





Previous                                                 Next                                                 Home

No comments:

Post a Comment