1 36 37 package org.columba.ristretto.auth.mechanism; 38 39 import java.lang.reflect.InvocationTargetException ; 40 import java.lang.reflect.Method ; 41 import java.util.Map ; 42 43 import javax.security.auth.callback.Callback ; 44 import javax.security.auth.callback.CallbackHandler ; 45 46 53 public class SaslWrapper { 54 55 private static final boolean JAVA_14 = System.getProperty("java.version").matches("1\\.4.*"); 56 57 private static final String PACKAGE_14 = "com.sun.security.sasl.preview."; 58 private static final String PACKAGE_15 = "javax.security.sasl."; 59 60 String packageString; 61 Class saslClient; 62 Class sasl; 63 64 Object saslClientInstance; 65 66 public SaslWrapper() throws Exception { 67 68 if( JAVA_14 ) { 69 packageString = PACKAGE_14; 70 } else { 71 packageString = PACKAGE_15; 72 } 73 74 saslClient = Class.forName(packageString + "SaslClient"); 75 sasl = Class.forName(packageString + "Sasl"); 76 77 } 78 79 public static boolean available() { 80 if( JAVA_14 ) { 81 try { 82 Class.forName(PACKAGE_14 + "Sasl"); 83 } catch (ClassNotFoundException e) { 84 return false; 85 } 86 } 87 88 return true; 89 } 90 91 public void createClient(String [] type, String user, String service, String hostname, Map map, CallbackHandler handler) throws IllegalArgumentException , IllegalAccessException , InvocationTargetException , SecurityException , NoSuchMethodException { 92 Method createSaslClient = sasl.getMethod("createSaslClient",new Class [] {String [].class 93 , String .class, String .class, String .class, Map .class, CallbackHandler .class 94 }); 95 96 saslClientInstance = createSaslClient.invoke(null, new Object [] {type, user, service, hostname, map, handler}); 97 } 98 99 public byte[] evaluateChallenge(byte[] challenge ) throws SecurityException , NoSuchMethodException , IllegalArgumentException , IllegalAccessException , InvocationTargetException { 100 Method evaluateChallenge = saslClient.getMethod("evaluateChallenge", new Class [] {byte[].class}); 101 102 Object result = evaluateChallenge.invoke(saslClientInstance, new Object [] {challenge}); 103 104 return (byte[]) result; 105 } 106 107 public boolean isComplete() throws SecurityException , NoSuchMethodException , IllegalArgumentException , IllegalAccessException , InvocationTargetException { 108 Method evaluateChallenge = saslClient.getMethod("isComplete", new Class [] {}); 109 110 Boolean result = (Boolean ) evaluateChallenge.invoke(saslClientInstance, new Object [0]); 111 112 return result.booleanValue(); 113 } 114 115 public boolean handle(Callback callback) { 116 try { 117 Class realmCallback = Class.forName(packageString + "RealmCallback"); 118 119 if( realmCallback.equals(callback.getClass())) { 120 Method setText = realmCallback.getMethod("setText", new Class [] {String .class}); 121 Method getDefaultText = realmCallback.getMethod("getDefaultText", new Class [0]); 122 123 setText.invoke(callback, new Object [] {getDefaultText.invoke(callback, new Object [0])}); 124 return true; 125 } 126 127 Class realmChoiceCallback = Class.forName(packageString + "RealmChoiceCallback"); 128 129 if( realmChoiceCallback.equals(callback.getClass())) { 130 Method setSelectedIndex = realmChoiceCallback.getMethod("setSelectedIndex", new Class [] {int.class}); 131 Method getDefaultChoice = realmChoiceCallback.getMethod("getDefaultChoice", new Class [0]); 132 133 setSelectedIndex.invoke(callback, new Object [] {getDefaultChoice.invoke(callback, new Object [0])}); 134 return true; 135 } 136 } catch (Exception e) { 137 return false; 138 } 139 140 return false; 141 } 142 143 } 144 | Popular Tags |