All files / src extract-user-info.ts

94.59% Statements 70/74
78.12% Branches 25/32
100% Functions 8/8
94.59% Lines 70/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94              1x 16x 16x 16x 16x 2x 2x 16x 1x 1x             1x 7x 7x 7x 7x 7x   23x 23x 23x 23x 8x 9x 8x 8x 8x 9x     23x   8x 8x 8x 8x 6x 6x 7x     8x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 2x 2x   18x 18x 7x 18x 18x   2x 2x 2x 2x 2x 2x 2x   1x 1x 1x 1x 1x  
import type { AuthResult } from "@dataflint/mcp-server";
 
export interface SignedInUser {
    name?: string;
    email?: string;
}
 
export function extractSignedInUser(
    auth: AuthResult | undefined
): SignedInUser | undefined {
    const claims = decodeIdTokenClaims(auth);
    if (!claims) return undefined;
    const email = pickString(claims.email);
    const name = pickName(claims, email);
    if (!name && !email) return undefined;
    return { name, email };
}
 
/**
 * Opaque JWT `sub` for telemetry distinct-counting. Separate from
 * extractSignedInUser on purpose: a sub-only token has no displayable
 * identity for the UI but is still a countable user.
 */
export function extractSubFromSecret(
    rawSecret: string | undefined
): string | undefined {
    const claims = decodeIdTokenClaims(parseAuthSecret(rawSecret));
    return claims ? pickString(claims.sub) : undefined;
}
 
function decodeIdTokenClaims(
    auth: AuthResult | undefined
): Record<string, unknown> | undefined {
    if (!auth?.idToken) return undefined;
    const parts = auth.idToken.split(".");
    if (parts.length !== 3) return undefined;
    try {
        const decoded = Buffer.from(parts[1], "base64url").toString("utf8");
        return JSON.parse(decoded);
    } catch {
        return undefined;
    }
}
 
function parseAuthSecret(
    rawSecret: string | undefined
): AuthResult | undefined {
    if (!rawSecret) return undefined;
    try {
        return JSON.parse(rawSecret) as AuthResult;
    } catch {
        return undefined;
    }
}
 
function pickName(
    claims: Record<string, unknown>,
    email: string | undefined
): string | undefined {
    const candidates: (string | undefined)[] = [
        pickString(claims.name),
        joinNames(
            pickString(claims.given_name),
            pickString(claims.family_name)
        ),
        pickString(claims.nickname),
        pickString(claims.preferred_username),
    ];
    for (const candidate of candidates) {
        if (candidate && candidate !== email) return candidate;
    }
    return undefined;
}
 
function pickString(value: unknown): string | undefined {
    if (typeof value !== "string") return undefined;
    const trimmed = value.trim();
    return trimmed === "" ? undefined : trimmed;
}
 
function joinNames(
    given: string | undefined,
    family: string | undefined
): string | undefined {
    const joined = [given, family].filter(Boolean).join(" ").trim();
    return joined === "" ? undefined : joined;
}
 
export function extractSignedInUserFromSecret(
    rawSecret: string | undefined
): SignedInUser | undefined {
    return extractSignedInUser(parseAuthSecret(rawSecret));
}