Search in sources :

Example 1 with Handshake

use of com.juick.xmpp.extensions.Handshake in project com.juick.xmpp by juick.

the class StreamComponentServer method handshake.

@Override
public void handshake() throws XmlPullParserException, IOException {
    parser.next();
    if (!parser.getName().equals("stream") || !parser.getNamespace(null).equals(NS_COMPONENT_ACCEPT) || !parser.getNamespace("stream").equals(NS_STREAM)) {
        throw new IOException("invalid stream");
    }
    Jid domain = Jid.of(parser.getAttributeValue(null, "to"));
    if (listenersStream.stream().anyMatch(l -> l.filter(null, domain))) {
        send(new XMPPError(XMPPError.Type.cancel, "forbidden").toString());
        throw new IOException("invalid domain");
    }
    from = domain;
    to = domain;
    send(String.format("<stream:stream xmlns:stream='%s' " + "xmlns='%s' from='%s' id='%s'>", NS_STREAM, NS_COMPONENT_ACCEPT, from.asBareJid().toEscapedString(), streamId));
    Handshake handshake = Handshake.parse(parser);
    boolean authenticated = handshake.getValue().equals(DigestUtils.sha1Hex(streamId + secret));
    setLoggedIn(authenticated);
    if (!authenticated) {
        send(new XMPPError(XMPPError.Type.cancel, "not-authorized").toString());
        for (StreamListener streamListener : listenersStream) {
            streamListener.fail(new IOException("stream:stream, failed authentication"));
        }
        return;
    }
    send(new Handshake().toString());
    listenersStream.forEach(StreamListener::ready);
}
Also used : Jid(rocks.xmpp.addr.Jid) IOException(java.io.IOException) XMPPError(com.juick.xmpp.extensions.XMPPError) Handshake(com.juick.xmpp.extensions.Handshake)

Aggregations

Handshake (com.juick.xmpp.extensions.Handshake)1 XMPPError (com.juick.xmpp.extensions.XMPPError)1 IOException (java.io.IOException)1 Jid (rocks.xmpp.addr.Jid)1