📄 bsauthorizationbean.java
字号:
IQAuthBuilder getIQSetAuthBuilder(IQAuth resultExt,
String user, String password, String resource) {
IQAuthBuilder iqAuthBuilder = new IQAuthBuilder();
if (resultExt == null) return null;
// if 0k auth is supported
else if (resultExt.getZeroKToken() != null) {
String hash = null;
try {
SHA1Helper helper = new SHA1Helper();
// the server uses sequence and token to hash the password
String token = resultExt.getZeroKToken();
int sequence = resultExt.getZeroKSequence();
if (sequence <= 0) return null;
hash = helper.digest(null, password);
hash = helper.digest(null, hash + token);
for (int i=0; i<sequence; i++)
hash = helper.digest(null, hash);
} catch(InstantiationException e) {
System.out.println(e.toString());
return null;
}
iqAuthBuilder.setZeroKHash(hash);
}
// if digest auth is supported
else if (resultExt.getSHA1Digest() != null) {
String digest = null;
try {
SHA1Helper helper = new SHA1Helper();
// the server uses the session ID to hash and dehash the password
String sessionID = iqBean.getConnection().getSessionID();
digest = helper.digest(sessionID, password);
} catch(InstantiationException e) {
System.out.println(e.toString());
return null;
}
iqAuthBuilder.setSHA1Digest(digest);
}
// if plain auth is supported
else if (resultExt.getPassword() != null) {
iqAuthBuilder.setPassword(password);
}
else return null;
iqAuthBuilder.setUsername(user);
iqAuthBuilder.setResource(resource);
return iqAuthBuilder;
}
// *** packet handling ***
/**
* Invoked when a IQ packet is received.
*/
public void receivedPacket(PacketEvent pe) {
if (!(pe.getPacket() instanceof InfoQuery)) {
BSCore.logEvent(name, "warning: nonIQ packet received");
return;
}
InfoQuery iq = (InfoQuery) pe.getPacket();
//if no ID or packet with that ID not expected
if (state.servedID == null || !state.servedID.equals(iq.getIdentifier())) {
return;
}
if ((new String("result")).equals(iq.getType()))
handleResult(iq);
else if ((new String("error")).equals(iq.getType()))
handleError(iq);
else if ((new String("set")).equals(iq.getType()))
handleSet(iq);
}
/**
* Invoked when a IQ packet send failes.
*/
public void sendFailed(PacketEvent pe) {
}
/**
* Invoked when a IQ packet is sent.
*/
public void sentPacket(PacketEvent pe) {
}
// *** infoQuery handling ***
/**
* Handles <code>InfoQuery</code> packet, if it does contain an error.
* Before this is called it checks if that is response on the sent IQ
* authentication packet.
*/
private void handleError(InfoQuery iq) {
BSCore.logEvent(name, "error " + iq.getErrorCode() + ": " + iq.getErrorText());
setState(null, BSAuthState.NOT_AUTHORIZED, null);
}
/**
* Handles <code>InfoQuery</code> packet, if it does contain a result.
* Before this is called it checks if that is response on the sent IQ
* authentication packet.
*/
private void handleResult(InfoQuery iq) {
Enumeration extensions = iq.Extensions();
// when in first phase of authentication
if (state.value == BSAuthState.AUTHORIZING1) {
while (extensions != null && extensions.hasMoreElements()) {
Extension ext = (Extension) extensions.nextElement();
if (ext instanceof IQAuth)
sendPassword((IQAuth)ext);
else {
BSCore.logEvent(name, "error: unexpected IQ extension");
setState(null, BSAuthState.NOT_AUTHORIZED, null);
}
}
}
// when in second phase of authentication
else if (state.value == BSAuthState.AUTHORIZING2) {
if (extensions == null || !extensions.hasMoreElements()) {
BSCore.logEvent(name, "authenticated");
setState(null, BSAuthState.AUTHORIZED, null);
}
else {
BSCore.logEvent(name, "error: IQ extension not expected");
setState(null, BSAuthState.NOT_AUTHORIZED, null);
}
}
else {
BSCore.logEvent(name, "error: unexpected IQ result");
//servedID = null;
//setState(BSAuthState.NOT_AUTHORIZED);
}
}
/**
* Handles <code>InfoQuery</code> packet, if it IQ-set.
* Before this is called it checks if that is response on the sent IQ
* authentication packet.
*/
private void handleSet(InfoQuery iq) {
return;
}
/**
* Invoked when authentication succeeded.
*/
private void authorized() {
return;
}
// *** authorization listeners ***
/**
* Adds <code>BSAuthListener</code> to listeners notified when
* authentication state changes.
*
* @see #removeAuthListener
* @see #removeAllAuthListeners
* @see #notifyAuthListeners
*/
public void addAuthListener(BSAuthListener listener) {
//assert listener != null : listener;
if (listener == null) return;
if (!authListeners.contains(listener)) {
authListeners.addElement(listener);
}
}
/**
* Removes <code>BSAuthListener</code> to listeners notified when
* authentication state changes.
*
* @see #addAuthListener
* @see #removeAllAuthListeners
* @see #notifyAuthListeners
*/
public void removeAuthListener(BSAuthListener listener) {
//assert listener != null : listener;
if (listener == null) return;
authListeners.removeElement(listener);
}
/**
* Removes all listeners notified when authentication state changes.
* This can be used before to free dependencies and allow dispose of
* all objects.
*
* @see #addAuthListener
* @see #removeAuthListener
* @see #notifyAuthListeners
*/
public void removeAllAuthListeners() {
authListeners.clear();
}
/**
* Notifies authentication listeners when
* authentication state changes.
*
* @see #addAuthListeners
* @see #removeAuthListener
* @see #removeAllAuthListeners
*/
private void notifyAuthListeners(BSAuthEvent ae) {
for (Enumeration e = authListeners.elements(); e.hasMoreElements(); ) {
BSAuthListener listener = (BSAuthListener) e.nextElement();
switch (ae.getState().value) {
case BSAuthState.NOT_AUTHORIZED:
listener.authError(ae);
break;
case BSAuthState.AUTHORIZING1:
case BSAuthState.AUTHORIZING2:
listener.authorizing(ae);
break;
case BSAuthState.AUTHORIZED:
listener.authorized(ae);
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -