-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJwtAuth.cs
More file actions
302 lines (254 loc) · 10.8 KB
/
Copy pathJwtAuth.cs
File metadata and controls
302 lines (254 loc) · 10.8 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
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
using System.IdentityModel.Tokens.Jwt;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
namespace NpgsqlRestClient;
// AOT-compatible JSON serialization context
[JsonSerializable(typeof(JwtErrorResponse))]
[JsonSerializable(typeof(JwtTokenResponse))]
internal partial class JwtJsonContext : JsonSerializerContext
{
}
internal class JwtErrorResponse
{
[JsonPropertyName("error")]
public string Error { get; set; } = null!;
}
internal class JwtTokenResponse
{
[JsonPropertyName("accessToken")]
public string AccessToken { get; set; } = null!;
[JsonPropertyName("refreshToken")]
public string RefreshToken { get; set; } = null!;
[JsonPropertyName("tokenType")]
public string TokenType { get; set; } = "Bearer";
[JsonPropertyName("expiresIn")]
public int ExpiresIn { get; set; }
[JsonPropertyName("refreshExpiresIn")]
public int RefreshExpiresIn { get; set; }
}
public class JwtTokenConfig
{
public string Scheme { get; set; } = JwtBearerDefaults.AuthenticationScheme;
public string Secret { get; set; } = null!;
public string? Issuer { get; set; }
public string? Audience { get; set; }
public int ExpireMinutes { get; set; } = 60;
public int RefreshExpireDays { get; set; } = 7;
public bool ValidateIssuer { get; set; }
public bool ValidateAudience { get; set; }
public bool ValidateLifetime { get; set; } = true;
public bool ValidateIssuerSigningKey { get; set; } = true;
public TimeSpan ClockSkew { get; set; } = TimeSpan.FromMinutes(5);
public string? RefreshPath { get; set; }
public SymmetricSecurityKey GetSecurityKey() => new(Encoding.UTF8.GetBytes(Secret));
public SigningCredentials GetSigningCredentials() => new(GetSecurityKey(), SecurityAlgorithms.HmacSha256);
public TokenValidationParameters GetTokenValidationParameters() => new()
{
ValidateIssuer = ValidateIssuer,
ValidateAudience = ValidateAudience,
ValidateLifetime = ValidateLifetime,
ValidateIssuerSigningKey = ValidateIssuerSigningKey,
ValidIssuer = Issuer,
ValidAudience = Audience,
IssuerSigningKey = GetSecurityKey(),
ClockSkew = ClockSkew
};
}
public class JwtTokenGenerator
{
private readonly JwtTokenConfig _config;
public JwtTokenGenerator(JwtTokenConfig config)
{
_config = config;
}
public (string accessToken, string refreshToken, DateTime accessExpires, DateTime refreshExpires) GenerateTokens(ClaimsPrincipal principal)
{
var accessExpires = DateTime.UtcNow.AddMinutes(_config.ExpireMinutes);
var refreshExpires = DateTime.UtcNow.AddDays(_config.RefreshExpireDays);
var accessToken = GenerateAccessToken(principal.Claims, accessExpires);
var refreshToken = GenerateRefreshToken(principal.Claims, refreshExpires);
return (accessToken, refreshToken, accessExpires, refreshExpires);
}
public string GenerateAccessToken(IEnumerable<Claim> claims, DateTime expires)
{
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(claims),
Expires = expires,
Issuer = _config.Issuer,
Audience = _config.Audience,
SigningCredentials = _config.GetSigningCredentials()
};
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
public string GenerateRefreshToken(IEnumerable<Claim> claims, DateTime expires)
{
// Add a claim to identify this as a refresh token
var refreshClaims = claims.ToList();
refreshClaims.Add(new Claim("token_type", "refresh"));
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(refreshClaims),
Expires = expires,
Issuer = _config.Issuer,
Audience = _config.Audience,
SigningCredentials = _config.GetSigningCredentials()
};
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
public ClaimsPrincipal? ValidateRefreshToken(string refreshToken)
{
var tokenHandler = new JwtSecurityTokenHandler();
var validationParameters = _config.GetTokenValidationParameters();
// For refresh tokens, we want to validate even if the access token would be expired
// but the refresh token itself should not be expired
validationParameters.ValidateLifetime = true;
try
{
var principal = tokenHandler.ValidateToken(refreshToken, validationParameters, out var validatedToken);
// Verify this is a refresh token
var tokenTypeClaim = principal.FindFirst("token_type");
if (tokenTypeClaim?.Value != "refresh")
{
return null;
}
return principal;
}
catch
{
return null;
}
}
}
public class JwtRefreshAuth
{
public JwtRefreshAuth(JwtTokenConfig? jwtConfig, WebApplication app, ILogger? logger)
{
if (jwtConfig is null ||
string.IsNullOrEmpty(jwtConfig.RefreshPath) ||
string.IsNullOrEmpty(jwtConfig.Secret))
{
return;
}
var refreshPath = jwtConfig.RefreshPath;
var tokenGenerator = new JwtTokenGenerator(jwtConfig);
app.Use(async (context, next) =>
{
if (!context.Request.Path.Equals(refreshPath, StringComparison.OrdinalIgnoreCase))
{
await next(context);
return;
}
if (!string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase))
{
await next(context);
return;
}
string refreshToken;
try
{
using var reader = new StreamReader(context.Request.Body);
var body = await reader.ReadToEndAsync();
var node = JsonNode.Parse(string.IsNullOrWhiteSpace(body) ? "{}" : body);
refreshToken = node!["refreshToken"]?.ToString() ?? throw new ArgumentException("refreshToken is null");
}
catch (Exception ex)
{
logger?.LogError(ex, "Failed to read refresh token from request body.");
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(JsonSerializer.Serialize(
new JwtErrorResponse { Error = "Invalid request body" },
JwtJsonContext.Default.JwtErrorResponse));
return;
}
var principal = tokenGenerator.ValidateRefreshToken(refreshToken);
if (principal is null)
{
logger?.LogWarning("Invalid or expired refresh token.");
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(JsonSerializer.Serialize(
new JwtErrorResponse { Error = "Invalid or expired refresh token" },
JwtJsonContext.Default.JwtErrorResponse));
return;
}
// Filter out the token_type claim when creating new tokens
var claims = principal.Claims.Where(c => c.Type != "token_type").ToList();
var newPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, jwtConfig.Scheme));
var (accessToken, newRefreshToken, accessExpires, refreshExpires) = tokenGenerator.GenerateTokens(newPrincipal);
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "application/json";
var response = new JwtTokenResponse
{
AccessToken = accessToken,
RefreshToken = newRefreshToken,
TokenType = "Bearer",
ExpiresIn = (int)(accessExpires - DateTime.UtcNow).TotalSeconds,
RefreshExpiresIn = (int)(refreshExpires - DateTime.UtcNow).TotalSeconds
};
await context.Response.WriteAsync(JsonSerializer.Serialize(response, JwtJsonContext.Default.JwtTokenResponse));
});
logger?.LogDebug("JWT refresh endpoint registered at {RefreshPath}", refreshPath);
}
}
/// <summary>
/// Static class that provides JWT token generation for login endpoints.
/// This is used by the NpgsqlRest login handler when JWT authentication scheme is specified.
/// </summary>
public static class JwtLoginHandler
{
private static JwtTokenConfig? _config;
private static JwtTokenGenerator? _tokenGenerator;
/// <summary>
/// Initializes the JWT login handler with the given configuration.
/// Must be called before any login attempts with JWT scheme.
/// </summary>
public static void Initialize(JwtTokenConfig config)
{
_config = config;
_tokenGenerator = new JwtTokenGenerator(config);
}
/// <summary>
/// Gets whether JWT login handling is configured.
/// </summary>
public static bool IsConfigured => _config is not null && _tokenGenerator is not null;
/// <summary>
/// Gets the configured JWT scheme name, or null if not configured.
/// </summary>
public static string? Scheme => _config?.Scheme;
/// <summary>
/// Generates JWT tokens for the given claims principal and writes them to the response.
/// Returns true if handled, false if JWT is not configured.
/// </summary>
public static async Task<bool> HandleLoginAsync(HttpContext context, ClaimsPrincipal principal)
{
if (_config is null || _tokenGenerator is null)
{
return false;
}
var (accessToken, refreshToken, accessExpires, refreshExpires) = _tokenGenerator.GenerateTokens(principal);
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "application/json";
var response = new JwtTokenResponse
{
AccessToken = accessToken,
RefreshToken = refreshToken,
TokenType = "Bearer",
ExpiresIn = (int)(accessExpires - DateTime.UtcNow).TotalSeconds,
RefreshExpiresIn = (int)(refreshExpires - DateTime.UtcNow).TotalSeconds
};
await context.Response.WriteAsync(JsonSerializer.Serialize(response, JwtJsonContext.Default.JwtTokenResponse));
return true;
}
}