-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-window.js
More file actions
411 lines (356 loc) · 11.7 KB
/
debug-window.js
File metadata and controls
411 lines (356 loc) · 11.7 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* Debug Window - Real-time auth traffic feed
* Displays live stream of authentication requests/responses
*/
import { DOMSecurity } from './modules/ui/dom-security.js';
class DebugWindow {
constructor() {
this.domain = null;
this.startTime = Date.now();
this.requestCount = 0;
this.feed = document.getElementById('feed');
this.port = null;
this.updateInterval = null;
}
async initialize() {
// Get domain from URL params
const params = new URLSearchParams(window.location.search);
this.domain = params.get('domain');
if (!this.domain) {
this.showError('No domain specified');
return;
}
// Update header
document.getElementById('domainName').textContent = this.domain;
// Connect to background via long-lived port
this.connectToBackground();
// Wire up buttons
document.getElementById('clearBtn').addEventListener('click', () => this.clearFeed());
document.getElementById('exportBtn').addEventListener('click', () => this.exportSession());
document.getElementById('closeBtn').addEventListener('click', () => window.close());
// Update duration counter
this.updateInterval = setInterval(() => this.updateDuration(), 1000);
console.log('[DebugWindow] Initialized for domain:', this.domain);
}
/**
* Connect to background script via long-lived port
*/
connectToBackground() {
this.port = chrome.runtime.connect({ name: 'debug-window' });
// Send initial message to register this window
this.port.postMessage({
type: 'register',
domain: this.domain
});
// Listen for messages from background
this.port.onMessage.addListener((message) => {
this.handleMessage(message);
});
// Handle disconnection
this.port.onDisconnect.addListener(() => {
console.log('[DebugWindow] Disconnected from background');
document.getElementById('status').textContent = 'Disconnected';
document.getElementById('status').style.color = '#f85149';
});
}
/**
* Handle messages from background script
*/
handleMessage(message) {
switch (message.type) {
case 'request':
this.addRequest(message.data);
break;
case 'response':
this.updateResponse(message.data);
break;
case 'redirect':
this.addRedirect(message.data);
break;
case 'consoleLog':
this.addConsoleLog(message.data);
break;
case 'session':
// Full session data (for initial load)
this.loadSession(message.data);
break;
default:
console.warn('[DebugWindow] Unknown message type:', message.type);
}
}
/**
* Add request to feed
*/
addRequest(request) {
// Remove empty state
const empty = this.feed.querySelector('.feed-empty');
if (empty) {
empty.remove();
}
const message = document.createElement('div');
message.className = 'feed-message';
message.dataset.requestId = request.id || request.requestId;
const time = this.formatTime(request.timestamp || Date.now());
const actor = this.identifyActor(request.url);
const method = request.method || 'GET';
const urlObj = new URL(request.url);
const path = urlObj.pathname + urlObj.search;
message.innerHTML = `
<div class="message-time">${DOMSecurity.sanitize(time)}</div>
<div class="message-card request" id="request-${DOMSecurity.sanitize(request.id || request.requestId)}">
<div class="message-header">
<span class="actor-icon">${actor.icon}</span>
<span class="actor-name">${DOMSecurity.sanitize(actor.name)}</span>
<span class="method ${DOMSecurity.sanitize(method)}">${DOMSecurity.sanitize(method)}</span>
</div>
<div class="message-url">${DOMSecurity.sanitize(path)}</div>
${request.requestBody ? `
<div class="message-details">
<div class="detail-line">
<span class="detail-key">Body:</span>
<span class="detail-value">${DOMSecurity.sanitize(this.truncate(request.requestBody, 100))}</span>
</div>
</div>
` : ''}
</div>
`;
this.feed.appendChild(message);
this.requestCount++;
document.getElementById('requestCount').textContent = this.requestCount;
// Auto-scroll to bottom
this.feed.scrollTop = this.feed.scrollHeight;
}
/**
* Update request with response data
*/
updateResponse(response) {
const card = document.getElementById(`request-${response.requestId}`);
if (!card) {
console.warn('[DebugWindow] Response for unknown request:', response.requestId);
return;
}
const statusClass = this.getStatusClass(response.statusCode);
const statusBadge = `<span class="status-badge ${statusClass}">${response.statusCode}</span>`;
// Update card
card.classList.remove('request');
card.classList.add('response');
// Add status badge to header
const header = card.querySelector('.message-header');
if (header && !header.querySelector('.status-badge')) {
header.insertAdjacentHTML('beforeend', statusBadge);
}
// Add response details
const existingDetails = card.querySelector('.message-details');
const responseDetails = `
<div class="detail-line">
<span class="detail-key">Status:</span>
<span class="detail-value">${DOMSecurity.sanitize(response.statusCode)} ${DOMSecurity.sanitize(response.statusText || '')}</span>
</div>
${response.responseHeaders ? `
<div class="detail-line">
<span class="detail-key">Headers:</span>
<span class="detail-value">${response.responseHeaders.length} headers</span>
</div>
` : ''}
`;
if (existingDetails) {
existingDetails.insertAdjacentHTML('beforeend', responseDetails);
} else {
card.insertAdjacentHTML('beforeend', `<div class="message-details">${responseDetails}</div>`);
}
}
/**
* Add redirect to feed
*/
addRedirect(redirect) {
const message = document.createElement('div');
message.className = 'feed-message';
const time = this.formatTime(redirect.timestamp || Date.now());
message.innerHTML = `
<div class="message-time">${DOMSecurity.sanitize(time)}</div>
<div class="message-card redirect">
<div class="message-header">
<span class="actor-icon">↪️</span>
<span class="actor-name">Redirect</span>
<span class="status-badge status-3xx">${DOMSecurity.sanitize(redirect.statusCode || '302')}</span>
</div>
<div class="message-details">
<div class="detail-line">
<span class="detail-key">From:</span>
<span class="detail-value">${DOMSecurity.sanitize(this.truncate(redirect.from, 80))}</span>
</div>
<div class="detail-line">
<span class="detail-key">To:</span>
<span class="detail-value">${DOMSecurity.sanitize(this.truncate(redirect.to, 80))}</span>
</div>
</div>
</div>
`;
this.feed.appendChild(message);
this.feed.scrollTop = this.feed.scrollHeight;
}
/**
* Add console log to feed
*/
addConsoleLog(log) {
const message = document.createElement('div');
message.className = 'feed-message';
const time = this.formatTime(log.timestamp || Date.now());
const levelClass = log.level === 'error' ? 'error' : 'request';
message.innerHTML = `
<div class="message-time">${DOMSecurity.sanitize(time)}</div>
<div class="message-card ${levelClass}">
<div class="message-header">
<span class="actor-icon">📝</span>
<span class="actor-name">Console</span>
<span class="method ${DOMSecurity.sanitize(log.level)}">${DOMSecurity.sanitize(log.level.toUpperCase())}</span>
</div>
<div class="message-url">${DOMSecurity.sanitize(log.text)}</div>
</div>
`;
this.feed.appendChild(message);
this.feed.scrollTop = this.feed.scrollHeight;
}
/**
* Load full session data (for initial render)
*/
loadSession(session) {
if (!session || !session.requests) return;
// Clear feed
this.feed.innerHTML = '';
this.requestCount = 0;
// Add all requests
session.requests.forEach(req => {
this.addRequest(req);
if (req.statusCode || req.response) {
this.updateResponse({
requestId: req.id || req.requestId,
statusCode: req.statusCode || req.response?.status,
statusText: req.response?.statusText,
responseHeaders: req.responseHeaders || req.response?.headers
});
}
});
// Add console logs
if (session.consoleLogs) {
session.consoleLogs.forEach(log => this.addConsoleLog(log));
}
}
/**
* Identify actor from URL
*/
identifyActor(url) {
try {
const urlObj = new URL(url);
const hostname = urlObj.hostname;
if (hostname.includes('authentik')) {
return { icon: '🔐', name: 'Authentik' };
}
if (hostname.includes('okta')) {
return { icon: '🔐', name: 'Okta' };
}
if (hostname.includes('microsoft') || hostname.includes('azure')) {
return { icon: '🔐', name: 'Microsoft' };
}
if (hostname.includes('google')) {
return { icon: '🔐', name: 'Google' };
}
if (urlObj.pathname.includes('/api')) {
return { icon: '⚡', name: hostname };
}
return { icon: '🌐', name: hostname };
} catch {
return { icon: '❓', name: 'Unknown' };
}
}
/**
* Get status code CSS class
*/
getStatusClass(status) {
if (status >= 200 && status < 300) return 'status-2xx';
if (status >= 300 && status < 400) return 'status-3xx';
if (status >= 400 && status < 500) return 'status-4xx';
if (status >= 500) return 'status-5xx';
return 'status-2xx';
}
/**
* Format timestamp
*/
formatTime(timestamp) {
const date = new Date(timestamp);
return date.toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 3
});
}
/**
* Truncate string
*/
truncate(str, length) {
if (!str || str.length <= length) return str;
return str.substring(0, length) + '...';
}
/**
* Update duration counter
*/
updateDuration() {
const seconds = Math.floor((Date.now() - this.startTime) / 1000);
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
const formatted = minutes > 0
? `${minutes}m ${secs}s`
: `${secs}s`;
document.getElementById('duration').textContent = formatted;
}
/**
* Clear feed
*/
clearFeed() {
if (!confirm('Clear all captured requests?')) return;
this.feed.innerHTML = `
<div class="feed-empty">
<div class="feed-empty-icon">📡</div>
<div>Waiting for authentication traffic...</div>
<div style="font-size: 10px; margin-top: 8px;">Perform a login to see requests appear here</div>
</div>
`;
this.requestCount = 0;
document.getElementById('requestCount').textContent = '0';
// Notify background to clear session
if (this.port) {
this.port.postMessage({
type: 'clearSession',
domain: this.domain
});
}
}
/**
* Export session
*/
exportSession() {
if (this.port) {
this.port.postMessage({
type: 'exportSession',
domain: this.domain
});
}
}
/**
* Show error
*/
showError(message) {
this.feed.innerHTML = `
<div class="feed-empty">
<div class="feed-empty-icon">❌</div>
<div>${DOMSecurity.sanitize(message)}</div>
</div>
`;
}
}
// Initialize when DOM ready
document.addEventListener('DOMContentLoaded', () => {
const debugWindow = new DebugWindow();
debugWindow.initialize();
});