SimpleLoginModule.java

package fr.umlv.ji.security;
import java.util.*;
import javax.security.auth.spi.LoginModule;
import javax.security.auth.*;
import javax.security.auth.callback.*;
import javax.security.auth.login.*;
public abstract class SimpleLoginModule implements LoginModule {
  // Diverses variables
  protected Subject subject;
  protected CallbackHandler loginHandler;
  protected boolean debug = false;
  protected boolean succeeded = false;
  protected boolean commitSucceeded = false;
  // Informations utilisateur
  private String login;
  private char[] password;
  private String file;
  // Identité
  private SimplePrincipal userPrincipal;
  /** Méthode pour la vérification du mot de passe. */
  public abstract boolean verifyPasswd(String log, char[] pass, String f);
  /** Initialisation du module. */
  public void initialize(Subject subject, CallbackHandler loginHandler,
             Map sharedState, Map options) {
    // Stockage des arguments utiles
    this.subject = subject;
    this.loginHandler = loginHandler;
    // Récupération des options
    debug = "true".equalsIgnoreCase((String)options.get("debug"));
    file = (String)options.get("passwdFile");
  }
  /** Méthode de demande d'authentification. */
  public boolean login() throws LoginException {
    // Si pas de gestionnaire d'interaction avec l'utilisateur
    // arrêter l'authentification
    if (loginHandler==null) 
      throw new LoginException("Error: no CallbackHandler available.");
    // Création du tableau des informations à récupérer
    // (login et mot de passe)
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("login: ");
    callbacks[1] = new PasswordCallback("password: ", false);
    try {
      // Demande de login et de mot de passe
      loginHandler.handle(callbacks);
      // Stockage des données obtenues auprès de l'utilisateur
      login = ((NameCallback)callbacks[0]).getName();
      char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
      if (tmpPassword==null) {
    password = new char[0];
      } else {
    password = new char[tmpPassword.length];
    System.arraycopy(tmpPassword,0, password,0,tmpPassword.length);
      }
      // Effacement du mot de passe stocké
      ((PasswordCallback)callbacks[1]).clearPassword();
    } catch (java.io.IOException ioe) {
      throw new LoginException(ioe.toString());
    } catch (UnsupportedCallbackException uce) {
      throw new LoginException("Error: no authentication");
    }
    if (debug) {
      System.err.println("Login: " + login);
      
      System.err.println("Password: " + password);
    }
    // Appel de la fonction de vérification de login
    succeeded = verifyPasswd(login,password,file);
    if (succeeded) {
      if (debug) System.out.println("Authentication succeeded");
      return true;
    }
    if (debug) System.out.println("Authentication failed");
    login = null;
    password = null;
    throw new FailedLoginException("Login failed");
  }
  /** Fonction appelée si tous les appels aux méthodes login() des
      modules d'authentification de l'applicaton ont été des
      succès. Elle complète le sujet avec de nouvelles identités. */
  public boolean commit() throws LoginException {
    if (!succeeded) return false;
    // Création de la nouvelle identité
    userPrincipal = new SimplePrincipal(login);
    if (!subject.getPrincipals().contains(userPrincipal))
      subject.getPrincipals().add(userPrincipal);
    if (debug) System.out.println("Added  SimplePrincipal to Subject");
    // Effacement des valeurs stockées
    login = null;
    password = null;
    commitSucceeded = true;
    return true;
  }
  /** Fonction appelée en cas d'erreur d'authentification. */ 
  public boolean abort() throws LoginException {
    if (!succeeded) return false;
    if (succeeded && !commitSucceeded) {
      succeeded = false;
      login = null;
      password = null;
      userPrincipal = null;
    } else
      logout();
    return true;
  }
  /** Fonction appelée pour la déconnexion. */
  public boolean logout() throws LoginException {
    subject.getPrincipals().remove(userPrincipal);
    succeeded = commitSucceeded = false;
    login = null;
    password = null;
    userPrincipal = null;
    return true;
} }