auth-framework 0.4.2

A comprehensive, production-ready authentication and authorization framework for Rust applications
Documentation
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
{% extends "base.html" %}

{% block title %}Logs - Auth Framework Admin{% endblock %}

{% block header %}
<div class="d-flex justify-content-between align-items-center mb-4">
  <h1>
    <i class="bi bi-journal-text me-2"></i>
    System Logs
  </h1>
  <div class="btn-group">
    <button type="button" class="btn btn-outline-primary" onclick="refreshLogs()">
      <i class="bi bi-arrow-clockwise me-1"></i>
      Refresh
    </button>
    <button type="button" class="btn btn-outline-secondary" onclick="downloadLogs()">
      <i class="bi bi-download me-1"></i>
      Download
    </button>
  </div>
</div>
{% endblock %}

{% block content %}
<!-- Log Controls -->
<div class="card mb-4">
  <div class="card-body">
    <div class="row align-items-center">
      <div class="col-md-3">
        <label for="logLevel" class="form-label">Log Level</label>
        <select class="form-select" id="logLevel">
          <option value="all">All Levels</option>
          <option value="error">Error</option>
          <option value="warn">Warning</option>
          <option value="info" selected>Info</option>
          <option value="debug">Debug</option>
          <option value="trace">Trace</option>
        </select>
      </div>
      <div class="col-md-3">
        <label for="logSource" class="form-label">Source</label>
        <select class="form-select" id="logSource">
          <option value="all">All Sources</option>
          <option value="auth">Authentication</option>
          <option value="api">API</option>
          <option value="database">Database</option>
          <option value="security">Security</option>
          <option value="system">System</option>
        </select>
      </div>
      <div class="col-md-3">
        <label for="logTimeRange" class="form-label">Time Range</label>
        <select class="form-select" id="logTimeRange">
          <option value="1h">Last Hour</option>
          <option value="24h" selected>Last 24 Hours</option>
          <option value="7d">Last 7 Days</option>
          <option value="30d">Last 30 Days</option>
          <option value="custom">Custom Range</option>
        </select>
      </div>
      <div class="col-md-3">
        <label for="logSearch" class="form-label">Search</label>
        <input type="text" class="form-control" id="logSearch" placeholder="Search logs...">
      </div>
    </div>
    <div class="row mt-3">
      <div class="col-md-6">
        <div class="form-check form-switch">
          <input class="form-check-input" type="checkbox" id="autoRefresh">
          <label class="form-check-label" for="autoRefresh">
            Auto-refresh every 10 seconds
          </label>
        </div>
      </div>
      <div class="col-md-6">
        <div class="form-check form-switch">
          <input class="form-check-input" type="checkbox" id="showRawLogs">
          <label class="form-check-label" for="showRawLogs">
            Show raw log format
          </label>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- Log Viewer -->
<div class="card">
  <div class="card-header">
    <div class="d-flex justify-content-between align-items-center">
      <h6 class="mb-0">Log Entries</h6>
      <div class="d-flex align-items-center">
        <small class="text-muted me-3">
          Showing {{ log_entries.len() }} of {{ total_log_entries }} entries
        </small>
        <div class="form-check form-switch">
          <input class="form-check-input" type="checkbox" id="followLogs">
          <label class="form-check-label" for="followLogs">
            Follow tail
          </label>
        </div>
      </div>
    </div>
  </div>
  <div class="card-body p-0">
    <div id="logContainer" class="log-container"

      style="height: 500px; overflow-y: auto; font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;">
      {% for entry in log_entries %}
      <div class="log-entry p-2 border-bottom log-{{ entry.level }}" data-level="{{ entry.level }}"

        data-source="{{ entry.source }}" data-timestamp="{{ entry.timestamp }}">
        <div class="d-flex align-items-start">
          <div class="me-3 text-nowrap">
            <small class="text-muted">{{ entry.timestamp | date:'H:i:s.u' }}</small>
          </div>
          <div class="me-2">
            {% if entry.level == 'error' %}
            <span class="badge bg-danger">ERROR</span>
            {% elif entry.level == 'warn' %}
            <span class="badge bg-warning">WARN</span>
            {% elif entry.level == 'info' %}
            <span class="badge bg-info">INFO</span>
            {% elif entry.level == 'debug' %}
            <span class="badge bg-secondary">DEBUG</span>
            {% elif entry.level == 'trace' %}
            <span class="badge bg-light text-dark">TRACE</span>
            {% endif %}
          </div>
          <div class="me-3">
            <small class="badge bg-light text-dark">{{ entry.source | upper }}</small>
          </div>
          <div class="flex-grow-1">
            <div class="log-message">{{ entry.message }}</div>
            {% if entry.details %}
            <div class="log-details mt-1 small text-muted">
              <details>
                <summary>Details</summary>
                <pre class="mt-2 mb-0 small">{{ entry.details | pprint }}</pre>
              </details>
            </div>
            {% endif %}
            {% if entry.stack_trace %}
            <div class="log-stack-trace mt-1">
              <details>
                <summary class="text-danger small">Stack Trace</summary>
                <pre class="mt-2 mb-0 small text-danger">{{ entry.stack_trace }}</pre>
              </details>
            </div>
            {% endif %}
          </div>
          {% if entry.user_id %}
          <div class="me-2">
            <small class="badge bg-primary">{{ entry.user_email | default:entry.user_id }}</small>
          </div>
          {% endif %}
          {% if entry.request_id %}
          <div class="me-2">
            <small class="text-muted">{{ entry.request_id | slice:":8" }}</small>
          </div>
          {% endif %}
        </div>

        <!-- Raw log format (hidden by default) -->
        <div class="log-raw d-none mt-2">
          <pre class="mb-0 small bg-light p-2 rounded">{{ entry.raw_log }}</pre>
        </div>
      </div>
      {% empty %}
      <div class="text-center text-muted py-5">
        <i class="bi bi-journal-x fs-2 d-block mb-3"></i>
        <h5>No Log Entries</h5>
        <p>No log entries match the current filters</p>
      </div>
      {% endfor %}
    </div>
  </div>

  <!-- Log Statistics Footer -->
  <div class="card-footer">
    <div class="row text-center">
      <div class="col">
        <div class="text-danger">
          <strong>{{ log_stats.error_count }}</strong>
          <small class="d-block text-muted">Errors</small>
        </div>
      </div>
      <div class="col">
        <div class="text-warning">
          <strong>{{ log_stats.warning_count }}</strong>
          <small class="d-block text-muted">Warnings</small>
        </div>
      </div>
      <div class="col">
        <div class="text-info">
          <strong>{{ log_stats.info_count }}</strong>
          <small class="d-block text-muted">Info</small>
        </div>
      </div>
      <div class="col">
        <div class="text-secondary">
          <strong>{{ log_stats.debug_count }}</strong>
          <small class="d-block text-muted">Debug</small>
        </div>
      </div>
      <div class="col">
        <div class="text-muted">
          <strong>{{ log_stats.total_count }}</strong>
          <small class="d-block text-muted">Total</small>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- Custom Date Range Modal -->
<div class="modal fade" id="customDateRangeModal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Custom Date Range</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-md-6">
            <label for="startDate" class="form-label">Start Date</label>
            <input type="datetime-local" class="form-control" id="startDate">
          </div>
          <div class="col-md-6">
            <label for="endDate" class="form-label">End Date</label>
            <input type="datetime-local" class="form-control" id="endDate">
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary" onclick="applyCustomDateRange()">Apply</button>
      </div>
    </div>
  </div>
</div>
{% endblock %}

{% block scripts %}
<script>
  let autoRefreshInterval = null;
  let logWebSocket = null;

  document.addEventListener('DOMContentLoaded', function () {
    // Initialize log filtering
    setupLogFilters();

    // Setup WebSocket for real-time logs
    setupLogWebSocket();

    // Setup auto-refresh
    document.getElementById('autoRefresh').addEventListener('change', function () {
      if (this.checked) {
        startAutoRefresh();
      } else {
        stopAutoRefresh();
      }
    });

    // Setup raw log toggle
    document.getElementById('showRawLogs').addEventListener('change', function () {
      toggleRawLogs(this.checked);
    });

    // Setup follow tail
    document.getElementById('followLogs').addEventListener('change', function () {
      if (this.checked) {
        scrollToBottom();
      }
    });

    // Setup custom date range
    document.getElementById('logTimeRange').addEventListener('change', function () {
      if (this.value === 'custom') {
        new bootstrap.Modal(document.getElementById('customDateRangeModal')).show();
      } else {
        applyFilters();
      }
    });
  });

  function setupLogFilters() {
    ['logLevel', 'logSource', 'logTimeRange', 'logSearch'].forEach(id => {
      document.getElementById(id).addEventListener('change', debounce(applyFilters, 300));
    });

    document.getElementById('logSearch').addEventListener('input', debounce(applyFilters, 500));
  }

  function setupLogWebSocket() {
    const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
    const wsUrl = `${protocol}//${window.location.host}/ws/logs`;

    logWebSocket = new WebSocket(wsUrl);

    logWebSocket.onmessage = function (event) {
      const logEntry = JSON.parse(event.data);
      addLogEntry(logEntry);
    };

    logWebSocket.onclose = function (event) {
      console.log('WebSocket connection closed, attempting to reconnect...');
      setTimeout(setupLogWebSocket, 5000);
    };
  }

  function applyFilters() {
    const level = document.getElementById('logLevel').value;
    const source = document.getElementById('logSource').value;
    const search = document.getElementById('logSearch').value.toLowerCase();

    const entries = document.querySelectorAll('.log-entry');

    entries.forEach(entry => {
      let show = true;

      // Level filter
      if (level !== 'all' && entry.dataset.level !== level) {
        show = false;
      }

      // Source filter
      if (source !== 'all' && entry.dataset.source !== source) {
        show = false;
      }

      // Search filter
      if (search && !entry.textContent.toLowerCase().includes(search)) {
        show = false;
      }

      entry.style.display = show ? 'block' : 'none';
    });
  }

  function addLogEntry(logEntry) {
    const container = document.getElementById('logContainer');
    const entry = createLogEntryElement(logEntry);

    container.appendChild(entry);

    // Follow tail if enabled
    if (document.getElementById('followLogs').checked) {
      scrollToBottom();
    }

    // Limit to prevent memory issues
    const entries = container.querySelectorAll('.log-entry');
    if (entries.length > 1000) {
      entries[0].remove();
    }
  }

  function createLogEntryElement(logEntry) {
    const div = document.createElement('div');
    div.className = `log-entry p-2 border-bottom log-${logEntry.level}`;
    div.dataset.level = logEntry.level;
    div.dataset.source = logEntry.source;
    div.dataset.timestamp = logEntry.timestamp;

    const levelBadge = getLevelBadge(logEntry.level);

    div.innerHTML = `

            <div class="d-flex align-items-start">

                <div class="me-3 text-nowrap">

                    <small class="text-muted">${formatTime(logEntry.timestamp)}</small>

                </div>

                <div class="me-2">${levelBadge}</div>

                <div class="me-3">

                    <small class="badge bg-light text-dark">${logEntry.source.toUpperCase()}</small>

                </div>

                <div class="flex-grow-1">

                    <div class="log-message">${logEntry.message}</div>

                </div>

            </div>

        `;

    return div;
  }

  function getLevelBadge(level) {
    const badges = {
      'error': '<span class="badge bg-danger">ERROR</span>',
      'warn': '<span class="badge bg-warning">WARN</span>',
      'info': '<span class="badge bg-info">INFO</span>',
      'debug': '<span class="badge bg-secondary">DEBUG</span>',
      'trace': '<span class="badge bg-light text-dark">TRACE</span>'
    };
    return badges[level] || '<span class="badge bg-secondary">LOG</span>';
  }

  function formatTime(timestamp) {
    return new Date(timestamp).toLocaleTimeString('en-US', {
      hour12: false,
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    });
  }

  function scrollToBottom() {
    const container = document.getElementById('logContainer');
    container.scrollTop = container.scrollHeight;
  }

  function toggleRawLogs(show) {
    const rawLogs = document.querySelectorAll('.log-raw');
    rawLogs.forEach(raw => {
      raw.classList.toggle('d-none', !show);
    });
  }

  function startAutoRefresh() {
    stopAutoRefresh(); // Clear any existing interval
    autoRefreshInterval = setInterval(refreshLogs, 10000);
  }

  function stopAutoRefresh() {
    if (autoRefreshInterval) {
      clearInterval(autoRefreshInterval);
      autoRefreshInterval = null;
    }
  }

  async function refreshLogs() {
    try {
      const params = new URLSearchParams({
        level: document.getElementById('logLevel').value,
        source: document.getElementById('logSource').value,
        time_range: document.getElementById('logTimeRange').value,
        search: document.getElementById('logSearch').value
      });

      const response = await apiCall(`/logs?${params.toString()}`);

      // Update log container
      const container = document.getElementById('logContainer');
      container.innerHTML = '';

      response.entries.forEach(entry => {
        container.appendChild(createLogEntryElement(entry));
      });

      // Update statistics
      updateLogStatistics(response.stats);

      if (document.getElementById('followLogs').checked) {
        scrollToBottom();
      }
    } catch (error) {
      console.error('Failed to refresh logs:', error);
    }
  }

  function updateLogStatistics(stats) {
    // Update footer statistics (implementation depends on API structure)
    console.log('Log stats updated:', stats);
  }

  async function downloadLogs() {
    try {
      const params = new URLSearchParams({
        level: document.getElementById('logLevel').value,
        source: document.getElementById('logSource').value,
        time_range: document.getElementById('logTimeRange').value,
        search: document.getElementById('logSearch').value,
        format: 'download'
      });

      const response = await fetch(`/api/logs/download?${params.toString()}`);
      const blob = await response.blob();

      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.style.display = 'none';
      a.href = url;
      a.download = `auth-framework-logs-${new Date().toISOString().split('T')[0]}.txt`;
      document.body.appendChild(a);
      a.click();
      window.URL.revokeObjectURL(url);
      document.body.removeChild(a);
    } catch (error) {
      alert('Failed to download logs: ' + error.message);
    }
  }

  function applyCustomDateRange() {
    const startDate = document.getElementById('startDate').value;
    const endDate = document.getElementById('endDate').value;

    if (!startDate || !endDate) {
      alert('Please select both start and end dates');
      return;
    }

    // Apply custom date range (implementation would depend on API)
    console.log('Custom date range:', startDate, 'to', endDate);

    bootstrap.Modal.getInstance(document.getElementById('customDateRangeModal')).hide();
    applyFilters();
  }

  // Utility function for debouncing
  function debounce(func, wait) {
    let timeout;
    return function executedFunction(...args) {
      const later = () => {
        clearTimeout(timeout);
        func(...args);
      };
      clearTimeout(timeout);
      timeout = setTimeout(later, wait);
    };
  }

  // Cleanup on page unload
  window.addEventListener('beforeunload', function () {
    stopAutoRefresh();
    if (logWebSocket) {
      logWebSocket.close();
    }
  });
</script>

<style>
  .log-container {
    background-color: #f8f9fa;
    font-size: 0.875rem;
  }

  .log-entry {
    transition: background-color 0.2s ease;
  }

  .log-entry:hover {
    background-color: rgba(0, 123, 255, 0.1);
  }

  .log-error {
    border-left: 4px solid #dc3545;
  }

  .log-warn {
    border-left: 4px solid #ffc107;
  }

  .log-info {
    border-left: 4px solid #17a2b8;
  }

  .log-debug {
    border-left: 4px solid #6c757d;
  }

  .log-trace {
    border-left: 4px solid #e9ecef;
  }

  .log-message {
    word-break: break-word;
  }

  .log-raw pre {
    font-size: 0.75rem;
    max-height: 200px;
    overflow-y: auto;
  }

  details summary {
    cursor: pointer;
    user-select: none;
  }

  details summary:hover {
    text-decoration: underline;
  }
</style>
{% endblock %}