@civic/auth 0.6.1-beta.3 → 0.6.1
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.
- package/CHANGELOG.md +3 -0
- package/README.md +7 -0
- package/dist/nextjs/config.d.ts.map +1 -1
- package/dist/nextjs/config.js +1 -5
- package/dist/nextjs/config.js.map +1 -1
- package/dist/nextjs/hooks/useUserCookie.d.ts.map +1 -1
- package/dist/nextjs/hooks/useUserCookie.js.map +1 -1
- package/dist/nextjs/middleware.d.ts.map +1 -1
- package/dist/nextjs/middleware.js +51 -18
- package/dist/nextjs/middleware.js.map +1 -1
- package/dist/nextjs/providers/NextAuthProvider.d.ts.map +1 -1
- package/dist/nextjs/providers/NextAuthProvider.js +0 -1
- package/dist/nextjs/providers/NextAuthProvider.js.map +1 -1
- package/dist/nextjs/routeHandler.d.ts.map +1 -1
- package/dist/nextjs/routeHandler.js +8 -0
- package/dist/nextjs/routeHandler.js.map +1 -1
- package/dist/shared/hooks/useSignIn.d.ts +4 -9
- package/dist/shared/hooks/useSignIn.d.ts.map +1 -1
- package/dist/shared/hooks/useSignIn.js +42 -75
- package/dist/shared/hooks/useSignIn.js.map +1 -1
- package/dist/shared/providers/AuthContext.d.ts +2 -7
- package/dist/shared/providers/AuthContext.d.ts.map +1 -1
- package/dist/shared/providers/AuthContext.js.map +1 -1
- package/dist/shared/providers/UserProvider.d.ts +1 -5
- package/dist/shared/providers/UserProvider.d.ts.map +1 -1
- package/dist/shared/providers/UserProvider.js.map +1 -1
- package/dist/shared/version.d.ts +1 -1
- package/dist/shared/version.d.ts.map +1 -1
- package/dist/shared/version.js +1 -1
- package/dist/shared/version.js.map +1 -1
- package/dist/vanillajs/auth/AuthenticationEvents.d.ts.map +1 -1
- package/dist/vanillajs/auth/AuthenticationEvents.js +2 -2
- package/dist/vanillajs/auth/AuthenticationEvents.js.map +1 -1
- package/dist/vanillajs/auth/CivicAuth.d.ts +107 -68
- package/dist/vanillajs/auth/CivicAuth.d.ts.map +1 -1
- package/dist/vanillajs/auth/CivicAuth.js +412 -389
- package/dist/vanillajs/auth/CivicAuth.js.map +1 -1
- package/dist/vanillajs/auth/{handlers/OAuthCallbackHandler.d.ts → OAuthCallbackHandler.d.ts} +2 -2
- package/dist/vanillajs/auth/OAuthCallbackHandler.d.ts.map +1 -0
- package/dist/vanillajs/auth/OAuthCallbackHandler.js +143 -0
- package/dist/vanillajs/auth/OAuthCallbackHandler.js.map +1 -0
- package/dist/vanillajs/auth/SessionManager.d.ts.map +1 -1
- package/dist/vanillajs/auth/SessionManager.js +2 -2
- package/dist/vanillajs/auth/SessionManager.js.map +1 -1
- package/dist/vanillajs/auth/TokenRefresher.d.ts.map +1 -1
- package/dist/vanillajs/auth/TokenRefresher.js +2 -2
- package/dist/vanillajs/auth/TokenRefresher.js.map +1 -1
- package/dist/vanillajs/iframe/IframeManager.d.ts +0 -33
- package/dist/vanillajs/iframe/IframeManager.d.ts.map +1 -1
- package/dist/vanillajs/iframe/IframeManager.js +36 -163
- package/dist/vanillajs/iframe/IframeManager.js.map +1 -1
- package/dist/vanillajs/index.d.ts +2 -2
- package/dist/vanillajs/index.d.ts.map +1 -1
- package/dist/vanillajs/index.js +2 -2
- package/dist/vanillajs/index.js.map +1 -1
- package/dist/vanillajs/services/ApiService.d.ts.map +1 -1
- package/dist/vanillajs/services/ApiService.js +2 -2
- package/dist/vanillajs/services/ApiService.js.map +1 -1
- package/dist/vanillajs/types/index.d.ts +10 -15
- package/dist/vanillajs/types/index.d.ts.map +1 -1
- package/dist/vanillajs/types/index.js +10 -15
- package/dist/vanillajs/types/index.js.map +1 -1
- package/dist/vanillajs/utils/auth-utils.d.ts +1 -2
- package/dist/vanillajs/utils/auth-utils.d.ts.map +1 -1
- package/dist/vanillajs/utils/auth-utils.js +3 -6
- package/dist/vanillajs/utils/auth-utils.js.map +1 -1
- package/dist/vanillajs/utils/logger.d.ts +15 -16
- package/dist/vanillajs/utils/logger.d.ts.map +1 -1
- package/dist/vanillajs/utils/logger.js +19 -35
- package/dist/vanillajs/utils/logger.js.map +1 -1
- package/package.json +1 -6
- package/dist/vanillajs/auth/config/ConfigProcessor.d.ts +0 -6
- package/dist/vanillajs/auth/config/ConfigProcessor.d.ts.map +0 -1
- package/dist/vanillajs/auth/config/ConfigProcessor.js +0 -59
- package/dist/vanillajs/auth/config/ConfigProcessor.js.map +0 -1
- package/dist/vanillajs/auth/handlers/IframeAuthHandler.d.ts +0 -40
- package/dist/vanillajs/auth/handlers/IframeAuthHandler.d.ts.map +0 -1
- package/dist/vanillajs/auth/handlers/IframeAuthHandler.js +0 -388
- package/dist/vanillajs/auth/handlers/IframeAuthHandler.js.map +0 -1
- package/dist/vanillajs/auth/handlers/MessageHandler.d.ts +0 -170
- package/dist/vanillajs/auth/handlers/MessageHandler.d.ts.map +0 -1
- package/dist/vanillajs/auth/handlers/MessageHandler.js +0 -367
- package/dist/vanillajs/auth/handlers/MessageHandler.js.map +0 -1
- package/dist/vanillajs/auth/handlers/OAuthCallbackHandler.d.ts.map +0 -1
- package/dist/vanillajs/auth/handlers/OAuthCallbackHandler.js +0 -301
- package/dist/vanillajs/auth/handlers/OAuthCallbackHandler.js.map +0 -1
- package/dist/vanillajs/auth/handlers/PopupHandler.d.ts +0 -108
- package/dist/vanillajs/auth/handlers/PopupHandler.d.ts.map +0 -1
- package/dist/vanillajs/auth/handlers/PopupHandler.js +0 -333
- package/dist/vanillajs/auth/handlers/PopupHandler.js.map +0 -1
- package/dist/vanillajs/auth/types/AuthTypes.d.ts +0 -128
- package/dist/vanillajs/auth/types/AuthTypes.d.ts.map +0 -1
- package/dist/vanillajs/auth/types/AuthTypes.js +0 -40
- package/dist/vanillajs/auth/types/AuthTypes.js.map +0 -1
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OAuth Callback Handler for Vanilla JavaScript Applications
|
|
3
|
-
*
|
|
4
|
-
* This module provides functionality for handling OAuth redirect/callback pages in vanilla JavaScript
|
|
5
|
-
* applications using the Civic Auth system. It processes the OAuth authorization code flow completion
|
|
6
|
-
* by exchanging authorization codes for access tokens and managing the authentication state.
|
|
7
|
-
*
|
|
8
|
-
* Key responsibilities:
|
|
9
|
-
* - Process OAuth callback URL parameters (code, state)
|
|
10
|
-
* - Exchange authorization codes for access tokens using PKCE
|
|
11
|
-
* - Store tokens and user session data using shared utilities
|
|
12
|
-
* - Create DOM signals for iframe-based authentication flows
|
|
13
|
-
* - Handle error states and cleanup during the OAuth flow
|
|
14
|
-
*
|
|
15
|
-
* This module works in conjunction with:
|
|
16
|
-
* - CivicAuth class for initiating OAuth flows
|
|
17
|
-
* - SignalObserver for detecting authentication completion in iframes
|
|
18
|
-
* - Shared token storage utilities for consistent state management
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```typescript
|
|
22
|
-
* // In an OAuth callback page
|
|
23
|
-
* import { handleOAuthRedirectPage, LocalStorageAdapter } from '@civic/auth/vanillajs';
|
|
24
|
-
*
|
|
25
|
-
* const handled = await handleOAuthRedirectPage({
|
|
26
|
-
* clientId: 'your-client-id',
|
|
27
|
-
* redirectUrl: 'https://your-app.com/callback',
|
|
28
|
-
* oauthServer: 'https://auth.civic.com/oauth/',
|
|
29
|
-
* textSignals: {
|
|
30
|
-
* success: 'Authentication successful!',
|
|
31
|
-
* error: 'Authentication failed!'
|
|
32
|
-
* },
|
|
33
|
-
* storageAdapter: new LocalStorageAdapter()
|
|
34
|
-
* });
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
import { exchangeTokens, buildOauth2Client, getEndpointsWithOverrides, storeTokens as sharedStoreTokens, } from "../../../shared/lib/util.js";
|
|
38
|
-
import { getUser } from "../../../shared/lib/session.js";
|
|
39
|
-
import { GenericUserSession } from "../../../shared/lib/UserSession.js";
|
|
40
|
-
import { CodeVerifier } from "../../../shared/lib/types.js";
|
|
41
|
-
import { createLogger } from "../../utils/logger.js";
|
|
42
|
-
import { GenericPublicClientPKCEProducer } from "../../../services/PKCE.js";
|
|
43
|
-
/**
|
|
44
|
-
* Store tokens using the shared utilities from /shared/lib
|
|
45
|
-
* This ensures consistency with the React implementation and also handles user session storage
|
|
46
|
-
*/
|
|
47
|
-
export async function storeTokens(tokens, storageAdapter, loggerInstance = createLogger("oauth-callback")) {
|
|
48
|
-
try {
|
|
49
|
-
// Use shared storeTokens utility for consistent token storage
|
|
50
|
-
await sharedStoreTokens(storageAdapter, tokens);
|
|
51
|
-
// Get user info using shared utilities
|
|
52
|
-
const user = await getUser(storageAdapter);
|
|
53
|
-
if (user) {
|
|
54
|
-
// Store user session using shared utilities
|
|
55
|
-
const userSession = new GenericUserSession(storageAdapter);
|
|
56
|
-
await userSession.set(user);
|
|
57
|
-
loggerInstance.info("CivicAuth: Tokens and user info stored successfully using shared utilities.");
|
|
58
|
-
return user;
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
loggerInstance.warn("CivicAuth: Failed to extract user info from tokens.");
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
loggerInstance.error("CivicAuth: Error storing tokens:", error);
|
|
67
|
-
throw error;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Handle OAuth redirect page processing for vanilla JavaScript applications.
|
|
72
|
-
* This function processes the OAuth callback URL parameters and exchanges the authorization code for tokens.
|
|
73
|
-
*
|
|
74
|
-
* @param config - Configuration object for handling the OAuth redirect
|
|
75
|
-
* @param config.clientId - OAuth client ID
|
|
76
|
-
* @param config.redirectUrl - URL to redirect to after authentication
|
|
77
|
-
* @param config.oauthServer - OAuth server base URL
|
|
78
|
-
* @param config.textSignals - Text signals for success and error states
|
|
79
|
-
* @param config.storageAdapter - Storage adapter for persisting auth state (required)
|
|
80
|
-
* @param config.logger - Optional logger instance
|
|
81
|
-
* @returns Promise<boolean> - Returns true if callback was handled, false otherwise
|
|
82
|
-
*
|
|
83
|
-
* @example
|
|
84
|
-
* ```typescript
|
|
85
|
-
* import { handleOAuthRedirectPage, LocalStorageAdapter } from '@civic/auth/vanillajs';
|
|
86
|
-
*
|
|
87
|
-
* const handled = await handleOAuthRedirectPage({
|
|
88
|
-
* clientId: 'your-client-id',
|
|
89
|
-
* redirectUrl: 'https://your-app.com/callback',
|
|
90
|
-
* oauthServer: 'https://auth.civic.com/oauth/',
|
|
91
|
-
* textSignals: {
|
|
92
|
-
* success: 'Authentication successful!',
|
|
93
|
-
* error: 'Authentication failed!'
|
|
94
|
-
* },
|
|
95
|
-
* storageAdapter: new LocalStorageAdapter()
|
|
96
|
-
* });
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
export async function handleOAuthRedirectPage(config) {
|
|
100
|
-
const loggerInstance = config.logger || createLogger("oauth-callback");
|
|
101
|
-
const urlParams = new URLSearchParams(window.location.search);
|
|
102
|
-
const code = urlParams.get("code");
|
|
103
|
-
const state = urlParams.get("state");
|
|
104
|
-
const error = urlParams.get("error");
|
|
105
|
-
// Check if we're in a popup window
|
|
106
|
-
const isPopup = window.opener && window.opener !== window;
|
|
107
|
-
loggerInstance.info("CivicAuth: Callback page context", {
|
|
108
|
-
isPopup,
|
|
109
|
-
hasOpener: !!window.opener,
|
|
110
|
-
hasCode: !!code,
|
|
111
|
-
hasError: !!error,
|
|
112
|
-
currentUrl: window.location.href,
|
|
113
|
-
});
|
|
114
|
-
if (error) {
|
|
115
|
-
loggerInstance.error("CivicAuth: OAuth error in callback", { error });
|
|
116
|
-
// Create error signal for iframe mode
|
|
117
|
-
const errorSignalElement = document.createElement("div");
|
|
118
|
-
errorSignalElement.id = "civic-auth-error-signal";
|
|
119
|
-
errorSignalElement.textContent = `${config.textSignals.error} (Error: ${error})`;
|
|
120
|
-
errorSignalElement.style.display = "none";
|
|
121
|
-
document.body.appendChild(errorSignalElement);
|
|
122
|
-
// Send postMessage for popup mode
|
|
123
|
-
if (isPopup && window.opener) {
|
|
124
|
-
loggerInstance.info("CivicAuth: Sending error message to parent window", {
|
|
125
|
-
hasOpener: !!window.opener,
|
|
126
|
-
openerOrigin: window.opener.origin || "unknown",
|
|
127
|
-
currentUrl: window.location.href,
|
|
128
|
-
messageData: {
|
|
129
|
-
type: "auth_error",
|
|
130
|
-
detail: `OAuth error: ${error}`,
|
|
131
|
-
error: error,
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
try {
|
|
135
|
-
window.opener.postMessage({
|
|
136
|
-
type: "auth_error",
|
|
137
|
-
detail: `OAuth error: ${error}`,
|
|
138
|
-
error: error,
|
|
139
|
-
}, "*");
|
|
140
|
-
loggerInstance.info("CivicAuth: Error postMessage sent successfully");
|
|
141
|
-
}
|
|
142
|
-
catch (postMessageError) {
|
|
143
|
-
loggerInstance.error("CivicAuth: Failed to send error postMessage", {
|
|
144
|
-
error: postMessageError,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
// Close popup after a short delay
|
|
148
|
-
setTimeout(() => {
|
|
149
|
-
loggerInstance.info("CivicAuth: Closing popup window");
|
|
150
|
-
try {
|
|
151
|
-
window.close();
|
|
152
|
-
}
|
|
153
|
-
catch (closeError) {
|
|
154
|
-
loggerInstance.warn("CivicAuth: Failed to close popup window", {
|
|
155
|
-
error: closeError,
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}, 1000);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
loggerInstance.debug("CivicAuth: Not in popup mode or no opener", {
|
|
162
|
-
isPopup,
|
|
163
|
-
hasOpener: !!window.opener,
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
return true;
|
|
167
|
-
}
|
|
168
|
-
if (code && state) {
|
|
169
|
-
loggerInstance.info("CivicAuth: OAuth callback detected with code:", code);
|
|
170
|
-
// Use the provided storage adapter from config
|
|
171
|
-
const storage = config.storageAdapter;
|
|
172
|
-
const pkceProducer = new GenericPublicClientPKCEProducer(storage);
|
|
173
|
-
try {
|
|
174
|
-
const endpoints = await getEndpointsWithOverrides(config.oauthServer);
|
|
175
|
-
const oauth2Client = buildOauth2Client(config.clientId, config.redirectUrl, endpoints);
|
|
176
|
-
const tokenResponse = await exchangeTokens(code, state, pkceProducer, oauth2Client, config.oauthServer, endpoints);
|
|
177
|
-
// Get userInfo from storeTokens using shared utilities
|
|
178
|
-
const userInfo = await storeTokens(tokenResponse, storage, loggerInstance);
|
|
179
|
-
loggerInstance.info("CivicAuth: Tokens stored successfully using shared utilities.");
|
|
180
|
-
// Create success signal for iframe mode
|
|
181
|
-
const signalElement = document.createElement("div");
|
|
182
|
-
signalElement.id = "civic-auth-success-signal";
|
|
183
|
-
signalElement.textContent = config.textSignals.success;
|
|
184
|
-
if (userInfo) {
|
|
185
|
-
// Embed userInfo if available
|
|
186
|
-
signalElement.setAttribute("data-user-info", JSON.stringify(userInfo));
|
|
187
|
-
}
|
|
188
|
-
signalElement.style.display = "none";
|
|
189
|
-
document.body.appendChild(signalElement);
|
|
190
|
-
loggerInstance.info("CivicAuth: Appended success signal to body.");
|
|
191
|
-
// Send postMessage for popup mode
|
|
192
|
-
if (isPopup && window.opener) {
|
|
193
|
-
loggerInstance.info("CivicAuth: Sending success message to parent window", {
|
|
194
|
-
hasOpener: !!window.opener,
|
|
195
|
-
openerOrigin: window.opener.origin || "unknown",
|
|
196
|
-
currentUrl: window.location.href,
|
|
197
|
-
messageData: {
|
|
198
|
-
type: "auth_success",
|
|
199
|
-
detail: "Authentication successful",
|
|
200
|
-
hasUser: !!userInfo,
|
|
201
|
-
hasTokens: !!tokenResponse,
|
|
202
|
-
},
|
|
203
|
-
});
|
|
204
|
-
try {
|
|
205
|
-
window.opener.postMessage({
|
|
206
|
-
type: "auth_success",
|
|
207
|
-
detail: "Authentication successful",
|
|
208
|
-
data: {
|
|
209
|
-
user: userInfo,
|
|
210
|
-
tokens: tokenResponse,
|
|
211
|
-
},
|
|
212
|
-
}, "*");
|
|
213
|
-
loggerInstance.info("CivicAuth: Success postMessage sent successfully");
|
|
214
|
-
}
|
|
215
|
-
catch (postMessageError) {
|
|
216
|
-
loggerInstance.error("CivicAuth: Failed to send success postMessage", {
|
|
217
|
-
error: postMessageError,
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
// Close popup after a short delay
|
|
221
|
-
setTimeout(() => {
|
|
222
|
-
loggerInstance.info("CivicAuth: Closing popup window");
|
|
223
|
-
try {
|
|
224
|
-
window.close();
|
|
225
|
-
}
|
|
226
|
-
catch (closeError) {
|
|
227
|
-
loggerInstance.warn("CivicAuth: Failed to close popup window", {
|
|
228
|
-
error: closeError,
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
}, 1000);
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
loggerInstance.debug("CivicAuth: Not in popup mode or no opener", {
|
|
235
|
-
isPopup,
|
|
236
|
-
hasOpener: !!window.opener,
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
// Clean up the code verifier using shared utilities
|
|
240
|
-
await storage.delete(CodeVerifier.COOKIE_NAME);
|
|
241
|
-
}
|
|
242
|
-
catch (error) {
|
|
243
|
-
loggerInstance.error("CivicAuth: Token exchange error:", error);
|
|
244
|
-
// Create error signal for iframe mode
|
|
245
|
-
const errorSignalElement = document.createElement("div");
|
|
246
|
-
errorSignalElement.id = "civic-auth-error-signal";
|
|
247
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
248
|
-
errorSignalElement.textContent = `${config.textSignals.error} (Error: ${errorMessage})`;
|
|
249
|
-
errorSignalElement.style.display = "none";
|
|
250
|
-
document.body.appendChild(errorSignalElement);
|
|
251
|
-
// Send postMessage for popup mode
|
|
252
|
-
if (isPopup && window.opener) {
|
|
253
|
-
loggerInstance.info("CivicAuth: Sending error message to parent window", {
|
|
254
|
-
hasOpener: !!window.opener,
|
|
255
|
-
openerOrigin: window.opener.origin || "unknown",
|
|
256
|
-
currentUrl: window.location.href,
|
|
257
|
-
messageData: {
|
|
258
|
-
type: "auth_error",
|
|
259
|
-
detail: `Token exchange error: ${errorMessage}`,
|
|
260
|
-
error: errorMessage,
|
|
261
|
-
},
|
|
262
|
-
});
|
|
263
|
-
try {
|
|
264
|
-
window.opener.postMessage({
|
|
265
|
-
type: "auth_error",
|
|
266
|
-
detail: `Token exchange error: ${errorMessage}`,
|
|
267
|
-
error: errorMessage,
|
|
268
|
-
}, "*");
|
|
269
|
-
loggerInstance.info("CivicAuth: Error postMessage sent successfully");
|
|
270
|
-
}
|
|
271
|
-
catch (postMessageError) {
|
|
272
|
-
loggerInstance.error("CivicAuth: Failed to send error postMessage", {
|
|
273
|
-
error: postMessageError,
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
// Close popup after a short delay
|
|
277
|
-
setTimeout(() => {
|
|
278
|
-
loggerInstance.info("CivicAuth: Closing popup window");
|
|
279
|
-
try {
|
|
280
|
-
window.close();
|
|
281
|
-
}
|
|
282
|
-
catch (closeError) {
|
|
283
|
-
loggerInstance.warn("CivicAuth: Failed to close popup window", {
|
|
284
|
-
error: closeError,
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
}, 1000);
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
loggerInstance.debug("CivicAuth: Not in popup mode or no opener", {
|
|
291
|
-
isPopup,
|
|
292
|
-
hasOpener: !!window.opener,
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
return true;
|
|
296
|
-
}
|
|
297
|
-
return true;
|
|
298
|
-
}
|
|
299
|
-
return false;
|
|
300
|
-
}
|
|
301
|
-
//# sourceMappingURL=OAuthCallbackHandler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"OAuthCallbackHandler.js","sourceRoot":"","sources":["../../../../src/vanillajs/auth/handlers/OAuthCallbackHandler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,yBAAyB,EACzB,WAA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,EAAE,yBAAyB,YAAY,EAAE;wBAC/C,KAAK,EAAE,YAAY;qBACpB,EACD,GAAG,CACJ,CAAC;oBACF,cAAc,CAAC,IAAI,CAAC,gDAAgD,CAAC,CAAC;gBACxE,CAAC;gBAAC,OAAO,gBAAgB,EAAE,CAAC;oBAC1B,cAAc,CAAC,KAAK,CAAC,6CAA6C,EAAE;wBAClE,KAAK,EAAE,gBAAgB;qBACxB,CAAC,CAAC;gBACL,CAAC;gBAED,kCAAkC;gBAClC,UAAU,CAAC,GAAG,EAAE;oBACd,cAAc,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAC;oBACvD,IAAI,CAAC;wBACH,MAAM,CAAC,KAAK,EAAE,CAAC;oBACjB,CAAC;oBAAC,OAAO,UAAU,EAAE,CAAC;wBACpB,cAAc,CAAC,IAAI,CAAC,yCAAyC,EAAE;4BAC7D,KAAK,EAAE,UAAU;yBAClB,CAAC,CAAC;oBACL,CAAC;gBACH,CAAC,EAAE,IAAI,CAAC,CAAC;YACX,CAAC;iBAAM,CAAC;gBACN,cAAc,CAAC,KAAK,CAAC,2CAA2C,EAAE;oBAChE,OAAO;oBACP,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM;iBAC3B,CAAC,CAAC;YACL,CAAC;YAED,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["/**\n * OAuth Callback Handler for Vanilla JavaScript Applications\n *\n * This module provides functionality for handling OAuth redirect/callback pages in vanilla JavaScript\n * applications using the Civic Auth system. It processes the OAuth authorization code flow completion\n * by exchanging authorization codes for access tokens and managing the authentication state.\n *\n * Key responsibilities:\n * - Process OAuth callback URL parameters (code, state)\n * - Exchange authorization codes for access tokens using PKCE\n * - Store tokens and user session data using shared utilities\n * - Create DOM signals for iframe-based authentication flows\n * - Handle error states and cleanup during the OAuth flow\n *\n * This module works in conjunction with:\n * - CivicAuth class for initiating OAuth flows\n * - SignalObserver for detecting authentication completion in iframes\n * - Shared token storage utilities for consistent state management\n *\n * @example\n * ```typescript\n * // In an OAuth callback page\n * import { handleOAuthRedirectPage, LocalStorageAdapter } from '@civic/auth/vanillajs';\n *\n * const handled = await handleOAuthRedirectPage({\n * clientId: 'your-client-id',\n * redirectUrl: 'https://your-app.com/callback',\n * oauthServer: 'https://auth.civic.com/oauth/',\n * textSignals: {\n * success: 'Authentication successful!',\n * error: 'Authentication failed!'\n * },\n * storageAdapter: new LocalStorageAdapter()\n * });\n * ```\n */\n\nimport {\n exchangeTokens,\n buildOauth2Client,\n getEndpointsWithOverrides,\n storeTokens as sharedStoreTokens,\n} from \"../../../shared/lib/util.js\";\nimport { getUser } from \"../../../shared/lib/session.js\";\nimport { GenericUserSession } from \"../../../shared/lib/UserSession.js\";\nimport { CodeVerifier } from \"../../../shared/lib/types.js\";\nimport type { getCurrentLogger } from \"../../utils/logger.js\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport { GenericPublicClientPKCEProducer } from \"../../../services/PKCE.js\";\nimport type { AuthStorage, OIDCTokenResponseBody } from \"../../../types.js\";\n\n/**\n * Store tokens using the shared utilities from /shared/lib\n * This ensures consistency with the React implementation and also handles user session storage\n */\nexport async function storeTokens(\n tokens: OIDCTokenResponseBody,\n storageAdapter: AuthStorage,\n loggerInstance = createLogger(\"oauth-callback\"),\n): Promise<object | null> {\n try {\n // Use shared storeTokens utility for consistent token storage\n await sharedStoreTokens(storageAdapter, tokens);\n\n // Get user info using shared utilities\n const user = await getUser(storageAdapter);\n if (user) {\n // Store user session using shared utilities\n const userSession = new GenericUserSession(storageAdapter);\n await userSession.set(user);\n loggerInstance.info(\n \"CivicAuth: Tokens and user info stored successfully using shared utilities.\",\n );\n return user;\n } else {\n loggerInstance.warn(\n \"CivicAuth: Failed to extract user info from tokens.\",\n );\n return null;\n }\n } catch (error) {\n loggerInstance.error(\"CivicAuth: Error storing tokens:\", error);\n throw error;\n }\n}\n\nexport interface HandleOAuthRedirectConfig {\n clientId: string;\n redirectUrl: string;\n oauthServer: string;\n textSignals: {\n success: string;\n error: string;\n };\n storageAdapter: AuthStorage;\n logger?: ReturnType<typeof getCurrentLogger>;\n}\n\n/**\n * Handle OAuth redirect page processing for vanilla JavaScript applications.\n * This function processes the OAuth callback URL parameters and exchanges the authorization code for tokens.\n *\n * @param config - Configuration object for handling the OAuth redirect\n * @param config.clientId - OAuth client ID\n * @param config.redirectUrl - URL to redirect to after authentication\n * @param config.oauthServer - OAuth server base URL\n * @param config.textSignals - Text signals for success and error states\n * @param config.storageAdapter - Storage adapter for persisting auth state (required)\n * @param config.logger - Optional logger instance\n * @returns Promise<boolean> - Returns true if callback was handled, false otherwise\n *\n * @example\n * ```typescript\n * import { handleOAuthRedirectPage, LocalStorageAdapter } from '@civic/auth/vanillajs';\n *\n * const handled = await handleOAuthRedirectPage({\n * clientId: 'your-client-id',\n * redirectUrl: 'https://your-app.com/callback',\n * oauthServer: 'https://auth.civic.com/oauth/',\n * textSignals: {\n * success: 'Authentication successful!',\n * error: 'Authentication failed!'\n * },\n * storageAdapter: new LocalStorageAdapter()\n * });\n * ```\n */\nexport async function handleOAuthRedirectPage(\n config: HandleOAuthRedirectConfig,\n): Promise<boolean> {\n const loggerInstance = config.logger || createLogger(\"oauth-callback\");\n const urlParams = new URLSearchParams(window.location.search);\n const code = urlParams.get(\"code\");\n const state = urlParams.get(\"state\");\n const error = urlParams.get(\"error\");\n\n // Check if we're in a popup window\n const isPopup = window.opener && window.opener !== window;\n loggerInstance.info(\"CivicAuth: Callback page context\", {\n isPopup,\n hasOpener: !!window.opener,\n hasCode: !!code,\n hasError: !!error,\n currentUrl: window.location.href,\n });\n\n if (error) {\n loggerInstance.error(\"CivicAuth: OAuth error in callback\", { error });\n\n // Create error signal for iframe mode\n const errorSignalElement = document.createElement(\"div\");\n errorSignalElement.id = \"civic-auth-error-signal\";\n errorSignalElement.textContent = `${config.textSignals.error} (Error: ${error})`;\n errorSignalElement.style.display = \"none\";\n document.body.appendChild(errorSignalElement);\n\n // Send postMessage for popup mode\n if (isPopup && window.opener) {\n loggerInstance.info(\"CivicAuth: Sending error message to parent window\", {\n hasOpener: !!window.opener,\n openerOrigin: window.opener.origin || \"unknown\",\n currentUrl: window.location.href,\n messageData: {\n type: \"auth_error\",\n detail: `OAuth error: ${error}`,\n error: error,\n },\n });\n\n try {\n window.opener.postMessage(\n {\n type: \"auth_error\",\n detail: `OAuth error: ${error}`,\n error: error,\n },\n \"*\",\n );\n loggerInstance.info(\"CivicAuth: Error postMessage sent successfully\");\n } catch (postMessageError) {\n loggerInstance.error(\"CivicAuth: Failed to send error postMessage\", {\n error: postMessageError,\n });\n }\n\n // Close popup after a short delay\n setTimeout(() => {\n loggerInstance.info(\"CivicAuth: Closing popup window\");\n try {\n window.close();\n } catch (closeError) {\n loggerInstance.warn(\"CivicAuth: Failed to close popup window\", {\n error: closeError,\n });\n }\n }, 1000);\n } else {\n loggerInstance.debug(\"CivicAuth: Not in popup mode or no opener\", {\n isPopup,\n hasOpener: !!window.opener,\n });\n }\n\n return true;\n }\n\n if (code && state) {\n loggerInstance.info(\"CivicAuth: OAuth callback detected with code:\", code);\n\n // Use the provided storage adapter from config\n const storage = config.storageAdapter;\n const pkceProducer = new GenericPublicClientPKCEProducer(storage);\n\n try {\n const endpoints = await getEndpointsWithOverrides(config.oauthServer);\n const oauth2Client = buildOauth2Client(\n config.clientId,\n config.redirectUrl,\n endpoints,\n );\n\n const tokenResponse = await exchangeTokens(\n code,\n state,\n pkceProducer,\n oauth2Client,\n config.oauthServer,\n endpoints,\n );\n\n // Get userInfo from storeTokens using shared utilities\n const userInfo = await storeTokens(\n tokenResponse,\n storage,\n loggerInstance,\n );\n loggerInstance.info(\n \"CivicAuth: Tokens stored successfully using shared utilities.\",\n );\n\n // Create success signal for iframe mode\n const signalElement = document.createElement(\"div\");\n signalElement.id = \"civic-auth-success-signal\";\n signalElement.textContent = config.textSignals.success;\n if (userInfo) {\n // Embed userInfo if available\n signalElement.setAttribute(\"data-user-info\", JSON.stringify(userInfo));\n }\n signalElement.style.display = \"none\";\n document.body.appendChild(signalElement);\n loggerInstance.info(\"CivicAuth: Appended success signal to body.\");\n\n // Send postMessage for popup mode\n if (isPopup && window.opener) {\n loggerInstance.info(\n \"CivicAuth: Sending success message to parent window\",\n {\n hasOpener: !!window.opener,\n openerOrigin: window.opener.origin || \"unknown\",\n currentUrl: window.location.href,\n messageData: {\n type: \"auth_success\",\n detail: \"Authentication successful\",\n hasUser: !!userInfo,\n hasTokens: !!tokenResponse,\n },\n },\n );\n\n try {\n window.opener.postMessage(\n {\n type: \"auth_success\",\n detail: \"Authentication successful\",\n data: {\n user: userInfo,\n tokens: tokenResponse,\n },\n },\n \"*\",\n );\n loggerInstance.info(\n \"CivicAuth: Success postMessage sent successfully\",\n );\n } catch (postMessageError) {\n loggerInstance.error(\n \"CivicAuth: Failed to send success postMessage\",\n {\n error: postMessageError,\n },\n );\n }\n\n // Close popup after a short delay\n setTimeout(() => {\n loggerInstance.info(\"CivicAuth: Closing popup window\");\n try {\n window.close();\n } catch (closeError) {\n loggerInstance.warn(\"CivicAuth: Failed to close popup window\", {\n error: closeError,\n });\n }\n }, 1000);\n } else {\n loggerInstance.debug(\"CivicAuth: Not in popup mode or no opener\", {\n isPopup,\n hasOpener: !!window.opener,\n });\n }\n\n // Clean up the code verifier using shared utilities\n await storage.delete(CodeVerifier.COOKIE_NAME);\n } catch (error) {\n loggerInstance.error(\"CivicAuth: Token exchange error:\", error);\n\n // Create error signal for iframe mode\n const errorSignalElement = document.createElement(\"div\");\n errorSignalElement.id = \"civic-auth-error-signal\";\n const errorMessage =\n error instanceof Error ? error.message : \"Unknown error\";\n errorSignalElement.textContent = `${config.textSignals.error} (Error: ${errorMessage})`;\n errorSignalElement.style.display = \"none\";\n document.body.appendChild(errorSignalElement);\n\n // Send postMessage for popup mode\n if (isPopup && window.opener) {\n loggerInstance.info(\n \"CivicAuth: Sending error message to parent window\",\n {\n hasOpener: !!window.opener,\n openerOrigin: window.opener.origin || \"unknown\",\n currentUrl: window.location.href,\n messageData: {\n type: \"auth_error\",\n detail: `Token exchange error: ${errorMessage}`,\n error: errorMessage,\n },\n },\n );\n\n try {\n window.opener.postMessage(\n {\n type: \"auth_error\",\n detail: `Token exchange error: ${errorMessage}`,\n error: errorMessage,\n },\n \"*\",\n );\n loggerInstance.info(\"CivicAuth: Error postMessage sent successfully\");\n } catch (postMessageError) {\n loggerInstance.error(\"CivicAuth: Failed to send error postMessage\", {\n error: postMessageError,\n });\n }\n\n // Close popup after a short delay\n setTimeout(() => {\n loggerInstance.info(\"CivicAuth: Closing popup window\");\n try {\n window.close();\n } catch (closeError) {\n loggerInstance.warn(\"CivicAuth: Failed to close popup window\", {\n error: closeError,\n });\n }\n }, 1000);\n } else {\n loggerInstance.debug(\"CivicAuth: Not in popup mode or no opener\", {\n isPopup,\n hasOpener: !!window.opener,\n });\n }\n\n return true;\n }\n return true;\n }\n return false;\n}\n"]}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import type { AuthResult } from "../../types/index.js";
|
|
2
|
-
import type { ProcessedCivicAuthConfig } from "../types/AuthTypes.js";
|
|
3
|
-
import type { createLogger } from "../../utils/logger.js";
|
|
4
|
-
export interface PopupHandlerConfig {
|
|
5
|
-
config: ProcessedCivicAuthConfig;
|
|
6
|
-
logger: ReturnType<typeof createLogger>;
|
|
7
|
-
onAuthSuccess: (result: AuthResult) => void;
|
|
8
|
-
onAuthError: (error: Error) => void;
|
|
9
|
-
cleanup: () => void;
|
|
10
|
-
}
|
|
11
|
-
export declare class PopupHandler {
|
|
12
|
-
private config;
|
|
13
|
-
private logger;
|
|
14
|
-
private popup?;
|
|
15
|
-
private popupCheckInterval?;
|
|
16
|
-
private onAuthSuccess;
|
|
17
|
-
private onAuthError;
|
|
18
|
-
private cleanup;
|
|
19
|
-
constructor(handlerConfig: PopupHandlerConfig);
|
|
20
|
-
/**
|
|
21
|
-
* Initiates authentication flow using a popup window.
|
|
22
|
-
*
|
|
23
|
-
* Opens a new browser window/tab for authentication, handles browser compatibility
|
|
24
|
-
* (especially Safari), and sets up monitoring for the authentication process.
|
|
25
|
-
*
|
|
26
|
-
* @param fullAuthUrl - The complete authentication URL to open in the popup
|
|
27
|
-
* @throws {PopupError} When popup window cannot be opened (blocked by browser)
|
|
28
|
-
* @returns Promise that resolves when popup setup is complete
|
|
29
|
-
*/
|
|
30
|
-
handleNewTabAuth(fullAuthUrl: string): Promise<void>;
|
|
31
|
-
/**
|
|
32
|
-
* Safely retrieves the current location of a popup window.
|
|
33
|
-
*
|
|
34
|
-
* Attempts to access the popup's location.href property, handling cross-origin
|
|
35
|
-
* access restrictions gracefully by returning a descriptive message instead of throwing.
|
|
36
|
-
*
|
|
37
|
-
* @param popup - The popup window to get location from
|
|
38
|
-
* @returns The popup's URL or a descriptive message if access is restricted
|
|
39
|
-
*/
|
|
40
|
-
private getPopupLocationSafely;
|
|
41
|
-
/**
|
|
42
|
-
* Monitors a popup window for authentication results and handles communication.
|
|
43
|
-
*
|
|
44
|
-
* This method sets up a postMessage listener to receive authentication results from the popup
|
|
45
|
-
* and periodically checks if the popup has been closed. It handles success/error messages
|
|
46
|
-
* and automatically cleans up resources when the popup closes or times out.
|
|
47
|
-
*
|
|
48
|
-
* @param popup - The popup window to monitor for authentication completion
|
|
49
|
-
*
|
|
50
|
-
* @remarks
|
|
51
|
-
* - Monitors popup for up to 5 minutes (300 checks at 1-second intervals)
|
|
52
|
-
* - Listens for 'auth_success' and 'auth_error' message types from the popup
|
|
53
|
-
* - Automatically removes event listeners and resolves/rejects promises when done
|
|
54
|
-
* - Logs detailed debugging information throughout the monitoring process
|
|
55
|
-
*
|
|
56
|
-
* @private
|
|
57
|
-
*/
|
|
58
|
-
private monitorPopup;
|
|
59
|
-
/**
|
|
60
|
-
* Handles successful authentication completion from the popup.
|
|
61
|
-
*
|
|
62
|
-
* Processes the authentication success message received from the popup window,
|
|
63
|
-
* emits success events, triggers the success callback, and performs cleanup.
|
|
64
|
-
*
|
|
65
|
-
* @param message - The authentication message containing success data
|
|
66
|
-
* @param popupMessageHandler - The message event handler to clean up, or null if none exists
|
|
67
|
-
*/
|
|
68
|
-
private handlePopupSuccess;
|
|
69
|
-
/**
|
|
70
|
-
* Handles authentication errors received from the popup.
|
|
71
|
-
*
|
|
72
|
-
* Processes error messages from the popup window, emits error events,
|
|
73
|
-
* creates appropriate CivicAuthError instances, and performs cleanup.
|
|
74
|
-
*
|
|
75
|
-
* @param message - The authentication message containing error details
|
|
76
|
-
* @param popupMessageHandler - The message event handler to clean up, or null if none exists
|
|
77
|
-
*/
|
|
78
|
-
private handlePopupError;
|
|
79
|
-
/**
|
|
80
|
-
* Handles the scenario when the authentication popup is closed by the user.
|
|
81
|
-
* This method is called when the popup window is detected as closed during monitoring.
|
|
82
|
-
* It emits an error event, creates an appropriate error, and performs cleanup.
|
|
83
|
-
*
|
|
84
|
-
* @param popupMessageHandler - The message event handler to clean up, or null if none exists
|
|
85
|
-
*/
|
|
86
|
-
private handlePopupClosed;
|
|
87
|
-
/**
|
|
88
|
-
* Handles authentication timeout scenarios.
|
|
89
|
-
*
|
|
90
|
-
* Called when the popup monitoring reaches its maximum time limit without
|
|
91
|
-
* receiving authentication results. Emits timeout events, creates timeout errors,
|
|
92
|
-
* and performs cleanup operations.
|
|
93
|
-
*
|
|
94
|
-
* @param popupMessageHandler - The message event handler to clean up, or null if none exists
|
|
95
|
-
*/
|
|
96
|
-
private handlePopupTimeout;
|
|
97
|
-
/**
|
|
98
|
-
* Monitors the popup's location for redirect changes.
|
|
99
|
-
*
|
|
100
|
-
* Attempts to check if the popup has redirected to the callback URL, which
|
|
101
|
-
* indicates the authentication flow has progressed. Handles cross-origin
|
|
102
|
-
* access restrictions gracefully by catching and logging access errors.
|
|
103
|
-
*
|
|
104
|
-
* @param popup - The popup window to monitor for location changes
|
|
105
|
-
*/
|
|
106
|
-
private checkPopupLocation;
|
|
107
|
-
}
|
|
108
|
-
//# sourceMappingURL=PopupHandler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PopupHandler.d.ts","sourceRoot":"","sources":["../../../../src/vanillajs/auth/handlers/PopupHandler.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,KAAK,EACV,wBAAwB,EAEzB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAG1D,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,wBAAwB,CAAC;IACjC,MAAM,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;IACxC,aAAa,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,IAAI,CAAC;IAC5C,WAAW,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACpC,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,KAAK,CAAC,CAAgB;IAC9B,OAAO,CAAC,kBAAkB,CAAC,CAAS;IACpC,OAAO,CAAC,aAAa,CAA+B;IACpD,OAAO,CAAC,WAAW,CAAyB;IAC5C,OAAO,CAAC,OAAO,CAAa;gBAEhB,aAAa,EAAE,kBAAkB;IAQ7C;;;;;;;;;OASG;IACU,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoFjE;;;;;;;;OAQG;IACH,OAAO,CAAC,sBAAsB;IAQ9B;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,YAAY;IA2FpB;;;;;;;;OAQG;IACH,OAAO,CAAC,kBAAkB;IAkB1B;;;;;;;;OAQG;IACH,OAAO,CAAC,gBAAgB;IA0BxB;;;;;;OAMG;IACH,OAAO,CAAC,iBAAiB;IAsBzB;;;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