-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComAtprotoServer_RefreshSession.cs
More file actions
130 lines (109 loc) · 3.51 KB
/
Copy pathComAtprotoServer_RefreshSession.cs
File metadata and controls
130 lines (109 loc) · 3.51 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
using System.Security.Claims;
using System.Text.Json.Nodes;
using dnproto.auth;
using dnproto.ws;
using Microsoft.AspNetCore.Http;
namespace dnproto.pds.xrpc;
public class ComAtprotoServer_RefreshSession : BaseXrpcCommand
{
private static object _refreshLock = new object();
public IResult GetResponse()
{
IncrementStatistics();
//
// Get the refresh jwt from the caller's Authorization header
//
string? originalRefreshJwt = GetAccessJwt();
if(string.IsNullOrEmpty(originalRefreshJwt))
{
return Results.Json(new
{
error = "InvalidRequest",
message = "Missing refresh token"
},
statusCode: 400);
}
//
// Verify refresh jwt
//
ClaimsPrincipal? claimsPrincipal = JwtSecret.VerifyRefreshJwt(originalRefreshJwt, Pds.PdsDb.GetConfigProperty("JwtSecret"));
if (claimsPrincipal == null)
{
return Results.Json(new
{
error = "ExpiredToken",
message = "Token has expired"
},
statusCode: 400);
}
//
// Check did
//
string? userDid = JwtSecret.GetDidFromClaimsPrincipal(claimsPrincipal);
if (userDid != Pds.PdsDb.GetConfigProperty("UserDid"))
{
return Results.Json(new
{
error = "InvalidToken",
message = "Token did not match expected user"
},
statusCode: 401);
}
//
// Check that refreshJwt exists in db.
//
bool refreshJwtExists = false;
lock(_refreshLock)
{
refreshJwtExists = Pds.PdsDb.LegacySessionExistsForRefreshJwt(originalRefreshJwt!);
Pds.PdsDb.DeleteLegacySessionForRefreshJwt(originalRefreshJwt!);
}
if (!refreshJwtExists)
{
return Results.Json(new
{
error = "InvalidToken",
message = "Token not found"
},
statusCode: 401);
}
//
// Generate new tokens
//
string? handle = Pds.PdsDb.GetConfigProperty("UserHandle");
string? accessJwt = JwtSecret.GenerateAccessJwt(userDid, Pds.PdsDb.GetConfigProperty("PdsDid"), Pds.PdsDb.GetConfigProperty("JwtSecret"));
string? newRefreshJwt = JwtSecret.GenerateRefreshJwt(userDid, Pds.PdsDb.GetConfigProperty("PdsDid"), Pds.PdsDb.GetConfigProperty("JwtSecret"));
if(string.IsNullOrEmpty(accessJwt) || string.IsNullOrEmpty(newRefreshJwt))
{
return Results.Json(new
{
error = "ServerError",
message = "Failed to generate new tokens"
},
statusCode: 401);
}
//
// Insert new tokens into db
//
var session = new LegacySession
{
CreatedDate = PdsDb.FormatDateTimeForDb(DateTimeOffset.UtcNow),
AccessJwt = accessJwt,
RefreshJwt = newRefreshJwt,
IpAddress = GetCallerIpAddress() ?? "unknown",
UserAgent = GetCallerUserAgent() ?? "unknown"
};
Pds.PdsDb.CreateLegacySession(session);
//
// Return session info
//
return Results.Json(new
{
did = userDid,
handle = handle,
accessJwt = accessJwt,
refreshJwt = newRefreshJwt
},
statusCode: 200);
}
}