00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
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 }