Skip to content

Commit

Permalink
feat: PssoPhoneCodeGrantValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 committed Mar 13, 2024
1 parent af90dab commit 7b44082
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="FluentValidation" Version="11.4.0" />
<PackageReference Include="Magicodes.IE.Csv" Version="2.6.4" />
<PackageReference Include="Masa.BuildingBlocks.Authentication.OpenIdConnect.Domain" Version="$(MasaFrameworkPackageVersion)" />
<PackageReference Include="Masa.BuildingBlocks.Authentication.OpenIdConnect.Models" Version="$(MasaFrameworkPackageVersion)" />
<PackageReference Include="Masa.BuildingBlocks.Authentication.OpenIdConnect.Models" Version="1.0.1-preview.10.3" />
<PackageReference Include="Masa.BuildingBlocks.StackSdks.Auth.Contracts" Version="1.0.1-preview.10.2" />
<!--<PackageReference Include="Masa.BuildingBlocks.StackSdks.Auth.Contracts" Version="$(MasaFrameworkPackageVersion)" />-->
<PackageReference Include="Masa.Contrib.Configuration.ConfigurationApi.Dcc" Version="$(MasaFrameworkPackageVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="AspNet.Security.OAuth.Apple" Version="6.0.15" />
<PackageReference Include="AspNet.Security.OAuth.GitHub" Version="6.0.10" />
<PackageReference Include="AspNet.Security.OAuth.Weixin" Version="6.0.11" />
<PackageReference Include="Masa.BuildingBlocks.Authentication.OpenIdConnect.Models" Version="$(MasaFrameworkPackageVersion)" />
<PackageReference Include="Masa.BuildingBlocks.Authentication.OpenIdConnect.Models" Version="1.0.1-preview.10.3" />
<PackageReference Include="Masa.BuildingBlocks.StackSdks.Auth.Contracts" Version="1.0.1-preview.10.2" />
<PackageReference Include="Masa.Contrib.Exceptions" Version="$(MasaFrameworkPackageVersion)" />
<PackageReference Include="Masa.Contrib.Caching.Distributed.StackExchangeRedis" Version="$(MasaFrameworkPackageVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Masa.Auth.Web.Sso.Infrastructure.Validations;
public class ImpersonationGrantValidator : IExtensionGrantValidator
{
IAuthClient _authClient;
public string GrantType { get; } = "impersonation";
public string GrantType { get; } = BuildingBlocks.Authentication.OpenIdConnect.Models.Constans.GrantType.IMPERSONATION;

const string IMPERSONATOR_USER_ID = "http://Lonsid.org/identity/claims/impersonatorUserId";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

namespace Masa.Auth.Web.Sso.Infrastructure.Validations;

public class PssoPhoneCodeGrantValidator : IExtensionGrantValidator
{
IAuthClient _authClient;

public string GrantType { get; } = BuildingBlocks.Authentication.OpenIdConnect.Models.Constans.GrantType.PSSO_PHONE_CODE;

public PssoPhoneCodeGrantValidator(IAuthClient authClient)
{
_authClient = authClient;
}

public async Task ValidateAsync(ExtensionGrantValidationContext context)
{
var phoneNumber = context.Request.Raw["PhoneNumber"];
var code = context.Request.Raw["Code"];
if (string.IsNullOrEmpty(phoneNumber) || string.IsNullOrEmpty(code))
throw new UserFriendlyException("must provider phone number and msg code");

var user = await _authClient.UserService.LoginByPhoneNumberAsync(new LoginByPhoneNumberModel
{
PhoneNumber = phoneNumber,
Code = code
});
if (user != null)
{
var authUser = await _authClient.UserService.GetThirdPartyUserByUserIdAsync(new GetThirdPartyUserByUserIdModel
{
Scheme = "Psso",
UserId = user.Id
});

var claims = new List<Claim>();
if (authUser != null)
{
foreach (var item in authUser.ClaimData)
{
claims.Add(new Claim(item.Key, item.Value));
}
}

context.Result = new GrantValidationResult(user.Id.ToString(), "sms", claims);
}
else
{
context.Result = new GrantValidationResult(
TokenRequestErrors.InvalidGrant,
"invalid custom credential");
}

}
}

This file was deleted.

4 changes: 3 additions & 1 deletion src/Web/Masa.Auth.Web.Sso/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
.AddExtensionGrantValidator<PhoneCodeGrantValidator>()
.AddExtensionGrantValidator<LocalPhoneNumberGrantValidator>()
.AddExtensionGrantValidator<ThirdPartyIdpGrantValidator>()
.AddExtensionGrantValidator<LdapGrantValidator>();
.AddExtensionGrantValidator<LdapGrantValidator>()
.AddExtensionGrantValidator<PssoPhoneCodeGrantValidator>()
.AddExtensionGrantValidator<ImpersonationGrantValidator>();

if (builder.Environment.IsDevelopment())
{
Expand Down

0 comments on commit 7b44082

Please sign in to comment.