refactor(auth): streamline allowFrom normalization
This commit is contained in:
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
isWhatsAppGroupJid,
|
||||
isWhatsAppUserJid,
|
||||
isWhatsAppUserTarget,
|
||||
normalizeWhatsAppTarget,
|
||||
} from "./normalize.js";
|
||||
|
||||
@@ -45,6 +45,7 @@ describe("normalizeWhatsAppTarget", () => {
|
||||
|
||||
it("normalizes LID JIDs to E.164", () => {
|
||||
expect(normalizeWhatsAppTarget("123456789@lid")).toBe("+123456789");
|
||||
expect(normalizeWhatsAppTarget("123456789@LID")).toBe("+123456789");
|
||||
});
|
||||
|
||||
it("rejects invalid targets", () => {
|
||||
@@ -52,6 +53,7 @@ describe("normalizeWhatsAppTarget", () => {
|
||||
expect(normalizeWhatsAppTarget("whatsapp:")).toBeNull();
|
||||
expect(normalizeWhatsAppTarget("@g.us")).toBeNull();
|
||||
expect(normalizeWhatsAppTarget("whatsapp:group:@g.us")).toBeNull();
|
||||
expect(normalizeWhatsAppTarget("abc@s.whatsapp.net")).toBeNull();
|
||||
});
|
||||
|
||||
it("handles repeated prefixes", () => {
|
||||
@@ -60,13 +62,16 @@ describe("normalizeWhatsAppTarget", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("isWhatsAppUserJid", () => {
|
||||
describe("isWhatsAppUserTarget", () => {
|
||||
it("detects user JIDs with various formats", () => {
|
||||
expect(isWhatsAppUserJid("41796666864:0@s.whatsapp.net")).toBe(true);
|
||||
expect(isWhatsAppUserJid("1234567890@s.whatsapp.net")).toBe(true);
|
||||
expect(isWhatsAppUserJid("123456789@lid")).toBe(true);
|
||||
expect(isWhatsAppUserJid("123456789-987654321@g.us")).toBe(false);
|
||||
expect(isWhatsAppUserJid("+1555123")).toBe(false);
|
||||
expect(isWhatsAppUserTarget("41796666864:0@s.whatsapp.net")).toBe(true);
|
||||
expect(isWhatsAppUserTarget("1234567890@s.whatsapp.net")).toBe(true);
|
||||
expect(isWhatsAppUserTarget("123456789@lid")).toBe(true);
|
||||
expect(isWhatsAppUserTarget("123456789@LID")).toBe(true);
|
||||
expect(isWhatsAppUserTarget("123@lid:0")).toBe(false);
|
||||
expect(isWhatsAppUserTarget("abc@s.whatsapp.net")).toBe(false);
|
||||
expect(isWhatsAppUserTarget("123456789-987654321@g.us")).toBe(false);
|
||||
expect(isWhatsAppUserTarget("+1555123")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -25,9 +25,9 @@ export function isWhatsAppGroupJid(value: string): boolean {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if value looks like a WhatsApp user JID (e.g. "41796666864:0@s.whatsapp.net" or "123@lid").
|
||||
* Check if value looks like a WhatsApp user target (e.g. "41796666864:0@s.whatsapp.net" or "123@lid").
|
||||
*/
|
||||
export function isWhatsAppUserJid(value: string): boolean {
|
||||
export function isWhatsAppUserTarget(value: string): boolean {
|
||||
const candidate = stripWhatsAppTargetPrefixes(value);
|
||||
return WHATSAPP_USER_JID_RE.test(candidate) || WHATSAPP_LID_RE.test(candidate);
|
||||
}
|
||||
@@ -53,7 +53,7 @@ export function normalizeWhatsAppTarget(value: string): string | null {
|
||||
return `${localPart}@g.us`;
|
||||
}
|
||||
// Handle user JIDs (e.g. "41796666864:0@s.whatsapp.net")
|
||||
if (isWhatsAppUserJid(candidate)) {
|
||||
if (isWhatsAppUserTarget(candidate)) {
|
||||
const phone = extractUserJidPhone(candidate);
|
||||
if (!phone) return null;
|
||||
const normalized = normalizeE164(phone);
|
||||
|
||||
Reference in New Issue
Block a user