Line data Source code
1 : import type { LocationQuery } from 'vue-router'
2 :
3 2356 : const OAUTH_REQUEST_ID_STORAGE_KEY = 'Comfy.OAuthRequestId'
4 2356 : const UUID_PATTERN =
5 : /^[0-9a-f]{8}-[0-9a-f]{4}-[1-7][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$/i
6 :
7 3983 : function readQueryString(value: unknown): string | null {
8 3983 : return typeof value === 'string' ? value : null
9 : }
10 :
11 0 : export function isOAuthRequestId(value: string): boolean {
12 0 : return UUID_PATTERN.test(value)
13 : }
14 :
15 3983 : export function captureOAuthRequestId(query: LocationQuery): string | null {
16 : // The router guard calls this on every navigation. We can't unconditionally
17 : // clear on absence — the OAuth return-trip from a social-login provider
18 : // (Google / GitHub) arrives at /login with `code` + `state` in the query
19 : // but no `oauth_request_id`, and we need the previously-captured value to
20 : // survive that hop.
21 : //
22 : // We DO clear on an explicitly invalid value (present but malformed): that
23 : // shape is either a stale deep-link or probing, and a stale Comfy.OAuthRequestId
24 : // contaminating later flows is worse than dropping the bad input.
25 3983 : const raw: unknown = query.oauth_request_id
26 3983 : const value = readQueryString(raw)
27 3983 : if (!value) {
28 3983 : if (raw !== undefined) {
29 : // Present but non-string (e.g. repeated `?oauth_request_id=a&oauth_request_id=b`).
30 0 : sessionStorage.removeItem(OAUTH_REQUEST_ID_STORAGE_KEY)
31 : }
32 3983 : return null
33 : }
34 0 : if (!isOAuthRequestId(value)) {
35 0 : sessionStorage.removeItem(OAUTH_REQUEST_ID_STORAGE_KEY)
36 0 : return null
37 : }
38 :
39 0 : sessionStorage.setItem(OAUTH_REQUEST_ID_STORAGE_KEY, value)
40 0 : return value
41 : }
42 :
43 0 : export function getOAuthRequestId(): string | null {
44 0 : const value = sessionStorage.getItem(OAUTH_REQUEST_ID_STORAGE_KEY)
45 0 : return value && isOAuthRequestId(value) ? value : null
46 : }
47 :
48 0 : export function clearOAuthRequestId(): void {
49 0 : sessionStorage.removeItem(OAUTH_REQUEST_ID_STORAGE_KEY)
50 : }
|