Source for gnu.javax.crypto.sasl.srp.SRPAuthInfoProvider

   1: /* SRPAuthInfoProvider.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.srp;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.java.security.util.Util;
  43: import gnu.javax.crypto.sasl.IAuthInfoProvider;
  44: import gnu.javax.crypto.sasl.NoSuchUserException;
  45: 
  46: import java.io.IOException;
  47: import java.util.HashMap;
  48: import java.util.Map;
  49: 
  50: import javax.security.sasl.AuthenticationException;
  51: 
  52: /**
  53:  * The SRP mechanism authentication information provider implementation.
  54:  */
  55: public class SRPAuthInfoProvider
  56:     implements IAuthInfoProvider
  57: {
  58:   private PasswordFile passwordFile = null;
  59: 
  60:   // implicit 0-args constrcutor
  61: 
  62:   public void activate(Map context) throws AuthenticationException
  63:   {
  64:     try
  65:       {
  66:         if (context == null)
  67:           passwordFile = new PasswordFile();
  68:         else
  69:           {
  70:             passwordFile = (PasswordFile) context.get(SRPRegistry.PASSWORD_DB);
  71:             if (passwordFile == null)
  72:               {
  73:                 String pfn = (String) context.get(SRPRegistry.PASSWORD_FILE);
  74:                 if (pfn == null)
  75:                   passwordFile = new PasswordFile();
  76:                 else
  77:                   passwordFile = new PasswordFile(pfn);
  78:               }
  79:           }
  80:       }
  81:     catch (IOException x)
  82:       {
  83:         throw new AuthenticationException("activate()", x);
  84:       }
  85:   }
  86: 
  87:   public void passivate() throws AuthenticationException
  88:   {
  89:     passwordFile = null;
  90:   }
  91: 
  92:   public boolean contains(String userName) throws AuthenticationException
  93:   {
  94:     if (passwordFile == null)
  95:       throw new AuthenticationException("contains()",
  96:                                         new IllegalStateException());
  97:     boolean result = false;
  98:     try
  99:       {
 100:         result = passwordFile.contains(userName);
 101:       }
 102:     catch (IOException x)
 103:       {
 104:         throw new AuthenticationException("contains()", x);
 105:       }
 106:     return result;
 107:   }
 108: 
 109:   public Map lookup(Map userID) throws AuthenticationException
 110:   {
 111:     if (passwordFile == null)
 112:       throw new AuthenticationException("lookup()", new IllegalStateException());
 113:     Map result = new HashMap();
 114:     try
 115:       {
 116:         String userName = (String) userID.get(Registry.SASL_USERNAME);
 117:         if (userName == null)
 118:           throw new NoSuchUserException("");
 119:         String mdName = (String) userID.get(SRPRegistry.MD_NAME_FIELD);
 120:         String[] data = passwordFile.lookup(userName, mdName);
 121:         result.put(SRPRegistry.USER_VERIFIER_FIELD, data[0]);
 122:         result.put(SRPRegistry.SALT_FIELD, data[1]);
 123:         result.put(SRPRegistry.CONFIG_NDX_FIELD, data[2]);
 124:       }
 125:     catch (Exception x)
 126:       {
 127:         if (x instanceof AuthenticationException)
 128:           throw (AuthenticationException) x;
 129:         throw new AuthenticationException("lookup()", x);
 130:       }
 131:     return result;
 132:   }
 133: 
 134:   public void update(Map userCredentials) throws AuthenticationException
 135:   {
 136:     if (passwordFile == null)
 137:       throw new AuthenticationException("update()", new IllegalStateException());
 138:     try
 139:       {
 140:         String userName = (String) userCredentials.get(Registry.SASL_USERNAME);
 141:         String password = (String) userCredentials.get(Registry.SASL_PASSWORD);
 142:         String salt = (String) userCredentials.get(SRPRegistry.SALT_FIELD);
 143:         String config = (String) userCredentials.get(SRPRegistry.CONFIG_NDX_FIELD);
 144:         if (salt == null || config == null)
 145:           passwordFile.changePasswd(userName, password);
 146:         else
 147:           passwordFile.add(userName, password, Util.fromBase64(salt), config);
 148:       }
 149:     catch (Exception x)
 150:       {
 151:         if (x instanceof AuthenticationException)
 152:           throw (AuthenticationException) x;
 153:         throw new AuthenticationException("update()", x);
 154:       }
 155:   }
 156: 
 157:   public Map getConfiguration(String mode) throws AuthenticationException
 158:   {
 159:     if (passwordFile == null)
 160:       throw new AuthenticationException("getConfiguration()",
 161:                                         new IllegalStateException());
 162:     Map result = new HashMap();
 163:     try
 164:       {
 165:         String[] data = passwordFile.lookupConfig(mode);
 166:         result.put(SRPRegistry.SHARED_MODULUS, data[0]);
 167:         result.put(SRPRegistry.FIELD_GENERATOR, data[1]);
 168:       }
 169:     catch (Exception x)
 170:       {
 171:         if (x instanceof AuthenticationException)
 172:           throw (AuthenticationException) x;
 173:         throw new AuthenticationException("getConfiguration()", x);
 174:       }
 175:     return result;
 176:   }
 177: }