-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMfaController.cs
More file actions
149 lines (134 loc) · 5.41 KB
/
Copy pathMfaController.cs
File metadata and controls
149 lines (134 loc) · 5.41 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
using redb.Identity.Contracts.Routes;
using redb.Route.Controllers.Attributes;
namespace redb.Identity.Http.Controllers;
/// <summary>
/// REST management API for MFA configuration.
/// Forwards to <c>direct-vm://identity-manage-mfa</c> route.
/// </summary>
[Route("mfa")]
public class MfaController : IdentityControllerBase
{
[HttpGet("status/{userId}")]
public async Task<object?> GetStatus([FromRoute("userId")] string userId)
{
return await Forward(IdentityEndpoints.MfaManage, "status",
new Dictionary<string, object> { ["userId"] = ParseLong(userId) });
}
[HttpPost("totp/setup")]
public async Task<object?> SetupTotp([FromBody] MfaSetupRequest request)
{
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.MfaManage, "setup",
new Dictionary<string, object?>
{
["userId"] = request.UserId,
["method"] = "totp",
["username"] = request.Username
});
}
[HttpPost("totp/confirm")]
public async Task<object?> ConfirmTotp([FromBody] MfaConfirmRequest request)
{
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.MfaManage, "confirm",
new Dictionary<string, object?>
{
["userId"] = request.UserId,
["method"] = "totp",
["code"] = request.Code
});
}
[HttpDelete("totp/{userId}")]
public async Task<object?> DisableTotp([FromRoute("userId")] string userId)
{
return await Forward(IdentityEndpoints.MfaManage, "disable",
new Dictionary<string, object?>
{
["userId"] = ParseLong(userId),
["method"] = "totp"
});
}
[HttpPost("recovery-codes/regenerate")]
public async Task<object?> RegenerateRecoveryCodes([FromBody] MfaUserIdRequest request)
{
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.MfaManage, "regenerate-recovery",
new Dictionary<string, object?> { ["userId"] = request.UserId });
}
// ── SMS OTP ──────────────────────────────────────────────────────────────
[HttpPost("sms/setup")]
public async Task<object?> SetupSms([FromBody] MfaOtpSetupRequest request)
{
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.MfaManage, "setup",
new Dictionary<string, object?>
{
["userId"] = request.UserId,
["method"] = "sms",
["username"] = request.Username,
["destination"] = request.Destination
});
}
[HttpPost("sms/confirm")]
public async Task<object?> ConfirmSms([FromBody] MfaOtpConfirmRequest request)
{
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.MfaManage, "confirm",
new Dictionary<string, object?>
{
["userId"] = request.UserId,
["method"] = "sms",
["code"] = request.Code,
["mfa_state"] = request.MfaState
});
}
[HttpDelete("sms/{userId}")]
public async Task<object?> DisableSms([FromRoute("userId")] string userId)
{
return await Forward(IdentityEndpoints.MfaManage, "disable",
new Dictionary<string, object?>
{
["userId"] = ParseLong(userId),
["method"] = "sms"
});
}
// ── Email OTP ────────────────────────────────────────────────────────────
[HttpPost("email/setup")]
public async Task<object?> SetupEmail([FromBody] MfaOtpSetupRequest request)
{
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.MfaManage, "setup",
new Dictionary<string, object?>
{
["userId"] = request.UserId,
["method"] = "email",
["username"] = request.Username,
["destination"] = request.Destination
});
}
[HttpPost("email/confirm")]
public async Task<object?> ConfirmEmail([FromBody] MfaOtpConfirmRequest request)
{
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.MfaManage, "confirm",
new Dictionary<string, object?>
{
["userId"] = request.UserId,
["method"] = "email",
["code"] = request.Code,
["mfa_state"] = request.MfaState
});
}
[HttpDelete("email/{userId}")]
public async Task<object?> DisableEmail([FromRoute("userId")] string userId)
{
return await Forward(IdentityEndpoints.MfaManage, "disable",
new Dictionary<string, object?>
{
["userId"] = ParseLong(userId),
["method"] = "email"
});
}
private static long ParseLong(string value) =>
long.TryParse(value, out var id) ? id : 0;
}