forked from gongxuegit/tiku-backend.net
feat: modularize external providers
This commit is contained in:
54
Tiku.Infrastructure/Auth/SelfHostedIdentityProvider.cs
Normal file
54
Tiku.Infrastructure/Auth/SelfHostedIdentityProvider.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Tiku.Application.Auth;
|
||||
|
||||
namespace Tiku.Infrastructure.Auth;
|
||||
|
||||
internal sealed class SelfHostedIdentityProvider(IAuthService authService) : IIdentityProvider
|
||||
{
|
||||
public async Task<IdentityProviderResult> AuthenticateAsync(
|
||||
IdentityProviderRequest request,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var provider = request.Provider.Trim().ToLowerInvariant().Replace("-", "_", StringComparison.Ordinal);
|
||||
var authenticated = provider switch
|
||||
{
|
||||
"password" => await authService.LoginWithPasswordAsync(
|
||||
new PasswordLoginRequest(
|
||||
request.TenantId,
|
||||
request.Identifier,
|
||||
request.Secret,
|
||||
request.IpAddress,
|
||||
request.UserAgent),
|
||||
cancellationToken),
|
||||
"sms" => await authService.LoginWithSmsAsync(
|
||||
new SmsLoginRequest(
|
||||
request.TenantId,
|
||||
request.Identifier,
|
||||
request.Secret,
|
||||
request.IpAddress,
|
||||
request.UserAgent),
|
||||
cancellationToken),
|
||||
"wechat_web" => await authService.LoginWithWechatWebAsync(
|
||||
new WechatLoginRequest(
|
||||
request.TenantId,
|
||||
request.Secret,
|
||||
request.IpAddress,
|
||||
request.UserAgent),
|
||||
cancellationToken),
|
||||
"wechat_miniapp" => await authService.LoginWithWechatMiniAppAsync(
|
||||
new WechatLoginRequest(
|
||||
request.TenantId,
|
||||
request.Secret,
|
||||
request.IpAddress,
|
||||
request.UserAgent),
|
||||
cancellationToken),
|
||||
_ => throw new AuthProviderNotConfiguredException(provider)
|
||||
};
|
||||
|
||||
return new IdentityProviderResult(
|
||||
provider,
|
||||
authenticated.UserId.ToString("N"),
|
||||
authenticated.Phone,
|
||||
authenticated.Email,
|
||||
authenticated.Name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user