2.4 Understanding the Authentication Class Example

This section demonstrates how a password authentication class might be implemented by using the PasswordClass. All authentication classes are derived from the LocalAuthenticationClass, so you need to understand the key methods within it:

2.4.1 Extending the Base Authentication Class

Authentication classes extend the base class LocalAuthenticationClass as shown on lines 11 and 12 of PasswordClass Example Code. The LocalAuthenticationClass has a single constructor that must be called as shown in lines 20 - 23. The Identity Server uses this constructor to pass the necessary properties and user store information defined in the Administration Console to the class.

The LocalAuthenticationClass defines a single abstract method, doAuthenticate(), which must be implemented by new classes. During user authentication, the Identity Server creates an instance of an authentication class and calls the authenticate() method, which in turn calls the doAuthenticate() method. By default, the class instance remains persistent, allowing the state to be preserved between requests/responses while credentials are obtained. If persistence is not needed, the mustPersist() method can be overloaded to return False so new instances of the class are created upon each call to the authenticate() method.

2.4.2 Implementing the doAuthenticate Method

Lines 43 - 65 in the PasswordClass Example Code show how the doAuthenticate() method is used. Return values from this method indicate to the Identity Server that the class has succeeded or failed to authenticate a user or that additional user credentials are required and must be obtained.

The call to the isFirstCallAfterPrevMethod() method on line 49 determines if the call to the class is following a successful authentication by another class executed by a method. If that is the case, any credentials provided for the previous class most likely are not valid for this class and should not be tested for (line 52). In this example, the handlePostedData() method is called to obtain and validate a username and password entered by a user.

2.4.3 Prompting for Credentials

When lines are encountered in the PasswordClass Example Code, it has been determined that a page needs to be returned through the execution of a JSP to enable credentials to be prompted for and returned. Tests are made to determine if provisioning should be enabled, and if a Cancel button and federated providers should be displayed. The return value of HANDLED_REQUEST or SHOW_JSP indicates that the class has responded to the request and requires more information to proceed.

2.4.4 Verifying Credentials

The handlePostedData() method does much of the important work of this example (lines 74 - 114 in the PasswordClass Example Code). Lines 81 - 100 attempt to obtain the credentials.

Line 86 provides an example of obtaining a class property configured by an administrator. In this case, a query can be defined by the administrator that can be used to look up a user instead of using the username and password. If the query is used, the authenticateWithQuery method is called at line 88. If a query is not available, the authenticateWithPassword() method is called at line 98.

If the credentials correctly identify the user, the value AUTHENTICATED is returned. If they fail to identify the user, NOT_AUTHENTICATED is returned.

When eDirectory is the user store and a password has either expired or is expiring, the return values PWD_EXPIRED and PWD_EXPIRING can be returned respectively. See lines 102 - 108.

Line 111 demonstrates how an attribute is used to set an error message that is displayed to the user by calling the method getUserErrorMsg().

2.4.5 PasswordClass Example Code

1 package com.novell.nidp.authentication.local;
2 
3 import java.util.*;
4 
5 import org.eclipse.higgins.sts.api.*;
6 
7 import com.novell.nidp.*;
8 import com.novell.nidp.authentication.*;
9 import com.novell.nidp.common.authority.*;
10 
11 public class PasswordClass extends LocalAuthenticationClass implements 
12 STSAuthenticationClass
13 {
14    /**
15     * Constructor for form based authentication
16     *
17     * @param props      Properties associated with the implementing class
18     * @param uStores    List of ordered user stores to authenticate against
19     */
20    public PasswordClass(Properties props, ArrayList uStores)
21    {
22        super(props,uStores);
23    }
24
25    /**
26     * Get the authentication type this class implements
27     * 
28     * @return  returns the authentication type represented by this class
29     */
30    public String getType()
31    {
32        return AuthnConstants.PASSWORD;
33    }
34 
35    /**
36    * Perform form based authentication.  This method gets called on each 
37    * response during authentication process
38    * 
39    * @return  returns the status of the authentication process which is
40    *          one of AUTHENTICATED, NOT_AUTHENTICATED, CANCELLED, 
41    *          HANDLED_REQUEST, PWD_EXPIRING, PWD_EXPIRED
42    */
43    protected int doAuthenticate()
44    {
45        // If this is the first time the class is called following another method
46        // we want to display the form that will get the credentials.  This method
47       // prevents a previous form from providing data to the next form if any
48       // parameter names end up being the same
49        if (!isFirstCallAfterPrevMethod())
50        {
51       // This wasnt first time method was called, so see if data can be processed
52            int status = handlePostedData();
53            if (status != NOT_AUTHENTICATED)
54                return status;
55        }
56        
57        String jsp = getProperty(AuthnConstants.PROPERTY_JSP); 
58        if (jsp == null || jsp.length() == 0)
59            jsp = NIDPConstants.JSP_LOGIN;            
60
61        m_PageToShow = new PageToShow(jsp);
62        m_PageToShow.addAttribute(NIDPConstants.ATTR_URL, (getReturnURL() != null
63 ? getReturnURL() : m_Request.getRequestURL().toString()));
64        return SHOW_JSP;
65    }
66     
67    /**
68     * Get and process the data that is posted from the form
69     *
70     * @return  returns the status of the authentication process which is
71     *          one of AUTHENTICATED, NOT_AUTHENTICATED, CANCELLED, 
72     *          HANDLED_REQUEST, PWD_EXPIRING, PWD_EXPIRED
73     */
74    private int handlePostedData()
75    {
76        // Look for a name and password
77        String id       = m_Request.getParameter(NIDPConstants.PARM_USERID);
78        String password = m_Request.getParameter(NIDPConstants.PARM_PASSWORD);
79        
80        // Check to see if admin has setup for a custom query
81      String ldapQuery = checkForQuery();
82      
83        try
84        {
85            // using admin defined attributes for query
86            if (ldapQuery != null)
87            {
88              if (authenticateWithQuery(ldapQuery,password)) 
89                return AUTHENTICATED;
90            }
91            
92            // If using default of name and password
93            else
94            {
95                if (id == null || id.length() == 0)
96                    return NOT_AUTHENTICATED;
97         
98                if (authenticateWithPassword(id,password))
99                    return AUTHENTICATED;
100            }            
101        }
102        catch (PasswordExpiringException pe)
103        {
104            return PWD_EXPIRING;
105        }
106        catch (PasswordExpiredException pe)
107        {
108            return PWD_EXPIRED;
109        }
110        
111        m_Request.setAttribute(NIDPConstants.ATTR_LOGIN_ERROR,
112 getUserErrorMsg());
113        return NOT_AUTHENTICATED;
114    }
115
116    public NIDPPrincipal handleSTSAuthentication(ISecurityInformation
117 securityInformation)
118    {
119       IUsernameToken usernameToken = 
120         (IUsernameToken)securityInformation.getFirst(IUsernameToken.class);
121         
122       if (null != usernameToken)
123       {
124             try
125             {
126                 if (authenticateWithPassword(usernameToken.getUsername(),
127 usernameToken.getPassword()))
128                     return getPrincipal();
129             }
130             catch (PasswordExpiringException pe)
131             {
132                 return getPrincipal();
133             }
134             catch (PasswordExpiredException pe) {}
135       }
136       return null;
137    }
138 }