Code Samples

Using the JPAM API

Pam Class Diagram

Attempt to authenticate a username and password

    String user1Name = "test";
    String user1Credentials = "testPassword";
    Pam pam = new Pam();
    boolean authenticated = pam.authenticateSuccessful(user1Name, user1Credentials));

Using the JAAS API

JPamLoginModule Class Diagram

Shows how to use the JAAS API together with a CallbackHandler.

    LoginContext loginContext = new LoginContext("net-sf-jpam", new JpamCallbackHandler());

     * The application must implement the CallbackHandler.
     * <p/>
     * <p> This application is text-based.  Therefore it displays information
     * to the user using the OutputStreams System.out and System.err,
     * and gathers input from the user using the InputStream,
    class JpamCallbackHandler implements CallbackHandler {

         * Invoke an array of Callbacks.
         * <p/>
         * <p/>
         * @param callbacks an array of <code>Callback</code> objects which contain
         *                  the information requested by an underlying security
         *                  service to be retrieved or displayed.
         * @throws          if an input or output error occurs. <p>
         * @throws UnsupportedCallbackException if the implementation of this
         *                                      method does not support one or more of the Callbacks
         *                                      specified in the <code>callbacks</code> parameter.
        public void handle(Callback[] callbacks)
                throws IOException, UnsupportedCallbackException {

            for (int i = 0; i < callbacks.length; i++) {
                if (callbacks[i] instanceof TextOutputCallback) {

                    // display the message according to the specified type
                    TextOutputCallback toc = (TextOutputCallback) callbacks[i];
                    switch (toc.getMessageType()) {
                        case TextOutputCallback.INFORMATION:
                        case TextOutputCallback.ERROR:
                            System.out.println("ERROR: " + toc.getMessage());
                        case TextOutputCallback.WARNING:
                            System.out.println("WARNING: " + toc.getMessage());
                            throw new IOException("Unsupported message type: "
                                    + toc.getMessageType());

                } else if (callbacks[i] instanceof NameCallback) {

                    // prompt the user for a username
                    NameCallback nc = (NameCallback) callbacks[i];

                } else if (callbacks[i] instanceof PasswordCallback) {

                    // prompt the user for sensitive information
                    PasswordCallback pc = (PasswordCallback) callbacks[i];

                } else {
                    throw new UnsupportedCallbackException
                            (callbacks[i], "Unrecognized Callback");


Browse the JUnit Tests

JPam comes with a comprehensive JUnit test suite, which not only tests the code, but shows you how to use JPam.

A link to browsable unit test source code for the major JPam classes is given per section. The unit tests are also in the in the JPam tarball.