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
{% extends "base.html" %}

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

{% block header %}
<div class="d-flex justify-content-between align-items-center mb-4">
  <h1>
    <i class="bi bi-people me-2"></i>
    User Management
  </h1>
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createUserModal">
    <i class="bi bi-person-plus me-1"></i>
    Create User
  </button>
</div>
{% endblock %}

{% block content %}
<!-- User Statistics -->
<div class="row mb-4">
  <div class="col-md-3">
    <div class="card">
      <div class="card-body text-center">
        <h4 class="text-primary">{{ stats.total_users }}</h4>
        <p class="mb-0 text-muted">Total Users</p>
      </div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card">
      <div class="card-body text-center">
        <h4 class="text-success">{{ stats.active_users }}</h4>
        <p class="mb-0 text-muted">Active Users</p>
      </div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card">
      <div class="card-body text-center">
        <h4 class="text-info">{{ stats.admin_users }}</h4>
        <p class="mb-0 text-muted">Admin Users</p>
      </div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card">
      <div class="card-body text-center">
        <h4 class="text-warning">{{ stats.pending_users }}</h4>
        <p class="mb-0 text-muted">Pending Verification</p>
      </div>
    </div>
  </div>
</div>

<!-- User Management Table -->
<div class="card">
  <div class="card-header">
    <div class="row align-items-center">
      <div class="col">
        <h6 class="mb-0">User List</h6>
      </div>
      <div class="col-auto">
        <div class="d-flex gap-2">
          <!-- Search -->
          <div class="input-group input-group-sm" style="width: 250px;">
            <input type="text" class="form-control" id="userSearch" placeholder="Search users...">
            <button class="btn btn-outline-secondary" type="button">
              <i class="bi bi-search"></i>
            </button>
          </div>

          <!-- Filter -->
          <select class="form-select form-select-sm" id="userFilter" style="width: auto;">
            <option value="all">All Users</option>
            <option value="active">Active Only</option>
            <option value="inactive">Inactive Only</option>
            <option value="admin">Admin Only</option>
            <option value="pending">Pending Verification</option>
          </select>
        </div>
      </div>
    </div>
  </div>
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead class="table-light">
          <tr>
            <th>User</th>
            <th>Status</th>
            <th>Role</th>
            <th>Last Login</th>
            <th>Created</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody id="userTableBody">
          {% if users.is_empty() %}
          <tr>
            <td colspan="6" class="text-center text-muted py-4">
              <i class="bi bi-person-x fs-2 d-block mb-2"></i>
              No users found
            </td>
          </tr>
          {% else %}
          {% for user in users %}
          <tr data-user-id="{{ user.id }}">
            <td>
              <div class="d-flex align-items-center">
                <div

                  class="avatar-sm bg-primary text-white rounded-circle d-flex align-items-center justify-content-center me-3">
                  U
                </div>
                <div>
                  <div class="fw-medium">{{ user.email }}</div>
                  {% if user.full_name %}
                  <div class="text-muted small">{{ user.full_name }}</div>
                  {% endif %}
                </div>
              </div>
            </td>
            <td>
              {% if user.is_active %}
              <span class="badge bg-success">Active</span>
              {% else %}
              <span class="badge bg-secondary">Inactive</span>
              {% endif %}

              {% if user.email_verified %}
              <span class="badge bg-info ms-1">Verified</span>
              {% else %}
              <span class="badge bg-warning ms-1">Unverified</span>
              {% endif %}
            </td>
            <td>
              {% if user.is_admin %}
              <span class="badge bg-danger">Admin</span>
              {% else %}
              <span class="badge bg-secondary">User</span>
              {% endif %}
            </td>
            <td>
              {% if user.last_login %}
              {{ user.last_login }}
              {% else %}
              <span class="text-muted">Never</span>
              {% endif %}
            </td>
            <td>{{ user.created_at }}</td>
            <td>
              <div class="btn-group btn-group-sm">
                <button type="button" class="btn btn-outline-primary" onclick="editUser('{{ user.id }}')">
                  <i class="bi bi-pencil"></i>
                </button>
                <button type="button" class="btn btn-outline-info" onclick="viewUserSessions('{{ user.id }}')">
                  <i class="bi bi-activity"></i>
                </button>
                {% if not user.is_admin or user.id != current_user.id %}
                <button type="button" class="btn btn-outline-danger"

                  onclick="deleteUser('{{ user.id }}', '{{ user.email }}')">
                  <i class="bi bi-trash"></i>
                </button>
                {% endif %}
              </div>
            </td>
          </tr>
          {% endfor %}
          {% endif %}
        </tbody>
      </table>
    </div>
  </div>

  <!-- Pagination -->
  {% if users.has_other_pages %}
  <div class="card-footer">
    <nav aria-label="User pagination">
      <ul class="pagination pagination-sm mb-0 justify-content-center">
        {% if users.has_previous %}
        <li class="page-item">
          <a class="page-link" href="?page={{ users.previous_page_number }}">Previous</a>
        </li>
        {% endif %}

        {% for num in users.paginator.page_range %}
        {% if users.number == num %}
        <li class="page-item active">
          <span class="page-link">{{ num }}</span>
        </li>
        {% else %}
        <li class="page-item">
          <a class="page-link" href="?page={{ num }}">{{ num }}</a>
        </li>
        {% endif %}
        {% endfor %}

        {% if users.has_next %}
        <li class="page-item">
          <a class="page-link" href="?page={{ users.next_page_number }}">Next</a>
        </li>
        {% endif %}
      </ul>
    </nav>
  </div>
  {% endif %}
</div>

<!-- Create User Modal -->
<div class="modal fade" id="createUserModal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Create New User</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <form id="createUserForm">
        <div class="modal-body">
          <div class="mb-3">
            <label for="userEmail" class="form-label">Email Address</label>
            <input type="email" class="form-control" id="userEmail" required>
          </div>
          <div class="mb-3">
            <label for="userFullName" class="form-label">Full Name (Optional)</label>
            <input type="text" class="form-control" id="userFullName">
          </div>
          <div class="mb-3">
            <label for="userPassword" class="form-label">Password</label>
            <input type="password" class="form-control" id="userPassword" required>
            <div class="form-text">Leave empty to send invitation email</div>
          </div>
          <div class="mb-3">
            <div class="form-check">
              <input class="form-check-input" type="checkbox" id="userIsAdmin">
              <label class="form-check-label" for="userIsAdmin">
                Administrator privileges
              </label>
            </div>
          </div>
          <div class="mb-3">
            <div class="form-check">
              <input class="form-check-input" type="checkbox" id="userIsActive" checked>
              <label class="form-check-label" for="userIsActive">
                Account is active
              </label>
            </div>
          </div>
          <div class="mb-3">
            <div class="form-check">
              <input class="form-check-input" type="checkbox" id="sendInvitation" checked>
              <label class="form-check-label" for="sendInvitation">
                Send invitation email
              </label>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-primary">Create User</button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- Edit User Modal -->
<div class="modal fade" id="editUserModal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Edit User</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <form id="editUserForm">
        <div class="modal-body">
          <input type="hidden" id="editUserId">
          <div class="mb-3">
            <label for="editUserEmail" class="form-label">Email Address</label>
            <input type="email" class="form-control" id="editUserEmail" required>
          </div>
          <div class="mb-3">
            <label for="editUserFullName" class="form-label">Full Name</label>
            <input type="text" class="form-control" id="editUserFullName">
          </div>
          <div class="mb-3">
            <label for="editUserPassword" class="form-label">New Password</label>
            <input type="password" class="form-control" id="editUserPassword">
            <div class="form-text">Leave empty to keep current password</div>
          </div>
          <div class="mb-3">
            <div class="form-check">
              <input class="form-check-input" type="checkbox" id="editUserIsAdmin">
              <label class="form-check-label" for="editUserIsAdmin">
                Administrator privileges
              </label>
            </div>
          </div>
          <div class="mb-3">
            <div class="form-check">
              <input class="form-check-input" type="checkbox" id="editUserIsActive">
              <label class="form-check-label" for="editUserIsActive">
                Account is active
              </label>
            </div>
          </div>
          <div class="mb-3">
            <div class="form-check">
              <input class="form-check-input" type="checkbox" id="editUserEmailVerified">
              <label class="form-check-label" for="editUserEmailVerified">
                Email is verified
              </label>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-primary">Update User</button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- User Sessions Modal -->
<div class="modal fade" id="userSessionsModal" tabindex="-1">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">User Sessions</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <div id="sessionsContent">
          <div class="text-center py-3">
            <div class="spinner-border" role="status">
              <span class="visually-hidden">Loading...</span>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
{% endblock %}

{% block scripts %}
<script>
  // User management functions
  async function createUser(event) {
    event.preventDefault();

    const formData = {
      email: document.getElementById('userEmail').value,
      full_name: document.getElementById('userFullName').value,
      password: document.getElementById('userPassword').value,
      is_admin: document.getElementById('userIsAdmin').checked,
      is_active: document.getElementById('userIsActive').checked,
      send_invitation: document.getElementById('sendInvitation').checked
    };

    try {
      await apiCall('/users', 'POST', formData);

      // Close modal and reload page
      bootstrap.Modal.getInstance(document.getElementById('createUserModal')).hide();
      window.location.reload();
    } catch (error) {
      alert('Failed to create user: ' + error.message);
    }
  }

  async function editUser(userId) {
    try {
      // Load user data
      const user = await apiCall(`/users/${userId}`);

      // Populate edit form
      document.getElementById('editUserId').value = user.id;
      document.getElementById('editUserEmail').value = user.email;
      document.getElementById('editUserFullName').value = user.full_name || '';
      document.getElementById('editUserIsAdmin').checked = user.is_admin;
      document.getElementById('editUserIsActive').checked = user.is_active;
      document.getElementById('editUserEmailVerified').checked = user.email_verified;

      // Show modal
      new bootstrap.Modal(document.getElementById('editUserModal')).show();
    } catch (error) {
      alert('Failed to load user data: ' + error.message);
    }
  }

  async function updateUser(event) {
    event.preventDefault();

    const userId = document.getElementById('editUserId').value;
    const formData = {
      email: document.getElementById('editUserEmail').value,
      full_name: document.getElementById('editUserFullName').value,
      is_admin: document.getElementById('editUserIsAdmin').checked,
      is_active: document.getElementById('editUserIsActive').checked,
      email_verified: document.getElementById('editUserEmailVerified').checked
    };

    const password = document.getElementById('editUserPassword').value;
    if (password) {
      formData.password = password;
    }

    try {
      await apiCall(`/users/${userId}`, 'PUT', formData);

      // Close modal and reload page
      bootstrap.Modal.getInstance(document.getElementById('editUserModal')).hide();
      window.location.reload();
    } catch (error) {
      alert('Failed to update user: ' + error.message);
    }
  }

  async function deleteUser(userId, email) {
    if (!confirm(`Are you sure you want to delete user "${email}"? This action cannot be undone.`)) {
      return;
    }

    try {
      await apiCall(`/users/${userId}`, 'DELETE');

      // Remove row from table
      document.querySelector(`tr[data-user-id="${userId}"]`).remove();

      // Update stats (simple refresh for now)
      window.location.reload();
    } catch (error) {
      alert('Failed to delete user: ' + error.message);
    }
  }

  async function viewUserSessions(userId) {
    try {
      const sessions = await apiCall(`/users/${userId}/sessions`);

      let sessionsHtml = '';
      if (sessions.length === 0) {
        sessionsHtml = '<div class="text-center text-muted py-3">No active sessions</div>';
      } else {
        sessionsHtml = `

                    <div class="table-responsive">

                        <table class="table table-sm">

                            <thead>

                                <tr>

                                    <th>Session ID</th>

                                    <th>IP Address</th>

                                    <th>User Agent</th>

                                    <th>Created</th>

                                    <th>Last Activity</th>

                                    <th>Actions</th>

                                </tr>

                            </thead>

                            <tbody>

                `;

        sessions.forEach(session => {
          sessionsHtml += `

                        <tr>

                            <td><code class="small">${session.id.substring(0, 8)}...</code></td>

                            <td>${session.ip_address}</td>

                            <td class="small" style="max-width: 200px; overflow: hidden; text-overflow: ellipsis;">

                                ${session.user_agent}

                            </td>

                            <td>${new Date(session.created_at).toLocaleDateString()}</td>

                            <td>${new Date(session.last_activity).toLocaleString()}</td>

                            <td>

                                <button class="btn btn-sm btn-outline-danger"

                                        onclick="terminateSession('${session.id}')">

                                    <i class="bi bi-x-circle"></i>

                                </button>

                            </td>

                        </tr>

                    `;
        });

        sessionsHtml += '</tbody></table></div>';
      }

      document.getElementById('sessionsContent').innerHTML = sessionsHtml;
      new bootstrap.Modal(document.getElementById('userSessionsModal')).show();
    } catch (error) {
      alert('Failed to load user sessions: ' + error.message);
    }
  }

  async function terminateSession(sessionId) {
    if (!confirm('Are you sure you want to terminate this session?')) {
      return;
    }

    try {
      await apiCall(`/sessions/${sessionId}`, 'DELETE');

      // Reload sessions
      const userId = document.getElementById('editUserId').value;
      if (userId) {
        viewUserSessions(userId);
      }
    } catch (error) {
      alert('Failed to terminate session: ' + error.message);
    }
  }

  // Search and filter functionality
  document.getElementById('userSearch').addEventListener('input', function () {
    filterUsers();
  });

  document.getElementById('userFilter').addEventListener('change', function () {
    filterUsers();
  });

  function filterUsers() {
    const searchTerm = document.getElementById('userSearch').value.toLowerCase();
    const filter = document.getElementById('userFilter').value;
    const rows = document.querySelectorAll('#userTableBody tr[data-user-id]');

    rows.forEach(row => {
      const email = row.querySelector('td:first-child .fw-medium').textContent.toLowerCase();
      const badges = row.querySelector('td:nth-child(2)').textContent.toLowerCase();

      let show = true;

      // Apply search filter
      if (searchTerm && !email.includes(searchTerm)) {
        show = false;
      }

      // Apply status filter
      if (filter !== 'all') {
        switch (filter) {
          case 'active':
            show = show && badges.includes('active');
            break;
          case 'inactive':
            show = show && badges.includes('inactive');
            break;
          case 'admin':
            show = show && badges.includes('admin');
            break;
          case 'pending':
            show = show && badges.includes('unverified');
            break;
        }
      }

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

  // Form event listeners
  document.getElementById('createUserForm').addEventListener('submit', createUser);
  document.getElementById('editUserForm').addEventListener('submit', updateUser);
</script>
{% endblock %}