CredentialCodec.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 
00026 package org.openmobileis.synchro.security.auth;
00027 
00028 import org.openmobileis.synchro.openmsp.OpenMSPException;
00029 import org.openmobileis.synchro.openmsp.protocol.*;
00030 
00031 import org.openmobileis.common.util.codec.GeneralCoder;
00032 
00043 public class CredentialCodec {
00044   private static final int BASIC_AUTH = 1;
00045   private static final int TEXT_FORMAT = 1;
00046   private static final int B64_FORMAT = 2;
00047 
00048   private static Credential getCredentialFromBasicAuth(String data)  {
00049     Credential cred = null;
00050     int index = data.indexOf(":");
00051     if (index != -1)  {
00052       try {
00053         cred = new Credential(data.substring(0, index), data.substring(index+1, data.length()));
00054       } catch (ArrayIndexOutOfBoundsException ex) {
00055       }
00056     }
00057     return cred;
00058   }
00059 
00060   public static Credential getOpenMSPCredential(String[] credential) throws OpenMSPException {
00061     if (credential == null)  return null;
00062     try {
00063       int authType = 0;
00064       int format = 0;
00065       ElementData[] credMeta = SimpleOpenMLParser.parseXML(credential[0]);
00066       for (int i=0; i<credMeta.length; i++)  {
00067         if (credMeta[i].getElement().equals("Type")) {
00068           if (credMeta[i].getData().equals("OpenMSP:auth-basic"))  {
00069             authType = CredentialCodec.BASIC_AUTH;
00070           }
00071         } else if (credMeta[i].getElement().equals("Format")) {
00072           //manage format
00073           if (credMeta[i].getData().equals("plain/text"))  {
00074             format = CredentialCodec.TEXT_FORMAT;
00075           } else if (credMeta[i].getData().equals("b64"))  {
00076             format = CredentialCodec.B64_FORMAT;
00077           }
00078         }
00079       }
00080 
00081       // get credential data ;
00082       if (( authType ==  CredentialCodec.BASIC_AUTH) && ( format== CredentialCodec.TEXT_FORMAT)) {
00083         return CredentialCodec.getCredentialFromBasicAuth(credential[1]);
00084       } else if (( authType ==  CredentialCodec.BASIC_AUTH) && ( format== CredentialCodec.B64_FORMAT)) {
00085         return CredentialCodec.getCredentialFromBasicAuth(new String(GeneralCoder.decodeBase64(credential[1].getBytes())));
00086       }
00087     } catch (java.io.IOException ex)  {
00088       throw new OpenMSPException(ex);
00089     }
00090     return null;
00091   }
00092 }

Generated on Mon Dec 4 11:03:25 2006 for OpenMobileIS by  doxygen 1.5.1-p1