GenericClient.java [plain text]
package CyrusSasl;
import javax.security.auth.callback.*;
import java.io.*;
public class GenericClient extends GenericCommon implements SaslClient
{
private byte[]initial_response;
private String mechanism;
private boolean hasinitresp;
private javax.security.auth.callback.CallbackHandler cbh;
GenericClient(int cptr, String mechlist,
java.util.Hashtable props,
javax.security.auth.callback.CallbackHandler cbh)
{
ptr=cptr;
this.cbh = cbh;
super.setcommonproperties(props);
initial_response = jni_sasl_client_start(cptr, mechlist);
}
private native byte[] jni_sasl_client_start(int ptr,
String mechlist);
public byte[] evaluateChallenge(byte[] challenge) throws SaslException
{
byte[] out=null;
if (complete && challenge == null) {
return null;
}
if (challenge==null) {
out=jni_sasl_client_step(ptr, null, 0);
} else {
out=jni_sasl_client_step(ptr, challenge, challenge.length);
}
return out;
}
private native byte[] jni_sasl_client_step(int ptr,
byte[] in,
int inlen);
public boolean hasInitialResponse()
{
return hasinitresp;
}
public String getMechanismName()
{
return mechanism;
}
private void callback_setmechanism(String mech, int initresp)
{
mechanism = mech;
hasinitresp = initresp != 0;
}
private String userid;
private String authid;
private String password;
private String realm;
private void do_callbacks(int wantuid, int wantaid, int wantpass, int wantrealm) throws SaslException
{
int numcb = wantuid+wantaid+wantpass+wantrealm;
Callback[] cbs = new Callback[numcb];
int pos = 0;
NameCallback nc = null;
NameCallback nc2 = null;
PasswordCallback pc = null;
RealmCallback rc = null;
if (wantuid==1) {
nc = new NameCallback("Please enter your authorization id");
cbs[pos] = nc;
pos++;
}
if (wantaid==1) {
nc2 = new NameCallback("Please enter your authentication id");
cbs[pos] = nc2;
pos++;
}
if (wantpass==1) {
pc = new PasswordCallback("Please enter your password", false);
cbs[pos] = pc;
pos++;
}
if (wantrealm==1) {
rc = new RealmCallback("Please enter your realm");
cbs[pos] = rc;
pos++;
}
try {
cbh.handle(cbs);
} catch (UnsupportedCallbackException e) {
throw new SaslException("Unsupported callback",null);
} catch (IOException e) {
throw new SaslException("IO exception",null);
}
if (nc!=null) {
this.userid = nc.getName();
}
if (nc2!=null) {
this.authid = nc2.getName();
}
if (pc!=null) {
this.password = new String(pc.getPassword());
}
if (rc!=null) {
this.realm = rc.getRealm();
}
}
private String get_userid(int a)
{
return userid;
}
private String get_authid(int a)
{
return authid;
}
private String get_password(int a)
{
return password;
}
private String get_realm(int a)
{
return realm;
}
public InputStream getInputStream(InputStream source) throws IOException
{
if (getSecurity() > 0) {
return new SaslInputStream(source,this);
} else {
return source;
}
}
public OutputStream getOutputStream(OutputStream dest) throws IOException
{
if (getSecurity() > 0) {
return new SaslOutputStream(dest,this);
} else {
return dest;
}
}
public byte[] createInitialResponse(){
return initial_response;
}
}