add refresh tokens
All checks were successful
Build and Deploy API / build-and-deploy (push) Successful in 9s
All checks were successful
Build and Deploy API / build-and-deploy (push) Successful in 9s
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text;
|
||||
using System.Security.Claims;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using agologumApi.Models;
|
||||
|
||||
public class JwtService {
|
||||
|
||||
private readonly IConfiguration config_;
|
||||
private readonly AppDbContext db_;
|
||||
|
||||
public JwtService(IConfiguration config) { // why the heck does c# not have initializer lists ?
|
||||
public JwtService(IConfiguration config, AppDbContext db) { // why the heck does c# not have initializer lists ?
|
||||
config_ = config;
|
||||
db_ = db;
|
||||
}
|
||||
|
||||
public string? GenerateJwt(User user) {
|
||||
@@ -42,5 +46,27 @@ public class JwtService {
|
||||
|
||||
}
|
||||
|
||||
public string GenerateRefreshToken() {
|
||||
|
||||
byte[] randomBytes = new byte[64];
|
||||
RandomNumberGenerator.Fill(randomBytes.AsSpan());
|
||||
return Convert.ToBase64String(randomBytes);
|
||||
|
||||
}
|
||||
|
||||
public async Task<RefreshToken?> GetRefreshToken(string refreshTokenString) {
|
||||
return await db_.RefreshTokens.FirstOrDefaultAsync(u => u.Token == refreshTokenString);
|
||||
}
|
||||
|
||||
public async Task<RefreshToken> AddRefreshToken(RefreshToken refreshToken) {
|
||||
db_.RefreshTokens.Add(refreshToken);
|
||||
await db_.SaveChangesAsync();
|
||||
return refreshToken;
|
||||
}
|
||||
|
||||
// helper to get the User from the id that exists in a refresh token object
|
||||
public async Task<User?> GetUser(string id) {
|
||||
return await db_.Users.FindAsync(id);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user