Exemple de code 2

L'exemple de code suivant illustre l'authentification personnalisée des utilisateurs à l'aide d'un nom d'utilisateur et d'un mot de passe contenus dans un fichier plat. Vous devez initialiser les listes de mots de passe et d'utilisateurs dans le constructeur de classe pour que l'authentification personnalisée fonctionne.

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{
  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){
          // log a message                   
  }
  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 flat file */

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");
          }
}