Sample Code 2

The following sample code demonstrates custom authentication of users using user name and password contained in a flat file. You must initialize user and password lists in the class constructor to make custom authentication work.

package com.hyperion.css.security;

import java.util.Map;
import java.util.HashMap;
import com.hyperion.css.CSSCustomAuthenticationIF;
import java.io.*;

public class CSSCustomAuthenticationImpl implements CSSCustomAuthenticationIF{
  //get the Logger to log Exception or other info useful for debugging
  /*static Logger logger = Logger.getLogger
    ("com.hyperion.css.custom.CSSCustomAuthenticationImpl"); */
  static final String DATA_FILE = "datafile.txt";

/**
  * authenticate method includes the core implementation of the
  * Custom Authentication Mechanism. If custom authentication is 
  * enabled for the provider, authentication operations
  * are delegated to this method. Upon successful authentication,
  * this method returns a valid user name, using which EPM System 
  * retrieves the user from a custom authentication enabled provider.
  * User name can be returned in the format username@providerName, 
  * where providerName indicates the name of the underlying provider 
  * where the user is available. authenticate method can use other 
  * private methods to access various core components of the 
  * custom authentication module.

  *   @param context
  *   @param userName
  *   @param password
  *   @return
  *   @throws Exception
*/
	
Map users = null;
	
public CSSCustomAuthenticationImpl(){
  users = new HashMap();
  InputStream is = null;
  BufferedReader br = null;
  String line;
  String[] userDetails = null;
  String userKey = null;
  try{
     is = CSSCustomAuthenticationImpl.class.getResourceAsStream(DATA_FILE);
     br = new BufferedReader(new InputStreamReader(is));
     while(null != (line = br.readLine())){
        userDetails = line.split(":");
          if(userDetails != null && userDetails.length==3){
            userKey = userDetails[0]+ ":" + userDetails[1];
            users.put(userKey, userDetails[2]);
          }
      }
     }
  catch(Exception e){
		
		}
  finally{
     try{
        if(br != null) br.close();
        if(is != null) is.close();
     }
     catch(IOException ioe){
        ioe.printStackTrace();
     }
  }
}
/* Use this authenticate method snippet to return username from a flatfile */

public String authenticate(Map context,String userName, String password)throws Exception{

  //UserName : user input for the userName
  //Password : user input for password
  //context  : Map, can be used to additional information required by 
  //           the custom authentication module.
		
  String authenticatedUserKey = userName + ":" + password;

  if(users.get(authenticatedUserKey)!=null)
     return(String)users.get(authenticatedUserKey);
  else throw new Exception("Invalid User Credentials");
	}

/* Refer to this authenticate method snippet to return username in 
   username@providername format */

public String authenticate(Map context,String userName, String password)throws Exception{

  //UserName : user input for userName
  //Password : user input for password
  //context  : Map, can be used to additional information required by 
  //           the custom authentication module.

  //Your code should uniquely identify the user in a custom provider and in a configured 
  //user directory in Shared Services. EPM Security expects you to append the provider 
  //name to the user name. Provider name must be identical to the name of a custom 
  //authentication-enabled user directory specified in Shared Services.

  //If invalid arguments, return null or throw exception with appropriate message
  //set authenticationSuccessFlag = false	

  String authenticatedUserKey = userName + ":" + password;
  if(users.get(authenticatedUserKey)!=null)
     String userNameStr = (new StringBuffer())
                           .append((String)users.get(authenticatedUserKey))
                           .append("@").append(PROVIDER_NAME).toString();
			return userNameStr;
  else throw new Exception("Invalid User Credentials");
	}
}