Index: topia/src/java/org/codelutin/topia/security/TopiaGroupPrincipal.java diff -u /dev/null topia/src/java/org/codelutin/topia/security/TopiaGroupPrincipal.java:1.1 --- /dev/null Fri Apr 29 16:00:44 2005 +++ topia/src/java/org/codelutin/topia/security/TopiaGroupPrincipal.java Fri Apr 29 16:00:39 2005 @@ -0,0 +1,38 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* TopiaGroupPrincipal.java +* +* Created: 29 avr. 2005 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +*/ + + +package org.codelutin.topia.security; + + +public class TopiaGroupPrincipal extends TopiaPrincipal { + public TopiaGroupPrincipal(String name) { + this.name = "group." + name; + } +} Index: topia/src/java/org/codelutin/topia/security/TopiaLoginModule.java diff -u /dev/null topia/src/java/org/codelutin/topia/security/TopiaLoginModule.java:1.1 --- /dev/null Fri Apr 29 16:00:44 2005 +++ topia/src/java/org/codelutin/topia/security/TopiaLoginModule.java Fri Apr 29 16:00:39 2005 @@ -0,0 +1,151 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, + * Cédric Pineau, Benjamin Poussin, + * + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * TopiaLoginModule.java + * + * Created: 29 avr. 2005 + * + * @author Arnaud Thimel + * @version $Revision: 1.1 $ + */ + +package org.codelutin.topia.security; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import javax.security.auth.Subject; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.auth.login.LoginException; +import javax.security.auth.spi.LoginModule; + +import org.codelutin.topia.TopiaContext; +import org.codelutin.topia.TopiaContextFactory; +import org.codelutin.topia.TopiaException; + +public class TopiaLoginModule implements LoginModule { + + private Subject subject; + private CallbackHandler callbackHandler; + private boolean loginSuccess; + private List principals; + private String appContextFile; + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#initialize(javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, java.util.Map, java.util.Map) + */ + public void initialize(Subject subject, CallbackHandler callbackHandler, + Map sharedState, Map options) { +System.out.println("org.codelutin.topia.security.TopiaLoginModule.initialize"); + this.subject = subject; + this.callbackHandler = callbackHandler; + this.principals = null; + this.appContextFile = (String)options.get("topia.app.context"); + } + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#login() + */ + public boolean login() throws LoginException { +System.out.println("org.codelutin.topia.security.TopiaLoginModule.login"); + + if (callbackHandler == null) + throw new LoginException("CallbackHandler cannot be null"); + if (appContextFile == null) + throw new LoginException("\"topia.app.context\" property must be set"); + + String login, password = null; + + loginSuccess = false; + + NameCallback nc = new NameCallback("login"); + PasswordCallback pc = new PasswordCallback("password", false); + + Callback[] callbacks = new Callback[2]; + + callbacks[0] = nc; + callbacks[1] = pc; + + try { + //Récupération du login et mot de passe + callbackHandler.handle(callbacks); + } catch (IOException e2) { + e2.printStackTrace(); + } catch (UnsupportedCallbackException e2) { + e2.printStackTrace(); + } + login = nc.getName(); + password = new String(pc.getPassword()); + + //Récupération du TopiaContext + TopiaContext context = null; + try { + context = TopiaContextFactory.getContext(appContextFile); + } catch (TopiaException e1) { + e1.printStackTrace(); + } + + //Véricfication du login/pass et récupération des Principal + try { + principals = context.authenticate(login, password); + } catch (TopiaSecurityException e) { + System.err.println("Login failed : " + e.getMessage()); + return false; + } + + loginSuccess = true; + return true; + } + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#commit() + */ + public boolean commit() throws LoginException { +System.out.println("org.codelutin.topia.security.TopiaLoginModule.commit"); + if (loginSuccess) { + subject.getPrincipals().addAll(principals); + return true; + } + return false; + } + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#abort() + */ + public boolean abort() throws LoginException { +System.out.println("org.codelutin.topia.security.TopiaLoginModule.abort"); + return false; + } + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#logout() + */ + public boolean logout() throws LoginException { +System.out.println("org.codelutin.topia.security.TopiaLoginModule.logout"); + return false; + } + +} Index: topia/src/java/org/codelutin/topia/security/TopiaPrincipal.java diff -u /dev/null topia/src/java/org/codelutin/topia/security/TopiaPrincipal.java:1.1 --- /dev/null Fri Apr 29 16:00:44 2005 +++ topia/src/java/org/codelutin/topia/security/TopiaPrincipal.java Fri Apr 29 16:00:39 2005 @@ -0,0 +1,47 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* TopiaPrincipal.java +* +* Created: 29 avr. 2005 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +*/ + + +package org.codelutin.topia.security; + +import java.security.Principal; + + +public class TopiaPrincipal implements Principal { + + protected String name; + + /* (non-Javadoc) + * @see java.security.Principal#getName() + */ + public String getName() { + return name; + } + +} Index: topia/src/java/org/codelutin/topia/security/TopiaSecurityException.java diff -u /dev/null topia/src/java/org/codelutin/topia/security/TopiaSecurityException.java:1.1 --- /dev/null Fri Apr 29 16:00:44 2005 +++ topia/src/java/org/codelutin/topia/security/TopiaSecurityException.java Fri Apr 29 16:00:39 2005 @@ -0,0 +1,53 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* TopiaSecurityException.java +* +* Created: 28 avr. 2005 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +*/ + + +package org.codelutin.topia.security; + +import org.codelutin.topia.TopiaException; + + +public class TopiaSecurityException extends TopiaException { + + /** + * @param msg + */ + public TopiaSecurityException(String msg) { + super(msg); + } + + + /** + * @param msg + * @param eee + */ + public TopiaSecurityException(String msg, Throwable eee) { + super(msg, eee); + } +} Index: topia/src/java/org/codelutin/topia/security/TopiaSimpleCallbackHandler.java diff -u /dev/null topia/src/java/org/codelutin/topia/security/TopiaSimpleCallbackHandler.java:1.1 --- /dev/null Fri Apr 29 16:00:44 2005 +++ topia/src/java/org/codelutin/topia/security/TopiaSimpleCallbackHandler.java Fri Apr 29 16:00:39 2005 @@ -0,0 +1,74 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* TopiaSimleCallbackHandler.java +* +* Created: 29 avr. 2005 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +*/ + + +package org.codelutin.topia.security; + +import java.io.IOException; + +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; + + +public class TopiaSimpleCallbackHandler implements CallbackHandler { + + private String username; + private String password; + + /** + * @param username + * @param password + */ + public TopiaSimpleCallbackHandler(String username, String password) { + super(); + this.username = username; + this.password = password; + } + + /* (non-Javadoc) + * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[]) + */ + public void handle(Callback[] callbacks) throws IOException, + UnsupportedCallbackException { + for (int i = 0; i< callbacks.length; i++) { + if (callbacks[i] instanceof NameCallback) { + NameCallback nc = (NameCallback)callbacks[i]; + nc.setName(username); + } else if (callbacks[i] instanceof PasswordCallback) { + PasswordCallback pc = (PasswordCallback)callbacks[i]; + pc.setPassword(password.toCharArray()); + } else throw new UnsupportedCallbackException(callbacks[i]); + } + + } + +} Index: topia/src/java/org/codelutin/topia/security/TopiaUserPrincipal.java diff -u /dev/null topia/src/java/org/codelutin/topia/security/TopiaUserPrincipal.java:1.1 --- /dev/null Fri Apr 29 16:00:44 2005 +++ topia/src/java/org/codelutin/topia/security/TopiaUserPrincipal.java Fri Apr 29 16:00:39 2005 @@ -0,0 +1,38 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* UserPrincipal.java +* +* Created: 29 avr. 2005 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +*/ + + +package org.codelutin.topia.security; + + +public class TopiaUserPrincipal extends TopiaPrincipal { + public TopiaUserPrincipal(String name) { + this.name = "user." + name; + } +}