-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComAtprotoServer_CreateSession.cs
More file actions
92 lines (76 loc) · 2.88 KB
/
Copy pathComAtprotoServer_CreateSession.cs
File metadata and controls
92 lines (76 loc) · 2.88 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
using System.Text.Json.Nodes;
using dnproto.auth;
using dnproto.ws;
using Microsoft.AspNetCore.Http;
namespace dnproto.pds.xrpc;
public class ComAtprotoServer_CreateSession : BaseXrpcCommand
{
public IResult GetResponse()
{
IncrementStatistics();
//
// Get body input
//
JsonNode? requestBody = GetRequestBodyAsJson();
string? identifier, password;
if(!CheckRequestBodyParam(requestBody, "identifier", out identifier)
|| ! CheckRequestBodyParam(requestBody, "password", out password)
|| string.IsNullOrEmpty(identifier)
|| string.IsNullOrEmpty(password)
)
{
return Results.Json(new { error = "InvalidRequest", message = "Error: invalid params." }, statusCode: 400);
}
//
// Resolve actor info
//
ActorInfo? actorInfo = BlueskyClient.ResolveActorInfo(identifier);
bool actorExists = actorInfo != null;
//
// Get account hashed password
//
string? storedHashedPassword = Pds.PdsDb.GetConfigProperty("UserHashedPassword");
bool passwordMatches = PasswordHasher.VerifyPassword(storedHashedPassword, password);
//
// Generate JWT tokens
//
string? accessJwt = null;
string? refreshJwt = null;
if(actorExists && passwordMatches)
{
Pds.Logger.LogInfo($"[AUTH] [LEGACY] Successful login. ip={GetCallerIpAddress()} userAgent={GetCallerUserAgent()}");
accessJwt = JwtSecret.GenerateAccessJwt(actorInfo?.Did, Pds.PdsDb.GetConfigProperty("PdsDid"), Pds.PdsDb.GetConfigProperty("JwtSecret"));
refreshJwt = JwtSecret.GenerateRefreshJwt(actorInfo?.Did, Pds.PdsDb.GetConfigProperty("PdsDid"), Pds.PdsDb.GetConfigProperty("JwtSecret"));
//
// Insert into db
//
if(string.IsNullOrEmpty(accessJwt) == false && string.IsNullOrEmpty(refreshJwt) == false)
{
var session = new LegacySession
{
CreatedDate = PdsDb.FormatDateTimeForDb(DateTimeOffset.UtcNow),
AccessJwt = accessJwt,
RefreshJwt = refreshJwt,
IpAddress = GetCallerIpAddress() ?? "unknown",
UserAgent = GetCallerUserAgent() ?? "unknown"
};
Pds.PdsDb.CreateLegacySession(session);
}
}
else
{
Pds.Logger.LogWarning($"[AUTH] [LEGACY] Failed login attempt. ip={GetCallerIpAddress()} userAgent={GetCallerUserAgent()}");
}
//
// Return session info
//
return Results.Json(new
{
did = actorInfo?.Did,
handle = actorInfo?.Handle,
accessJwt = accessJwt,
refreshJwt = refreshJwt
},
statusCode: 200);
}
}