Tuesday, September 8, 2015

Java Program to use JPam Authentication

JPam is a Java-PAM bridge. PAM, or Pluggable Authentication Modules, is a standard security architecture used on Linux, Mac OS X, Solaris, HP-UX and other Unix systems. JPam is the missing link between the two.

JPAM permits the use of PAM authentication facilities by Java applications running on those platforms.

These facilities include:

account
auth
password
session

In this example I will demonstrate how to use Jpam in your java application.
1. Download the JPam from http://sourceforge.net/projects/jpam/files/
2. Extract the tar on the disk
3. change directory to the extracted folder and copy the net-sf-jpam file to the /etc/pam.d
4. Create a sample java program-make sure you have JPam-1.1.jar in the classpath

import java.util.Arrays;
import java.util.Arrays;
import java.util.List;

import net.sf.jpam.Pam;


public class PamAuthenticator {
public static void main(String[] args) {

authenticate(args[0],args[1],args[2]);
}

public static void authenticate(String user, String password,String profile) {
Pam pam = new Pam(profile);
System.out.println("Starting auth with username "+user+" passwd "+password+" for profile "+profile);
if (!pam.authenticateSuccessful(user, password)) {
throw new RuntimeException(String.format("PAM profile '%s' validation failed", profile));
}
System.out.println("done with "+profile);
}
}

5. compile and run the test class as follows
java -cp .:* -Djava.library.path=`pwd` PamAuthenticator mapr mapr login

output:
Starting auth with username mapr passwd mapr for profile login
done with login