-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System.Diagnostics; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Security.Cryptography; | ||
using System.Text.Json.Serialization; | ||
using Flurl; | ||
using Flurl.Http; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Net.Vatprc.Uniapi.Services; | ||
|
||
public class VatprcAtcService(IOptions<VatprcAtcService.Option> Options, | ||
IOptions<TokenService.Option> TokenOptions) | ||
{ | ||
public static WebApplicationBuilder ConfigureOn(WebApplicationBuilder builder) | ||
{ | ||
builder.Services.Configure<Option>(builder.Configuration.GetSection(Option.LOCATION)); | ||
builder.Services.ConfigureOptions<OptionConfigure>(); | ||
builder.Services.AddSingleton<VatprcAtcService>(); | ||
return builder; | ||
} | ||
|
||
public class Option | ||
{ | ||
public const string LOCATION = "Authentication:Internal:VatprcAtcService"; | ||
|
||
public required string Endpoint { get; set; } | ||
|
||
public required string TokenAudience { get; set; } | ||
|
||
public required string PrivateKey { get; set; } | ||
|
||
public required string PublicKey { get; set; } | ||
|
||
public SecurityKey SecurityKey { get; set; } = null!; | ||
|
||
public SigningCredentials Credentials { get; set; } = null!; | ||
} | ||
|
||
public class OptionConfigure : IConfigureOptions<Option> | ||
{ | ||
public void Configure(Option opts) | ||
{ | ||
var key = ECDsa.Create(); | ||
key.ImportFromPem(opts.PrivateKey); | ||
opts.SecurityKey = new ECDsaSecurityKey(key); | ||
opts.Credentials = new(opts.SecurityKey, opts.SecurityKey.KeySize switch | ||
{ | ||
256 => SecurityAlgorithms.EcdsaSha256, | ||
384 => SecurityAlgorithms.EcdsaSha384, | ||
521 => SecurityAlgorithms.EcdsaSha512, | ||
_ => throw new NotSupportedException(), | ||
}); | ||
} | ||
} | ||
|
||
public async Task<IEnumerable<Role>> GetUserRole(string cid) | ||
{ | ||
var claims = new List<Claim> | ||
{ | ||
new (JwtRegisteredClaimNames.Iat, DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64), | ||
new (JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), | ||
}; | ||
var token = new JwtSecurityToken( | ||
issuer: TokenOptions.Value.Issuer, | ||
audience: Options.Value.TokenAudience, | ||
expires: DateTime.Now.Add(TimeSpan.FromMinutes(5)), | ||
notBefore: DateTime.Now, | ||
claims: claims, | ||
signingCredentials: Options.Value.Credentials); | ||
var tokenStr = new JwtSecurityTokenHandler().WriteToken(token); | ||
return await Options.Value.Endpoint | ||
.AppendPathSegments("v1/internal/users/", cid, "/roles") | ||
.WithHeader("User-Agent", UniapiUserAgent) | ||
.WithHeader("Token", tokenStr) | ||
.GetJsonAsync<IEnumerable<Role>>() ?? | ||
throw new Exception("Unexpected null on fetch vatprc atcapi data"); | ||
} | ||
|
||
public class Role | ||
{ | ||
[JsonPropertyName("id")] | ||
public long Id { get; set; } | ||
|
||
[JsonPropertyName("name")] | ||
public string Name { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("expiration_time")] | ||
public DateTimeOffset? ExpirationTime { get; set; } | ||
|
||
[JsonPropertyName("all_superroles")] | ||
public Role[] AllSuperroles { get; set; } = []; | ||
} | ||
} |