-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add PssoPhoneNumberGrantValidator
- Loading branch information
Showing
6 changed files
with
115 additions
and
3 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
9 changes: 9 additions & 0 deletions
9
...vices/Masa.Auth.Service.Admin/Application/Subjects/Queries/ThirdPartyUserByUserIdQuery.cs
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,9 @@ | ||
// 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.Service.Admin.Application.Subjects.Queries; | ||
|
||
public record ThirdPartyUserByUserIdQuery(Guid UserId, Guid ThirdPartyIdpId) : Query<UserModel?> | ||
{ | ||
public override UserModel? Result { get; set; } | ||
} |
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
75 changes: 75 additions & 0 deletions
75
src/Web/Masa.Auth.Web.Sso/Infrastructure/Validations/PssoPhoneNumberGrantValidator.cs
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,75 @@ | ||
// 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 PssoPhoneNumberGrantValidator : IExtensionGrantValidator | ||
{ | ||
IAuthClient _authClient; | ||
LocalLoginByPhoneNumberAgent _localLoginByPhoneNumber; | ||
|
||
public string GrantType { get; } = "psso_phone"; | ||
|
||
public PssoPhoneNumberGrantValidator(IAuthClient authClient, LocalLoginByPhoneNumberAgent localLoginByPhoneNumber) | ||
{ | ||
_authClient = authClient; | ||
_localLoginByPhoneNumber = localLoginByPhoneNumber; | ||
} | ||
|
||
public async Task ValidateAsync(ExtensionGrantValidationContext context) | ||
{ | ||
var phoneNumber = context.Request.Raw["PhoneNumber"]; | ||
var spToken = context.Request.Raw["SpToken"]; | ||
if (string.IsNullOrEmpty(phoneNumber) || string.IsNullOrEmpty(spToken)) | ||
{ | ||
context.Result = new GrantValidationResult | ||
{ | ||
IsError = true, | ||
Error = "Must provide phone number and spToken", | ||
ErrorDescription = "Must provide phone number and spToken" | ||
}; | ||
return; | ||
} | ||
|
||
var (success, errorMsg) = await _localLoginByPhoneNumber.VerifyPhoneWithTokenAsync(phoneNumber, spToken); | ||
if (success) | ||
{ | ||
var user = await _authClient.UserService.GetByPhoneNumberAsync(phoneNumber); | ||
if (user is null) | ||
{ | ||
context.Result = new GrantValidationResult | ||
{ | ||
IsError = true, | ||
Error = $"User {phoneNumber} does not exist", | ||
ErrorDescription = errorMsg | ||
}; | ||
} | ||
else | ||
{ | ||
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(), "local", claims); | ||
} | ||
} | ||
else | ||
context.Result = new GrantValidationResult | ||
{ | ||
IsError = true, | ||
Error = errorMsg, | ||
ErrorDescription = errorMsg | ||
}; | ||
} | ||
} |
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