backend complete

This commit is contained in:
2025-05-17 13:48:10 -04:00
parent 3ac1fbcd8e
commit bb740cbefc
12 changed files with 173 additions and 34 deletions

View File

@@ -1,6 +1,15 @@
using MongoDB.Bson;
using AobaCore.Models;
using AobaServer.Models;
using Microsoft.IdentityModel.Tokens;
using MongoDB.Bson;
using MongoDB.Driver;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
namespace AobaServer.Utils;
public static class Extensions
@@ -14,5 +23,18 @@ public static class Extensions
return ObjectId.Empty;
}
public static string GetToken(this User user, AuthInfo authInfo)
{
var handler = new JwtSecurityTokenHandler();
var signCreds = new SigningCredentials(new SymmetricSecurityKey(authInfo.SecureKey), SecurityAlgorithms.HmacSha256);
var identity = user.GetIdentity();
var token = handler.CreateEncodedJwt(authInfo.Issuer, authInfo.Audience, identity, notBefore: DateTime.Now, expires: null, issuedAt: DateTime.Now, signCreds);
return token;
}
public static ObjectId GetId(this ClaimsPrincipal user)
{
return user.FindFirstValue(ClaimTypes.NameIdentifier).ToObjectId();
}
}