git » sdk » commit a7adec9

Implement XEP-0070

author Stephen Paul Weber
2026-10-20 17:59:11 UTC
committer Stephen Paul Weber
2026-10-26 18:45:19 UTC
parent ab1020dbf47ff113b3dff3ba7a7267c953cf5015

Implement XEP-0070

With rate limits at 5, 15, and 60 minute sliding windows

borogove/Client.hx +46 -0
borogove/LoginRequest.hx +52 -0
borogove/Message.hx +12 -0
borogove/Util.hx +4 -0
npm/index.ts +1 -0
test/TestClient.hx +98 -0

diff --git a/borogove/Client.hx b/borogove/Client.hx
index 665e24f..e5ba7b6 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -134,6 +134,7 @@ class Client extends EventEmitter {
 	private var firstSync = true;
 	@:allow(borogove.Channel)
 	private final channelPinger: ChannelPinger;
+	private final loginRequestRateLimit: Array<std.Date> = [];
 
 	/**
 		Create a new Client to connect to a particular account
@@ -591,6 +592,23 @@ class Client extends EventEmitter {
 						this.trigger("chats/update", [channel]);
 					});
 				}
+			case LoginRequest(req):
+				final tooOld = Date.format(
+					DateTools.delta(std.Date.now(), DateTools.minutes(-5))
+				);
+				while (loginRequestRateLimit.length > 0 && loginRequestRateLimit[0].olderThan(DateTools.minutes(60))) {
+					loginRequestRateLimit.shift();
+				}
+				final lastMinute = loginRequestRateLimit.filter(d -> !d.olderThan(DateTools.minutes(1))).length;
+				final last15Mins = loginRequestRateLimit.filter(d -> !d.olderThan(DateTools.minutes(15))).length;
+				if (req.timeSent > tooOld) {
+					if (lastMinute < 5 && last15Mins < 10 && loginRequestRateLimit.length < 20) {
+						loginRequestRateLimit.push(std.Date.now());
+						trigger("message/login", req);
+					}
+				} else {
+					rejectLoginRequest(req, "Expired");
+				}
 			default:
 				// ignore
 				trace("Ignoring non-chat message: " + stanza.toString());
@@ -1884,6 +1902,34 @@ class Client extends EventEmitter {
 	}
 #end
 
+	/**
+		Event fired when the user is trying to login to a website
+
+		@param handler takes one argument, the LoginRequest
+		@returns token for use with removeEventListener
+	**/
+	@:HaxeSwiftBridge.contextLifetime(handler, EventEmitter)
+	public function addLoginRequestListener(handler: (LoginRequest)->Void) {
+		return this.on("message/login", (req) -> {
+			handler(req);
+			return EventHandled;
+		});
+	}
+
+	/**
+		Approve a LoginRequest to a website
+	**/
+	public function approveLoginRequest(req: LoginRequest) {
+		sendStanza(req.approveStanza());
+	}
+
+	/**
+		Reject a LoginRequest to a website
+	**/
+	public function rejectLoginRequest(req: LoginRequest, reason: String) {
+		sendStanza(req.rejectStanza(reason));
+	}
+
 	/**
 		Let the SDK know the UI is in the foreground
 	**/
diff --git a/borogove/LoginRequest.hx b/borogove/LoginRequest.hx
new file mode 100644
index 0000000..30eecee
--- /dev/null
+++ b/borogove/LoginRequest.hx
@@ -0,0 +1,52 @@
+package borogove;
+
+@:nullSafety(Strict)
+@:expose
+class LoginRequest {
+	@:allow(borogove)
+	private final from: JID;
+	@:allow(borogove)
+	private final threadId: String;
+	public final id: String;
+	public final method: String;
+	public final url: String;
+	public final timeSent: String;
+
+	@:allow(borogove)
+	private function new(from: JID, threadId: String, id: String, method: String, url: String, timeSent: String) {
+		this.from = from;
+		this.threadId = threadId;
+		this.id = id;
+		this.method = method;
+		this.url = url;
+		this.timeSent = timeSent;
+	}
+
+	@:allow(borogove)
+	private function approveStanza() {
+		return new Stanza("message", { to: from.asString() })
+			.textTag("thread", threadId)
+			.tag("confirm", {
+				xmlns: "http://jabber.org/protocol/http-auth",
+				id: id,
+				method: method,
+				url: url
+			}).up();
+	}
+
+	@:allow(borogove)
+	private function rejectStanza(reason: String) {
+		return new Stanza("message", { to: from.asString(), type: "error" })
+			.textTag("thread", threadId)
+			.tag("confirm", {
+				xmlns: "http://jabber.org/protocol/http-auth",
+				id: id,
+				method: method,
+				url: url
+			}).up()
+			.tag("error", { code: "401", type: "auth" })
+				.tag("not-authorized", { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" }).up()
+				.textTag("text", reason, { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" })
+				.up();
+	}
+}
diff --git a/borogove/Message.hx b/borogove/Message.hx
index 064ad16..ea4cdff 100644
--- a/borogove/Message.hx
+++ b/borogove/Message.hx
@@ -41,6 +41,7 @@ enum MessageStanza {
 	ReactionUpdateStanza(update: ReactionUpdate);
 	MucInviteStanza(serverId: Null<String>, serverIdBy: Null<String>, reason: Null<String>, password: Null<String>);
 	MucVoiceRequest(jid: JID);
+	LoginRequest(req: LoginRequest);
 	UnknownMessageStanza(stanza: Stanza);
 }
 
@@ -249,6 +250,17 @@ class Message {
 		if (addContext != null) msg = addContext(msg, stanza);
 		final timeSent = msg.timeSent ?? msg.timeReceived ?? Date.format(std.Date.now());
 
+		final xep0070 = stanza.getChild("confirm", "http://jabber.org/protocol/http-auth");
+		if (xep0070 != null) {
+			final threadId = msg.threadId;
+			final id = xep0070.attr.get("id");
+			final method = xep0070.attr.get("method");
+			final url = xep0070.attr.get("url");
+			if (from != null && threadId != null && id != null && method != null && url != null) {
+				return new Message(msg.chatId(), msg.senderId, threadId, LoginRequest(new LoginRequest(from, threadId, id, method, url, timeSent)), encryptionInfo);
+			}
+		}
+
 		if (jmi != null && jmi.name != "accept" && jmi.name != "propose") {
 			msg.versions = [msg.build()];
 		}
diff --git a/borogove/Util.hx b/borogove/Util.hx
index 8c92643..d593b03 100644
--- a/borogove/Util.hx
+++ b/borogove/Util.hx
@@ -251,4 +251,8 @@ class Util {
 
 		return first + rest;
 	}
+
+	static public function olderThan(d: std.Date, delta: Float) {
+		return d.getTime() < DateTools.delta(std.Date.now(), -1*delta).getTime();
+	}
 }
diff --git a/npm/index.ts b/npm/index.ts
index ee1e251..f8a9973 100644
--- a/npm/index.ts
+++ b/npm/index.ts
@@ -35,6 +35,7 @@ export {
 	borogove_Identicon as Identicon,
 	borogove_Importer as Importer,
 	borogove_LinkMetadata as LinkMetadata,
+	borogove_LoginRequest as LoginRequest,
 	borogove_Notification as Notification,
 	borogove_Member as Member,
 	borogove_Profile as Profile,
diff --git a/test/TestClient.hx b/test/TestClient.hx
index 0bb6ea9..3812c6e 100644
--- a/test/TestClient.hx
+++ b/test/TestClient.hx
@@ -10,6 +10,7 @@ import borogove.ChatMessageBuilder;
 import borogove.Client;
 import borogove.Date;
 import borogove.JID;
+import borogove.LoginRequest;
 import borogove.Member;
 import borogove.MemberUpdate;
 import borogove.Message;
@@ -244,6 +245,103 @@ class TestClient extends utest.Test {
 		return builder.build();
 	}
 
+	private function loginRequestStanza(ageMinutes: Float, id: String): Stanza {
+		final stamp = Date.format(DateTools.delta(std.Date.now(), -DateTools.minutes(ageMinutes)));
+		return new Stanza("message", { xmlns: "jabber:client", from: "site@example.com", type: "chat", id: id })
+			.textTag("thread", "login-thread")
+			.tag("delay", { xmlns: "urn:xmpp:delay", stamp: stamp }).up()
+			.tag("confirm", {
+				xmlns: "http://jabber.org/protocol/http-auth",
+				id: "confirm-" + id,
+				method: "GET",
+				url: "https://example.com/login"
+			}).up();
+	}
+
+	public function testLoginRateLimitAllowsFiveRequestsPerMinute() {
+		final client = new Client("test@example.com", new Dummy());
+		final received = new Array<LoginRequest>();
+		client.addLoginRequestListener(req -> received.push(req));
+		for (i in 0...4) {
+			client.loginRequestRateLimit.push(std.Date.now());
+		}
+
+		client.stream.onStanza(loginRequestStanza(0, "first"));
+		Assert.equals(1, received.length);
+
+		// A sixth request within the same minute is silently dropped
+		client.stream.onStanza(loginRequestStanza(0, "second"));
+		Assert.equals(1, received.length);
+		Assert.equals(5, client.loginRequestRateLimit.length);
+	}
+
+	public function testLoginRateLimitDoesNotCountRequestsOlderThanAMinute() {
+		final client = new Client("test@example.com", new Dummy());
+		final received = new Array<LoginRequest>();
+		client.addLoginRequestListener(req -> received.push(req));
+		for (i in 0...5) {
+			client.loginRequestRateLimit.push(DateTools.delta(std.Date.now(), -DateTools.minutes(2)));
+		}
+
+		client.stream.onStanza(loginRequestStanza(0, "fresh"));
+		Assert.equals(1, received.length);
+	}
+
+	public function testLoginRateLimitAllowsTenRequestsPerFifteenMinutes() {
+		final client = new Client("test@example.com", new Dummy());
+		final received = new Array<LoginRequest>();
+		client.addLoginRequestListener(req -> received.push(req));
+		for (i in 0...9) {
+			client.loginRequestRateLimit.push(DateTools.delta(std.Date.now(), -DateTools.minutes(6)));
+		}
+
+		client.stream.onStanza(loginRequestStanza(0, "first"));
+		Assert.equals(1, received.length);
+
+		client.stream.onStanza(loginRequestStanza(0, "second"));
+		Assert.equals(1, received.length);
+		Assert.equals(10, client.loginRequestRateLimit.length);
+	}
+
+	public function testLoginRateLimitAllowsTwentyRequestsPerHour() {
+		final client = new Client("test@example.com", new Dummy());
+		final received = new Array<LoginRequest>();
+		client.addLoginRequestListener(req -> received.push(req));
+		for (i in 0...19) {
+			client.loginRequestRateLimit.push(DateTools.delta(std.Date.now(), -DateTools.minutes(20)));
+		}
+
+		client.stream.onStanza(loginRequestStanza(0, "first"));
+		Assert.equals(1, received.length);
+
+		client.stream.onStanza(loginRequestStanza(0, "second"));
+		Assert.equals(1, received.length);
+		Assert.equals(20, client.loginRequestRateLimit.length);
+	}
+
+	public function testLoginRateLimitForgetsRequestsOlderThanAnHour() {
+		final client = new Client("test@example.com", new Dummy());
+		final received = new Array<LoginRequest>();
+		client.addLoginRequestListener(req -> received.push(req));
+		for (i in 0...20) {
+			client.loginRequestRateLimit.push(DateTools.delta(std.Date.now(), -DateTools.minutes(61)));
+		}
+
+		client.stream.onStanza(loginRequestStanza(0, "fresh"));
+		Assert.equals(1, received.length);
+		Assert.equals(1, client.loginRequestRateLimit.length);
+	}
+
+	public function testLoginRequestExpiredAfterFiveMinutes() {
+		final client = new Client("test@example.com", new Dummy());
+		final received = new Array<LoginRequest>();
+		client.addLoginRequestListener(req -> received.push(req));
+
+		client.stream.onStanza(loginRequestStanza(10, "expired"));
+		Assert.equals(0, received.length);
+		Assert.equals(0, client.loginRequestRateLimit.length);
+	}
+
 	@:timeout(3000)
 	public function testPrepareAttachmentNoUploadService(async: Async) {
 		final client = new Client("test@example.com", new Dummy());