Source for gnu.javax.crypto.sasl.plain.PlainAuthInfoProvider

   1: /* PlainAuthInfoProvider.java --
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.javax.crypto.sasl.plain;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.javax.crypto.sasl.IAuthInfoProvider;
  43: import gnu.javax.crypto.sasl.NoSuchUserException;
  44: 
  45: import java.io.IOException;
  46: import java.util.HashMap;
  47: import java.util.Map;
  48: 
  49: import javax.security.sasl.AuthenticationException;
  50: 
  51: /**
  52:  * The PLAIN mechanism authentication information provider implementation.
  53:  */
  54: public class PlainAuthInfoProvider
  55:     implements IAuthInfoProvider, PlainRegistry
  56: {
  57:   private PasswordFile passwordFile = null;
  58: 
  59:   // implicit 0-args constrcutor
  60: 
  61:   public void activate(Map context) throws AuthenticationException
  62:   {
  63:     try
  64:       {
  65:         if (context == null)
  66:           passwordFile = new PasswordFile();
  67:         else
  68:           {
  69:             String pfn = (String) context.get(PASSWORD_FILE);
  70:             if (pfn == null)
  71:               passwordFile = new PasswordFile();
  72:             else
  73:               passwordFile = new PasswordFile(pfn);
  74:           }
  75:       }
  76:     catch (IOException x)
  77:       {
  78:         throw new AuthenticationException("activate()", x);
  79:       }
  80:   }
  81: 
  82:   public void passivate() throws AuthenticationException
  83:   {
  84:     passwordFile = null;
  85:   }
  86: 
  87:   public boolean contains(String userName) throws AuthenticationException
  88:   {
  89:     if (passwordFile == null)
  90:       throw new AuthenticationException("contains()",
  91:                                         new IllegalStateException());
  92:     boolean result = false;
  93:     try
  94:       {
  95:         result = passwordFile.contains(userName);
  96:       }
  97:     catch (IOException x)
  98:       {
  99:         throw new AuthenticationException("contains()", x);
 100:       }
 101:     return result;
 102:   }
 103: 
 104:   public Map lookup(Map userID) throws AuthenticationException
 105:   {
 106:     if (passwordFile == null)
 107:       throw new AuthenticationException("lookup()", new IllegalStateException());
 108:     Map result = new HashMap();
 109:     try
 110:       {
 111:         String userName = (String) userID.get(Registry.SASL_USERNAME);
 112:         if (userName == null)
 113:           throw new NoSuchUserException("");
 114:         String[] data = passwordFile.lookup(userName);
 115:         result.put(Registry.SASL_USERNAME, data[0]);
 116:         result.put(Registry.SASL_PASSWORD, data[1]);
 117:         result.put(UID_FIELD, data[2]);
 118:         result.put(GID_FIELD, data[3]);
 119:         result.put(GECOS_FIELD, data[4]);
 120:         result.put(DIR_FIELD, data[5]);
 121:         result.put(SHELL_FIELD, data[6]);
 122:       }
 123:     catch (Exception x)
 124:       {
 125:         if (x instanceof AuthenticationException)
 126:           throw (AuthenticationException) x;
 127:         throw new AuthenticationException("lookup()", x);
 128:       }
 129:     return result;
 130:   }
 131: 
 132:   public void update(Map userCredentials) throws AuthenticationException
 133:   {
 134:     if (passwordFile == null)
 135:       throw new AuthenticationException("update()", new IllegalStateException());
 136:     try
 137:       {
 138:         String userName = (String) userCredentials.get(Registry.SASL_USERNAME);
 139:         String password = (String) userCredentials.get(Registry.SASL_PASSWORD);
 140:         String uid = (String) userCredentials.get(UID_FIELD);
 141:         String gid = (String) userCredentials.get(GID_FIELD);
 142:         String gecos = (String) userCredentials.get(GECOS_FIELD);
 143:         String dir = (String) userCredentials.get(DIR_FIELD);
 144:         String shell = (String) userCredentials.get(SHELL_FIELD);
 145:         if (uid == null || gid == null || gecos == null || dir == null
 146:             || shell == null)
 147:           passwordFile.changePasswd(userName, password);
 148:         else
 149:           {
 150:             String[] attributes = new String[] { uid, gid, gecos, dir, shell };
 151:             passwordFile.add(userName, password, attributes);
 152:           }
 153:       }
 154:     catch (Exception x)
 155:       {
 156:         if (x instanceof AuthenticationException)
 157:           throw (AuthenticationException) x;
 158:         throw new AuthenticationException("update()", x);
 159:       }
 160:   }
 161: 
 162:   public Map getConfiguration(String mode) throws AuthenticationException
 163:   {
 164:     throw new AuthenticationException("", new UnsupportedOperationException());
 165:   }
 166: }