@omnikit-ai/sdk 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Authentication utility functions for Omnikit SDK
3
+ * Provides helpers for token management
4
+ */
5
+ /**
6
+ * Set the app-specific localStorage key for tokens
7
+ * This ensures each app has its own isolated token storage
8
+ * Called by APIClient during initialization
9
+ * @param appId - The app ID to use for the storage key
10
+ */
11
+ declare function setAccessTokenKey(appId: string): void;
12
+ /**
13
+ * Check if token exists in URL parameters
14
+ * @returns True if token is present in URL
15
+ */
16
+ declare function isTokenInUrl(): boolean;
17
+ /**
18
+ * Remove token from URL without page reload
19
+ * Cleans up the URL after token has been saved to localStorage
20
+ */
21
+ declare function cleanTokenFromUrl(): void;
22
+ /**
23
+ * Remove OAuth token from URL hash without page reload
24
+ * Cleans up the hash after token has been saved to localStorage
25
+ */
26
+ declare function cleanTokenFromHash(): void;
27
+ /**
28
+ * Save access token to localStorage
29
+ * @param token - The access token to save
30
+ */
31
+ declare function saveAccessToken(token: string): void;
32
+ /**
33
+ * Remove access token from localStorage
34
+ * Call this on logout to clear the saved token
35
+ */
36
+ declare function removeAccessToken(): void;
37
+ /**
38
+ * Get access token from URL or localStorage (cookie-less auth for CSRF protection)
39
+ *
40
+ * Priority:
41
+ * 1. Check URL hash fragment (e.g., #_auth_token=xxx from OAuth redirect - most secure)
42
+ * 2. Check URL query parameters (e.g., ?token=xxx - legacy support)
43
+ * 3. Check localStorage (previously saved token)
44
+ *
45
+ * If token is found in URL (hash or query), it will be automatically saved to localStorage
46
+ * and removed from the URL to prevent token exposure in browser history.
47
+ *
48
+ * @returns The access token or null if not found
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * import { createClient, getAccessToken } from '@omnikit/sdk';
53
+ *
54
+ * const omnikit = createClient({
55
+ * appId: 'your-app-id',
56
+ * token: getAccessToken() // Auto-retrieves from URL or localStorage
57
+ * });
58
+ * ```
59
+ */
60
+ declare function getAccessToken(): string | null;
61
+ /**
62
+ * Set access token (saves to localStorage)
63
+ * Alias for saveAccessToken for consistency with getAccessToken
64
+ * @param token - The access token to set
65
+ */
66
+ declare function setAccessToken(token: string): void;
67
+
68
+ export { cleanTokenFromHash, cleanTokenFromUrl, getAccessToken, isTokenInUrl, removeAccessToken, saveAccessToken, setAccessToken, setAccessTokenKey };
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Authentication utility functions for Omnikit SDK
3
+ * Provides helpers for token management
4
+ */
5
+ /**
6
+ * Set the app-specific localStorage key for tokens
7
+ * This ensures each app has its own isolated token storage
8
+ * Called by APIClient during initialization
9
+ * @param appId - The app ID to use for the storage key
10
+ */
11
+ declare function setAccessTokenKey(appId: string): void;
12
+ /**
13
+ * Check if token exists in URL parameters
14
+ * @returns True if token is present in URL
15
+ */
16
+ declare function isTokenInUrl(): boolean;
17
+ /**
18
+ * Remove token from URL without page reload
19
+ * Cleans up the URL after token has been saved to localStorage
20
+ */
21
+ declare function cleanTokenFromUrl(): void;
22
+ /**
23
+ * Remove OAuth token from URL hash without page reload
24
+ * Cleans up the hash after token has been saved to localStorage
25
+ */
26
+ declare function cleanTokenFromHash(): void;
27
+ /**
28
+ * Save access token to localStorage
29
+ * @param token - The access token to save
30
+ */
31
+ declare function saveAccessToken(token: string): void;
32
+ /**
33
+ * Remove access token from localStorage
34
+ * Call this on logout to clear the saved token
35
+ */
36
+ declare function removeAccessToken(): void;
37
+ /**
38
+ * Get access token from URL or localStorage (cookie-less auth for CSRF protection)
39
+ *
40
+ * Priority:
41
+ * 1. Check URL hash fragment (e.g., #_auth_token=xxx from OAuth redirect - most secure)
42
+ * 2. Check URL query parameters (e.g., ?token=xxx - legacy support)
43
+ * 3. Check localStorage (previously saved token)
44
+ *
45
+ * If token is found in URL (hash or query), it will be automatically saved to localStorage
46
+ * and removed from the URL to prevent token exposure in browser history.
47
+ *
48
+ * @returns The access token or null if not found
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * import { createClient, getAccessToken } from '@omnikit/sdk';
53
+ *
54
+ * const omnikit = createClient({
55
+ * appId: 'your-app-id',
56
+ * token: getAccessToken() // Auto-retrieves from URL or localStorage
57
+ * });
58
+ * ```
59
+ */
60
+ declare function getAccessToken(): string | null;
61
+ /**
62
+ * Set access token (saves to localStorage)
63
+ * Alias for saveAccessToken for consistency with getAccessToken
64
+ * @param token - The access token to set
65
+ */
66
+ declare function setAccessToken(token: string): void;
67
+
68
+ export { cleanTokenFromHash, cleanTokenFromUrl, getAccessToken, isTokenInUrl, removeAccessToken, saveAccessToken, setAccessToken, setAccessTokenKey };
@@ -0,0 +1,124 @@
1
+ 'use strict';
2
+
3
+ // src/auth-utils.ts
4
+ var ACCESS_TOKEN_KEY = "access_token";
5
+ var TOKEN_URL_PARAM = "token";
6
+ function setAccessTokenKey(appId) {
7
+ ACCESS_TOKEN_KEY = `omnikit_token_${appId}`;
8
+ }
9
+ function isBrowser() {
10
+ return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
11
+ }
12
+ function getTokenFromUrl() {
13
+ if (!isBrowser()) return null;
14
+ try {
15
+ const urlParams = new URLSearchParams(window.location.search);
16
+ return urlParams.get(TOKEN_URL_PARAM);
17
+ } catch (error) {
18
+ console.warn("Failed to get token from URL:", error);
19
+ return null;
20
+ }
21
+ }
22
+ function getTokenFromStorage() {
23
+ if (!isBrowser()) return null;
24
+ try {
25
+ return localStorage.getItem(ACCESS_TOKEN_KEY);
26
+ } catch (error) {
27
+ console.warn("Failed to get token from localStorage:", error);
28
+ return null;
29
+ }
30
+ }
31
+ function isTokenInUrl() {
32
+ return getTokenFromUrl() !== null;
33
+ }
34
+ function cleanTokenFromUrl() {
35
+ if (!isBrowser()) return;
36
+ try {
37
+ const url = new URL(window.location.href);
38
+ if (url.searchParams.has(TOKEN_URL_PARAM)) {
39
+ url.searchParams.delete(TOKEN_URL_PARAM);
40
+ window.history.replaceState({}, "", url.toString());
41
+ }
42
+ } catch (error) {
43
+ console.warn("Failed to clean token from URL:", error);
44
+ }
45
+ }
46
+ function getTokenFromHash() {
47
+ if (!isBrowser()) return null;
48
+ const hash = window.location.hash;
49
+ if (!hash) return null;
50
+ try {
51
+ const params = new URLSearchParams(hash.substring(1));
52
+ return params.get("_auth_token");
53
+ } catch (error) {
54
+ console.warn("Failed to get token from hash:", error);
55
+ return null;
56
+ }
57
+ }
58
+ function cleanTokenFromHash() {
59
+ if (!isBrowser()) return;
60
+ try {
61
+ window.history.replaceState(
62
+ null,
63
+ "",
64
+ window.location.pathname + window.location.search
65
+ );
66
+ } catch (error) {
67
+ console.warn("Failed to clean token from hash:", error);
68
+ }
69
+ }
70
+ function saveAccessToken(token) {
71
+ if (!isBrowser()) {
72
+ console.warn("Cannot save token: not in browser environment");
73
+ return;
74
+ }
75
+ try {
76
+ localStorage.setItem(ACCESS_TOKEN_KEY, token);
77
+ } catch (error) {
78
+ console.error("Failed to save token to localStorage:", error);
79
+ }
80
+ }
81
+ function removeAccessToken() {
82
+ if (!isBrowser()) return;
83
+ try {
84
+ localStorage.removeItem(ACCESS_TOKEN_KEY);
85
+ } catch (error) {
86
+ console.warn("Failed to remove token from localStorage:", error);
87
+ }
88
+ }
89
+ function getAccessToken() {
90
+ const hashToken = getTokenFromHash();
91
+ if (hashToken) {
92
+ saveAccessToken(hashToken);
93
+ cleanTokenFromHash();
94
+ console.log("\u2705 OAuth token captured from URL hash");
95
+ if (isBrowser()) {
96
+ window.dispatchEvent(new CustomEvent("omnikit:auth-change"));
97
+ }
98
+ return hashToken;
99
+ }
100
+ const urlToken = getTokenFromUrl();
101
+ if (urlToken) {
102
+ saveAccessToken(urlToken);
103
+ cleanTokenFromUrl();
104
+ if (isBrowser()) {
105
+ window.dispatchEvent(new CustomEvent("omnikit:auth-change"));
106
+ }
107
+ return urlToken;
108
+ }
109
+ return getTokenFromStorage();
110
+ }
111
+ function setAccessToken(token) {
112
+ saveAccessToken(token);
113
+ }
114
+
115
+ exports.cleanTokenFromHash = cleanTokenFromHash;
116
+ exports.cleanTokenFromUrl = cleanTokenFromUrl;
117
+ exports.getAccessToken = getAccessToken;
118
+ exports.isTokenInUrl = isTokenInUrl;
119
+ exports.removeAccessToken = removeAccessToken;
120
+ exports.saveAccessToken = saveAccessToken;
121
+ exports.setAccessToken = setAccessToken;
122
+ exports.setAccessTokenKey = setAccessTokenKey;
123
+ //# sourceMappingURL=auth-utils.js.map
124
+ //# sourceMappingURL=auth-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/auth-utils.ts"],"names":[],"mappings":";;;AAOA,IAAI,gBAAA,GAAmB,cAAA;AACvB,IAAM,eAAA,GAAkB,OAAA;AAQjB,SAAS,kBAAkB,KAAA,EAAqB;AACrD,EAAA,gBAAA,GAAmB,iBAAiB,KAAK,CAAA,CAAA;AAC3C;AAKA,SAAS,SAAA,GAAqB;AAC5B,EAAA,OAAO,OAAO,MAAA,KAAW,WAAA,IAAe,OAAO,OAAO,YAAA,KAAiB,WAAA;AACzE;AAMA,SAAS,eAAA,GAAiC;AACxC,EAAA,IAAI,CAAC,SAAA,EAAU,EAAG,OAAO,IAAA;AAEzB,EAAA,IAAI;AACF,IAAA,MAAM,SAAA,GAAY,IAAI,eAAA,CAAgB,MAAA,CAAO,SAAS,MAAM,CAAA;AAC5D,IAAA,OAAO,SAAA,CAAU,IAAI,eAAe,CAAA;AAAA,EACtC,SAAS,KAAA,EAAO;AACd,IAAA,OAAA,CAAQ,IAAA,CAAK,iCAAiC,KAAK,CAAA;AACnD,IAAA,OAAO,IAAA;AAAA,EACT;AACF;AAMA,SAAS,mBAAA,GAAqC;AAC5C,EAAA,IAAI,CAAC,SAAA,EAAU,EAAG,OAAO,IAAA;AAEzB,EAAA,IAAI;AACF,IAAA,OAAO,YAAA,CAAa,QAAQ,gBAAgB,CAAA;AAAA,EAC9C,SAAS,KAAA,EAAO;AACd,IAAA,OAAA,CAAQ,IAAA,CAAK,0CAA0C,KAAK,CAAA;AAC5D,IAAA,O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MAAA,MAAA,CAAO,aAAA,CAAc,IAAI,WAAA,CAAY,qBAAqB,CAAC,CAAA;AAAA,IAC7D;AAEA,IAAA,OAAO,QAAA;AAAA,EACT;AAIA,EAAA,OAAO,mBAAA,EAAoB;AAC7B;AAOO,SAAS,eAAe,KAAA,EAAqB;AAClD,EAAA,eAAA,CAAgB,KAAK,CAAA;AACvB","file":"auth-utils.js","sourcesContent":["/**\n * Authentication utility functions for Omnikit SDK\n * Provides helpers for token management\n */\n\n// App-specific localStorage key to prevent token sharing between apps\n// Format: omnikit_token_{appId} for app-specific isolation\nlet ACCESS_TOKEN_KEY = 'access_token'; // Default fallback for backwards compatibility\nconst TOKEN_URL_PARAM = 'token';\n\n/**\n * Set the app-specific localStorage key for tokens\n * This ensures each app has its own isolated token storage\n * Called by APIClient during initialization\n * @param appId - The app ID to use for the storage key\n */\nexport function setAccessTokenKey(appId: string): void {\n ACCESS_TOKEN_KEY = `omnikit_token_${appId}`;\n}\n\n/**\n * Check if code is running in browser environment\n */\nfunction isBrowser(): boolean {\n return typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';\n}\n\n/**\n * Get access token from URL parameters\n * @returns Token string or null if not found\n */\nfunction getTokenFromUrl(): string | null {\n if (!isBrowser()) return null;\n\n try {\n const urlParams = new URLSearchParams(window.location.search);\n return urlParams.get(TOKEN_URL_PARAM);\n } catch (error) {\n console.warn('Failed to get token from URL:', error);\n return null;\n }\n}\n\n/**\n * Get access token from localStorage\n * @returns Token string or null if not found\n */\nfunction getTokenFromStorage(): string | null {\n if (!isBrowser()) return null;\n\n try {\n return localStorage.getItem(ACCESS_TOKEN_KEY);\n } catch (error) {\n console.warn('Failed to get token from localStorage:', error);\n return null;\n }\n}\n\n/**\n * Check if token exists in URL parameters\n * @returns True if token is present in URL\n */\nexport function isTokenInUrl(): boolean {\n return getTokenFromUrl() !== null;\n}\n\n/**\n * Remove token from URL without page reload\n * Cleans up the URL after token has been saved to localStorage\n */\nexport function cleanTokenFromUrl(): void {\n if (!isBrowser()) return;\n\n try {\n const url = new URL(window.location.href);\n if (url.searchParams.has(TOKEN_URL_PARAM)) {\n url.searchParams.delete(TOKEN_URL_PARAM);\n window.history.replaceState({}, '', url.toString());\n }\n } catch (error) {\n console.warn('Failed to clean token from URL:', error);\n }\n}\n\n/**\n * Get OAuth token from URL hash fragment\n * Checks for _auth_token (primary) from OAuth callback\n * Hash fragments are more secure than query params (not sent to server)\n * @returns Token string or null if not found\n */\nfunction getTokenFromHash(): string | null {\n if (!isBrowser()) return null;\n\n const hash = window.location.hash;\n if (!hash) return null;\n\n try {\n // Remove # prefix and parse as URLSearchParams\n const params = new URLSearchParams(hash.substring(1));\n // Check for _auth_token (used by OAuth callback)\n return params.get('_auth_token');\n } catch (error) {\n console.warn('Failed to get token from hash:', error);\n return null;\n }\n}\n\n/**\n * Remove OAuth token from URL hash without page reload\n * Cleans up the hash after token has been saved to localStorage\n */\nexport function cleanTokenFromHash(): void {\n if (!isBrowser()) return;\n\n try {\n // Remove hash entirely, keeping only path and query string\n window.history.replaceState(\n null,\n '',\n window.location.pathname + window.location.search\n );\n } catch (error) {\n console.warn('Failed to clean token from hash:', error);\n }\n}\n\n/**\n * Save access token to localStorage\n * @param token - The access token to save\n */\nexport function saveAccessToken(token: string): void {\n if (!isBrowser()) {\n console.warn('Cannot save token: not in browser environment');\n return;\n }\n\n try {\n localStorage.setItem(ACCESS_TOKEN_KEY, token);\n } catch (error) {\n console.error('Failed to save token to localStorage:', error);\n }\n}\n\n/**\n * Remove access token from localStorage\n * Call this on logout to clear the saved token\n */\nexport function removeAccessToken(): void {\n if (!isBrowser()) return;\n\n try {\n localStorage.removeItem(ACCESS_TOKEN_KEY);\n } catch (error) {\n console.warn('Failed to remove token from localStorage:', error);\n }\n}\n\n/**\n * Get access token from URL or localStorage (cookie-less auth for CSRF protection)\n *\n * Priority:\n * 1. Check URL hash fragment (e.g., #_auth_token=xxx from OAuth redirect - most secure)\n * 2. Check URL query parameters (e.g., ?token=xxx - legacy support)\n * 3. Check localStorage (previously saved token)\n *\n * If token is found in URL (hash or query), it will be automatically saved to localStorage\n * and removed from the URL to prevent token exposure in browser history.\n *\n * @returns The access token or null if not found\n *\n * @example\n * ```typescript\n * import { createClient, getAccessToken } from '@omnikit/sdk';\n *\n * const omnikit = createClient({\n * appId: 'your-app-id',\n * token: getAccessToken() // Auto-retrieves from URL or localStorage\n * });\n * ```\n */\nexport function getAccessToken(): string | null {\n // Priority 1: Check URL hash (OAuth callback: #_auth_token=xxx)\n // Hash fragments are more secure - not sent to server in HTTP requests\n const hashToken = getTokenFromHash();\n if (hashToken) {\n // Save to localStorage for future use\n saveAccessToken(hashToken);\n\n // Clean hash to prevent token exposure in browser history\n cleanTokenFromHash();\n\n console.log('✅ OAuth token captured from URL hash');\n\n // Dispatch auth-change event so AuthGate can re-check auth state\n // This triggers a re-render after OAuth callback\n if (isBrowser()) {\n window.dispatchEvent(new CustomEvent('omnikit:auth-change'));\n }\n\n return hashToken;\n }\n\n // Priority 2: Check URL query params (legacy: ?token=xxx)\n const urlToken = getTokenFromUrl();\n if (urlToken) {\n // Save to localStorage for future use\n saveAccessToken(urlToken);\n\n // Clean URL to prevent token exposure in browser history\n cleanTokenFromUrl();\n\n // Dispatch auth-change event so AuthGate can re-check auth state\n if (isBrowser()) {\n window.dispatchEvent(new CustomEvent('omnikit:auth-change'));\n }\n\n return urlToken;\n }\n\n // Priority 3: Fall back to localStorage\n // No cookies used (cookie-less auth for CSRF protection)\n return getTokenFromStorage();\n}\n\n/**\n * Set access token (saves to localStorage)\n * Alias for saveAccessToken for consistency with getAccessToken\n * @param token - The access token to set\n */\nexport function setAccessToken(token: string): void {\n saveAccessToken(token);\n}\n"]}
@@ -0,0 +1,115 @@
1
+ // src/auth-utils.ts
2
+ var ACCESS_TOKEN_KEY = "access_token";
3
+ var TOKEN_URL_PARAM = "token";
4
+ function setAccessTokenKey(appId) {
5
+ ACCESS_TOKEN_KEY = `omnikit_token_${appId}`;
6
+ }
7
+ function isBrowser() {
8
+ return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
9
+ }
10
+ function getTokenFromUrl() {
11
+ if (!isBrowser()) return null;
12
+ try {
13
+ const urlParams = new URLSearchParams(window.location.search);
14
+ return urlParams.get(TOKEN_URL_PARAM);
15
+ } catch (error) {
16
+ console.warn("Failed to get token from URL:", error);
17
+ return null;
18
+ }
19
+ }
20
+ function getTokenFromStorage() {
21
+ if (!isBrowser()) return null;
22
+ try {
23
+ return localStorage.getItem(ACCESS_TOKEN_KEY);
24
+ } catch (error) {
25
+ console.warn("Failed to get token from localStorage:", error);
26
+ return null;
27
+ }
28
+ }
29
+ function isTokenInUrl() {
30
+ return getTokenFromUrl() !== null;
31
+ }
32
+ function cleanTokenFromUrl() {
33
+ if (!isBrowser()) return;
34
+ try {
35
+ const url = new URL(window.location.href);
36
+ if (url.searchParams.has(TOKEN_URL_PARAM)) {
37
+ url.searchParams.delete(TOKEN_URL_PARAM);
38
+ window.history.replaceState({}, "", url.toString());
39
+ }
40
+ } catch (error) {
41
+ console.warn("Failed to clean token from URL:", error);
42
+ }
43
+ }
44
+ function getTokenFromHash() {
45
+ if (!isBrowser()) return null;
46
+ const hash = window.location.hash;
47
+ if (!hash) return null;
48
+ try {
49
+ const params = new URLSearchParams(hash.substring(1));
50
+ return params.get("_auth_token");
51
+ } catch (error) {
52
+ console.warn("Failed to get token from hash:", error);
53
+ return null;
54
+ }
55
+ }
56
+ function cleanTokenFromHash() {
57
+ if (!isBrowser()) return;
58
+ try {
59
+ window.history.replaceState(
60
+ null,
61
+ "",
62
+ window.location.pathname + window.location.search
63
+ );
64
+ } catch (error) {
65
+ console.warn("Failed to clean token from hash:", error);
66
+ }
67
+ }
68
+ function saveAccessToken(token) {
69
+ if (!isBrowser()) {
70
+ console.warn("Cannot save token: not in browser environment");
71
+ return;
72
+ }
73
+ try {
74
+ localStorage.setItem(ACCESS_TOKEN_KEY, token);
75
+ } catch (error) {
76
+ console.error("Failed to save token to localStorage:", error);
77
+ }
78
+ }
79
+ function removeAccessToken() {
80
+ if (!isBrowser()) return;
81
+ try {
82
+ localStorage.removeItem(ACCESS_TOKEN_KEY);
83
+ } catch (error) {
84
+ console.warn("Failed to remove token from localStorage:", error);
85
+ }
86
+ }
87
+ function getAccessToken() {
88
+ const hashToken = getTokenFromHash();
89
+ if (hashToken) {
90
+ saveAccessToken(hashToken);
91
+ cleanTokenFromHash();
92
+ console.log("\u2705 OAuth token captured from URL hash");
93
+ if (isBrowser()) {
94
+ window.dispatchEvent(new CustomEvent("omnikit:auth-change"));
95
+ }
96
+ return hashToken;
97
+ }
98
+ const urlToken = getTokenFromUrl();
99
+ if (urlToken) {
100
+ saveAccessToken(urlToken);
101
+ cleanTokenFromUrl();
102
+ if (isBrowser()) {
103
+ window.dispatchEvent(new CustomEvent("omnikit:auth-change"));
104
+ }
105
+ return urlToken;
106
+ }
107
+ return getTokenFromStorage();
108
+ }
109
+ function setAccessToken(token) {
110
+ saveAccessToken(token);
111
+ }
112
+
113
+ export { cleanTokenFromHash, cleanTokenFromUrl, getAccessToken, isTokenInUrl, removeAccessToken, saveAccessToken, setAccessToken, setAccessTokenKey };
114
+ //# sourceMappingURL=auth-utils.mjs.map
115
+ //# sourceMappingURL=auth-utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/auth-utils.ts"],"names":[],"mappings":";AAOA,IAAI,gBAAA,GAAmB,cAAA;AACvB,IAAM,eAAA,GAAkB,OAAA;AAQjB,SAAS,kBAAkB,KAAA,EAAqB;AACrD,EAAA,gBAAA,GAAmB,iBAAiB,KAAK,CAAA,CAAA;AAC3C;AAKA,SAAS,SAAA,GAAqB;AAC5B,EAAA,OAAO,OAAO,MAAA,KAAW,WAAA,IAAe,OAAO,OAAO,YAAA,KAAiB,WAAA;AACzE;AAMA,SAAS,eAAA,GAAiC;AACxC,EAAA,IAAI,CAAC,SAAA,EAAU,EAAG,OAAO,IAAA;AAEzB,EAAA,IAAI;AACF,IAAA,MAAM,SA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M,YAAY,gBAAA,EAAiB;AACnC,EAAA,IAAI,SAAA,EAAW;AAEb,IAAA,eAAA,CAAgB,SAAS,CAAA;AAGzB,IAAA,kBAAA,EAAmB;AAEnB,IAAA,OAAA,CAAQ,IAAI,2CAAsC,CAAA;AAIlD,IAAA,IAAI,WAAU,EAAG;AACf,MAAA,MAAA,CAAO,aAAA,CAAc,IAAI,WAAA,CAAY,qBAAqB,CAAC,CAAA;AAAA,IAC7D;AAEA,IAAA,OAAO,SAAA;AAAA,EACT;AAGA,EAAA,MAAM,WAAW,eAAA,EAAgB;AACjC,EAAA,IAAI,QAAA,EAAU;AAEZ,IAAA,eAAA,CAAgB,QAAQ,CAAA;AAGxB,IAAA,iBAAA,EAAkB;AAGlB,IAAA,IAAI,WAAU,EAAG;AACf,MAAA,MAAA,CAAO,aAAA,CAAc,IAAI,WAAA,CAAY,qBAAqB,CAAC,CAAA;AAAA,IAC7D;AAEA,IAAA,OAAO,QAAA;AAAA,EACT;AAIA,EAAA,OAAO,mBAAA,EAAoB;AAC7B;AAOO,SAAS,eAAe,KAAA,EAAqB;AAClD,EAAA,eAAA,CAAgB,KAAK,CAAA;AACvB","file":"auth-utils.mjs","sourcesContent":["/**\n * Authentication utility functions for Omnikit SDK\n * Provides helpers for token management\n */\n\n// App-specific localStorage key to prevent token sharing between apps\n// Format: omnikit_token_{appId} for app-specific isolation\nlet ACCESS_TOKEN_KEY = 'access_token'; // Default fallback for backwards compatibility\nconst TOKEN_URL_PARAM = 'token';\n\n/**\n * Set the app-specific localStorage key for tokens\n * This ensures each app has its own isolated token storage\n * Called by APIClient during initialization\n * @param appId - The app ID to use for the storage key\n */\nexport function setAccessTokenKey(appId: string): void {\n ACCESS_TOKEN_KEY = `omnikit_token_${appId}`;\n}\n\n/**\n * Check if code is running in browser environment\n */\nfunction isBrowser(): boolean {\n return typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';\n}\n\n/**\n * Get access token from URL parameters\n * @returns Token string or null if not found\n */\nfunction getTokenFromUrl(): string | null {\n if (!isBrowser()) return null;\n\n try {\n const urlParams = new URLSearchParams(window.location.search);\n return urlParams.get(TOKEN_URL_PARAM);\n } catch (error) {\n console.warn('Failed to get token from URL:', error);\n return null;\n }\n}\n\n/**\n * Get access token from localStorage\n * @returns Token string or null if not found\n */\nfunction getTokenFromStorage(): string | null {\n if (!isBrowser()) return null;\n\n try {\n return localStorage.getItem(ACCESS_TOKEN_KEY);\n } catch (error) {\n console.warn('Failed to get token from localStorage:', error);\n return null;\n }\n}\n\n/**\n * Check if token exists in URL parameters\n * @returns True if token is present in URL\n */\nexport function isTokenInUrl(): boolean {\n return getTokenFromUrl() !== null;\n}\n\n/**\n * Remove token from URL without page reload\n * Cleans up the URL after token has been saved to localStorage\n */\nexport function cleanTokenFromUrl(): void {\n if (!isBrowser()) return;\n\n try {\n const url = new URL(window.location.href);\n if (url.searchParams.has(TOKEN_URL_PARAM)) {\n url.searchParams.delete(TOKEN_URL_PARAM);\n window.history.replaceState({}, '', url.toString());\n }\n } catch (error) {\n console.warn('Failed to clean token from URL:', error);\n }\n}\n\n/**\n * Get OAuth token from URL hash fragment\n * Checks for _auth_token (primary) from OAuth callback\n * Hash fragments are more secure than query params (not sent to server)\n * @returns Token string or null if not found\n */\nfunction getTokenFromHash(): string | null {\n if (!isBrowser()) return null;\n\n const hash = window.location.hash;\n if (!hash) return null;\n\n try {\n // Remove # prefix and parse as URLSearchParams\n const params = new URLSearchParams(hash.substring(1));\n // Check for _auth_token (used by OAuth callback)\n return params.get('_auth_token');\n } catch (error) {\n console.warn('Failed to get token from hash:', error);\n return null;\n }\n}\n\n/**\n * Remove OAuth token from URL hash without page reload\n * Cleans up the hash after token has been saved to localStorage\n */\nexport function cleanTokenFromHash(): void {\n if (!isBrowser()) return;\n\n try {\n // Remove hash entirely, keeping only path and query string\n window.history.replaceState(\n null,\n '',\n window.location.pathname + window.location.search\n );\n } catch (error) {\n console.warn('Failed to clean token from hash:', error);\n }\n}\n\n/**\n * Save access token to localStorage\n * @param token - The access token to save\n */\nexport function saveAccessToken(token: string): void {\n if (!isBrowser()) {\n console.warn('Cannot save token: not in browser environment');\n return;\n }\n\n try {\n localStorage.setItem(ACCESS_TOKEN_KEY, token);\n } catch (error) {\n console.error('Failed to save token to localStorage:', error);\n }\n}\n\n/**\n * Remove access token from localStorage\n * Call this on logout to clear the saved token\n */\nexport function removeAccessToken(): void {\n if (!isBrowser()) return;\n\n try {\n localStorage.removeItem(ACCESS_TOKEN_KEY);\n } catch (error) {\n console.warn('Failed to remove token from localStorage:', error);\n }\n}\n\n/**\n * Get access token from URL or localStorage (cookie-less auth for CSRF protection)\n *\n * Priority:\n * 1. Check URL hash fragment (e.g., #_auth_token=xxx from OAuth redirect - most secure)\n * 2. Check URL query parameters (e.g., ?token=xxx - legacy support)\n * 3. Check localStorage (previously saved token)\n *\n * If token is found in URL (hash or query), it will be automatically saved to localStorage\n * and removed from the URL to prevent token exposure in browser history.\n *\n * @returns The access token or null if not found\n *\n * @example\n * ```typescript\n * import { createClient, getAccessToken } from '@omnikit/sdk';\n *\n * const omnikit = createClient({\n * appId: 'your-app-id',\n * token: getAccessToken() // Auto-retrieves from URL or localStorage\n * });\n * ```\n */\nexport function getAccessToken(): string | null {\n // Priority 1: Check URL hash (OAuth callback: #_auth_token=xxx)\n // Hash fragments are more secure - not sent to server in HTTP requests\n const hashToken = getTokenFromHash();\n if (hashToken) {\n // Save to localStorage for future use\n saveAccessToken(hashToken);\n\n // Clean hash to prevent token exposure in browser history\n cleanTokenFromHash();\n\n console.log('✅ OAuth token captured from URL hash');\n\n // Dispatch auth-change event so AuthGate can re-check auth state\n // This triggers a re-render after OAuth callback\n if (isBrowser()) {\n window.dispatchEvent(new CustomEvent('omnikit:auth-change'));\n }\n\n return hashToken;\n }\n\n // Priority 2: Check URL query params (legacy: ?token=xxx)\n const urlToken = getTokenFromUrl();\n if (urlToken) {\n // Save to localStorage for future use\n saveAccessToken(urlToken);\n\n // Clean URL to prevent token exposure in browser history\n cleanTokenFromUrl();\n\n // Dispatch auth-change event so AuthGate can re-check auth state\n if (isBrowser()) {\n window.dispatchEvent(new CustomEvent('omnikit:auth-change'));\n }\n\n return urlToken;\n }\n\n // Priority 3: Fall back to localStorage\n // No cookies used (cookie-less auth for CSRF protection)\n return getTokenFromStorage();\n}\n\n/**\n * Set access token (saves to localStorage)\n * Alias for saveAccessToken for consistency with getAccessToken\n * @param token - The access token to set\n */\nexport function setAccessToken(token: string): void {\n saveAccessToken(token);\n}\n"]}