also looking at this
feat: mailbox recovery addresses and password reset by email
#13
21 files
+1503
-10
| @@ -2,6 +2,8 @@ const std = @import("std"); | ||
| 2 | 2 | const database = @import("storage/database.zig"); |
| 3 | 3 | const password_mod = @import("auth/password.zig"); |
| 4 | 4 | const auth = @import("auth/auth.zig"); |
| 5 | const recovery = @import("api/webmail_recovery.zig"); | |
| 6 | const time_compat = @import("core/time_compat.zig"); | |
| 5 | 7 | |
| 6 | 8 | pub fn createUser(auth_backend: *auth.AuthBackend, username: []const u8, password: []const u8, email: []const u8) !void { |
| 7 | 9 | const user_id = auth_backend.createUser(username, password, email) catch |err| { |
| @@ -61,4 +63,55 @@ pub fn userInfo(db: *database.Database, allocator: std.mem.Allocator, username: | ||
| 61 | 63 | std.debug.print(" Enabled: {}\n", .{user.enabled}); |
| 62 | 64 | std.debug.print(" Created: {d}\n", .{user.created_at}); |
| 63 | 65 | std.debug.print(" Updated: {d}\n", .{user.updated_at}); |
| 66 | if (try recovery.get(allocator, db, username)) |r| { | |
| 67 | defer if (r.address) |a| allocator.free(a); | |
| 68 | if (r.address) |address| { | |
| 69 | std.debug.print(" Recovery: {s} (set by {s})\n", .{ address, if (r.source) |source| @tagName(source) else "unknown" }); | |
| 70 | } else std.debug.print(" Recovery: none\n", .{}); | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | pub const RecoveryAction = union(enum) { | |
| 75 | show, | |
| 76 | clear, | |
| 77 | apply: struct { address: []const u8, policy: recovery.Policy }, | |
| 78 | }; | |
| 79 | ||
| 80 | /// `user:local recovery`: show, set or clear a mailbox's recovery address. | |
| 81 | /// | |
| 82 | /// Prints one `Recovery: <outcome>` line that deploy provisioning matches on: | |
| 83 | /// `updated`, `unchanged`, `kept` (the owner set their own and the policy is | |
| 84 | /// `initial`), `cleared`, or the current value for `show`. A missing mailbox | |
| 85 | /// or an unusable address is an error, so the exit status is non-zero. | |
| 86 | pub fn recoveryCommand(db: *database.Database, allocator: std.mem.Allocator, username: []const u8, action: RecoveryAction) !void { | |
| 87 | const current = (try recovery.get(allocator, db, username)) orelse { | |
| 88 | std.debug.print("Error: user '{s}' not found\n", .{username}); | |
| 89 | return error.UserNotFound; | |
| 90 | }; | |
| 91 | defer if (current.address) |a| allocator.free(a); | |
| 92 | const now = time_compat.timestamp(); | |
| 93 | switch (action) { | |
| 94 | .show => if (current.address) |address| { | |
| 95 | std.debug.print("Recovery: {s} (set by {s})\n", .{ address, if (current.source) |source| @tagName(source) else "unknown" }); | |
| 96 | } else std.debug.print("Recovery: none\n", .{}), | |
| 97 | .clear => { | |
| 98 | try recovery.set(db, username, null, .operator, now); | |
| 99 | std.debug.print("Recovery: cleared\n", .{}); | |
| 100 | }, | |
| 101 | .apply => |wanted| { | |
| 102 | const result = recovery.apply(allocator, db, username, wanted.address, wanted.policy, now) catch |err| { | |
| 103 | switch (err) { | |
| 104 | recovery.RecoveryError.SameAsMailbox => std.debug.print("Error: a mailbox cannot be its own recovery address\n", .{}), | |
| 105 | recovery.RecoveryError.InvalidAddress => std.debug.print("Error: '{s}' is not a usable recovery address\n", .{wanted.address}), | |
| 106 | else => {}, | |
| 107 | } | |
| 108 | return err; | |
| 109 | }; | |
| 110 | switch (result) { | |
| 111 | .updated => std.debug.print("Recovery: updated\n", .{}), | |
| 112 | .unchanged => std.debug.print("Recovery: unchanged\n", .{}), | |
| 113 | .kept_owner_choice => std.debug.print("Recovery: kept (the mailbox owner set their own; use --policy managed to replace it)\n", .{}), | |
| 114 | } | |
| 115 | }, | |
| 116 | } | |
| 64 | 117 | } |