metrics service
fixes to auth info
This commit is contained in:
26
AobaServer/Services/AuthConfigService.cs
Normal file
26
AobaServer/Services/AuthConfigService.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using AobaServer.Models;
|
||||
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace AobaServer.Services;
|
||||
|
||||
public class AuthConfigService(IMongoDatabase db)
|
||||
{
|
||||
public IMongoCollection<AuthInfo> _authInfo = db.GetCollection<AuthInfo>("auth_config");
|
||||
|
||||
public async Task<AuthInfo> GetAuthInfoAsync(string issuer, string audience)
|
||||
{
|
||||
var info = await _authInfo.Find("{}").FirstOrDefaultAsync();
|
||||
if(info != null)
|
||||
return info;
|
||||
|
||||
info = AuthInfo.Create(issuer, audience);
|
||||
await _authInfo.InsertOneAsync(info);
|
||||
return info;
|
||||
}
|
||||
|
||||
public Task<AuthInfo> GetDefaultAuthInfoAsync()
|
||||
{
|
||||
return GetAuthInfoAsync("aobaV2", "aoba");
|
||||
}
|
||||
}
|
||||
33
AobaServer/Services/MetricsRpcService.cs
Normal file
33
AobaServer/Services/MetricsRpcService.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Aoba.RPC;
|
||||
using Aoba.RPC.Auth;
|
||||
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
|
||||
using Grpc.Core;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
|
||||
namespace AobaServer.Services;
|
||||
|
||||
public class MetricsRpcService(AuthConfigService authConfig): Aoba.RPC.Metrics.Metrics.MetricsBase
|
||||
{
|
||||
[AllowAnonymous]
|
||||
public override async Task<Jwt> GetToken(Empty request, ServerCallContext context)
|
||||
{
|
||||
var authInfo = await authConfig.GetAuthInfoAsync("aoba", "metrics");
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
|
||||
var jwt = handler.CreateEncodedJwt(new SecurityTokenDescriptor
|
||||
{
|
||||
Audience = authInfo.Audience,
|
||||
Issuer = authInfo.Issuer,
|
||||
IssuedAt = DateTime.UtcNow,
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(authInfo.SecureKey), SecurityAlgorithms.HmacSha256)
|
||||
});
|
||||
|
||||
return new Jwt { Token = jwt };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user