Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revoke account #51

Open
wants to merge 10 commits into
base: release/1.6.6-back
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CAVerifierServer.Account.Dtos;

public class VerifyRevokeCodeResponseDto
{
public bool Success { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using CAVerifierServer.Account.Dtos;
using CAVerifierServer.Verifier.Dtos;
using Volo.Abp.Application.Services;

Expand All @@ -21,4 +22,5 @@ public interface IAccountAppService : IApplicationService
Task<ResponseResultDto<VerifierCodeDto>> VerifyFacebookTokenAsync(VerifyTokenRequestDto tokenRequestDto);
Task<ResponseResultDto<VerifyFacebookTokenResponseDto>> VerifyFacebookAccessTokenAsync(string accessToken);
Task<ResponseResultDto<VerifyTwitterTokenDto>> VerifyTwitterTokenAsync(VerifyTokenRequestDto tokenRequestDto);
Task<VerifyRevokeCodeResponseDto> VerifyRevokeCodeAsync(VerifyRevokeCodeDto revokeCodeDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Text.Json.Serialization;

namespace CAVerifierServer.Account.Dtos;

public class VerifyRevokeCodeDto
{
[JsonPropertyName("guardianIdentifier")]
public string GuardianIdentifier { get; set; }

[JsonPropertyName("VerifierSessionId")]
public Guid VerifierSessionId{ get; set; }

[JsonPropertyName("VerifyCode")]
public string VerifyCode{ get; set; }

[JsonPropertyName("Type")]
public string Type{ get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;
using CAVerifierServer.Account.Dtos;

namespace CAVerifierServer.VerifyRevokeCode;

public interface IVerifyRevokeCodeValidator
{
string Type { get; }

Task<bool> VerifyRevokeCodeAsync(VerifyRevokeCodeDto verifyRevokeCodeDto);
}
35 changes: 33 additions & 2 deletions src/CAVerifierServer.Application/Account/AccountAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Amazon.SimpleNotificationService.Util;
using CAVerifierServer.Account.Dtos;
using CAVerifierServer.Verifier.Dtos;
using CAVerifierServer.Application;
using CAVerifierServer.Contracts;
using CAVerifierServer.Grains.Grain;
using CAVerifierServer.Grains.Grain.ThirdPartyVerification;
using CAVerifierServer.Options;
using CAVerifierServer.VerifyCodeSender;
using CAVerifierServer.VerifyRevokeCode;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -39,6 +40,7 @@ public class AccountAppService : CAVerifierServerAppService, IAccountAppService
private readonly IContractsProvider _contractsProvider;
private readonly FacebookOptions _facebookOptions;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IEnumerable<IVerifyRevokeCodeValidator> _revokeCodeValidators;

private const string CaServerListKey = "CAServerListKey";

Expand All @@ -48,7 +50,7 @@ public AccountAppService(IClusterClient clusterClient,
IEnumerable<IVerifyCodeSender> verifyCodeSenders, IObjectMapper objectMapper,
IOptions<WhiteListExpireTimeOptions> whiteListExpireTimeOption, ILogger<AccountAppService> logger,
IContractsProvider contractsProvider, IOptionsSnapshot<FacebookOptions> facebookOptions,
IHttpClientFactory httpClientFactory)
IHttpClientFactory httpClientFactory, IEnumerable<IVerifyRevokeCodeValidator> revokeCodeValidators)
{
_clusterClient = clusterClient;
_distributedCache = distributedCache;
Expand All @@ -57,6 +59,7 @@ public AccountAppService(IClusterClient clusterClient,
_logger = logger;
_contractsProvider = contractsProvider;
_httpClientFactory = httpClientFactory;
_revokeCodeValidators = revokeCodeValidators;
_facebookOptions = facebookOptions.Value;
_whiteListExpireTimeOptions = whiteListExpireTimeOption.Value;
_chainOptions = chainOptions.Value;
Expand Down Expand Up @@ -442,6 +445,34 @@ await grain.VerifyTwitterTokenAsync(
}
}

public async Task<VerifyRevokeCodeResponseDto> VerifyRevokeCodeAsync(
VerifyRevokeCodeDto revokeCodeDto)
{
var revokeValidator = _revokeCodeValidators.FirstOrDefault(t => t.Type == revokeCodeDto.Type);
if (null == revokeValidator)
{
return new VerifyRevokeCodeResponseDto
{
Success = false
};
}

var result = await revokeValidator.VerifyRevokeCodeAsync(revokeCodeDto);

if (result)
{
return new VerifyRevokeCodeResponseDto
{
Success = true
};
}

return new VerifyRevokeCodeResponseDto
{
Success = false
};
}


private async Task<DidServerList> GetDidServerListAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CAVerifierServer.Options;
using CAVerifierServer.Phone;
using CAVerifierServer.VerifyCodeSender;
using CAVerifierServer.VerifyRevokeCode;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AutoMapper;
using Volo.Abp.Emailing;
Expand Down Expand Up @@ -43,6 +44,14 @@ public override void ConfigureServices(ServiceConfigurationContext context)
context.Services.AddSingleton<ISMSServiceSender, TwilioSmsMessageSender>();
context.Services.AddSingleton<IVerifyCodeSender, EmailVerifyCodeSender>();
context.Services.AddSingleton<IVerifyCodeSender, PhoneVerifyCodeSender>();

context.Services.AddSingleton<IVerifyRevokeCodeValidator, EmailRevokeCodeValidator>();
context.Services.AddSingleton<IVerifyRevokeCodeValidator, FaceBookRevokeCodeValidator>();
context.Services.AddSingleton<IVerifyRevokeCodeValidator, TwitterRevokeCodeValidator>();
context.Services.AddSingleton<IVerifyRevokeCodeValidator, TelegramRevokeCodeValidator>();
context.Services.AddSingleton<IVerifyRevokeCodeValidator, AppleRevokeCodeValidator>();
context.Services.AddSingleton<IVerifyRevokeCodeValidator, GoogleRevokeCodeValidator>();

context.Services.AddHttpClient();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Threading.Tasks;
using Castle.Core.Logging;
using CAVerifierServer.Account.Dtos;
using CAVerifierServer.Grains.Grain.ThirdPartyVerification;
using Microsoft.Extensions.Logging;
using Orleans;

namespace CAVerifierServer.VerifyRevokeCode;

public class AppleRevokeCodeValidator : IVerifyRevokeCodeValidator
{
private readonly IClusterClient _clusterClient;
private readonly ILogger<AppleRevokeCodeValidator> _logger;

public AppleRevokeCodeValidator(IClusterClient clusterClient, ILogger<AppleRevokeCodeValidator> logger)
{
_clusterClient = clusterClient;
_logger = logger;
}

public string Type => "Apple";

public async Task<bool> VerifyRevokeCodeAsync(VerifyRevokeCodeDto revokeCodeDto)
{
var grain = _clusterClient.GetGrain<IThirdPartyVerificationGrain>(revokeCodeDto.VerifyCode);
try
{
await grain.ValidateTokenAsync(revokeCodeDto.VerifyCode);
return true;
}
catch (Exception e)
{
_logger.LogError(e,"validate apple token failed :{message}", e.Message);
return false;

Check warning on line 35 in src/CAVerifierServer.Application/VerifyRevokeCode/AppleRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/AppleRevokeCodeValidator.cs#L32-L35

Added lines #L32 - L35 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Threading.Tasks;
using CAVerifierServer.Account.Dtos;
using CAVerifierServer.Grains.Grain;
using Microsoft.Extensions.Logging;
using Orleans;

namespace CAVerifierServer.VerifyRevokeCode;

public class EmailRevokeCodeValidator : IVerifyRevokeCodeValidator
{
private readonly IClusterClient _clusterClient;
private readonly ILogger<EmailRevokeCodeValidator> _logger;

public EmailRevokeCodeValidator(IClusterClient clusterClient, ILogger<EmailRevokeCodeValidator> logger)
{
_clusterClient = clusterClient;
_logger = logger;
}

public string Type => "Email";
public async Task<bool> VerifyRevokeCodeAsync(VerifyRevokeCodeDto revokeCodeDto)
{
var grain = _clusterClient.GetGrain<IGuardianIdentifierVerificationGrain>(revokeCodeDto.GuardianIdentifier);
var resultDto = await grain.VerifyRevokeCodeAsync(revokeCodeDto);
if (resultDto.Success)
{
return true;
}
_logger.LogError("validate Email verifyCode failed:{reason}",resultDto.Message);
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using CAVerifierServer.Account;
using CAVerifierServer.Account.Dtos;
using CAVerifierServer.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace CAVerifierServer.VerifyRevokeCode;

public class FaceBookRevokeCodeValidator : IVerifyRevokeCodeValidator
{
private readonly ILogger<FaceBookRevokeCodeValidator> _logger;
private readonly FacebookOptions _facebookOptions;
private readonly IHttpClientFactory _httpClientFactory;

public FaceBookRevokeCodeValidator(ILogger<FaceBookRevokeCodeValidator> logger,
IOptionsSnapshot<FacebookOptions> facebookOptions, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_facebookOptions = facebookOptions.Value;
_httpClientFactory = httpClientFactory;
}

public string Type => "Facebook";

public async Task<bool> VerifyRevokeCodeAsync(VerifyRevokeCodeDto revokeCodeDto)
{

Check warning on line 31 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L31

Added line #L31 was not covered by tests
try
{
var result = await VerifyFacebookAccessTokenAsync(revokeCodeDto.VerifyCode);

Check warning on line 34 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L33-L34

Added lines #L33 - L34 were not covered by tests
if (result)
{
return true;

Check warning on line 37 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L36-L37

Added lines #L36 - L37 were not covered by tests
}

_logger.LogError("validate Facebook token failed");
return false;

Check warning on line 41 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L40-L41

Added lines #L40 - L41 were not covered by tests
}
catch (Exception e)
{
_logger.LogError(e, "validate Facebook token failed:{reason}", e.Message);
return false;

Check warning on line 46 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L43-L46

Added lines #L43 - L46 were not covered by tests
}
}

Check warning on line 48 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L48

Added line #L48 was not covered by tests

private async Task<bool> VerifyFacebookAccessTokenAsync(
string accessToken)
{
var appToken = _facebookOptions.AppId + "%7C" + _facebookOptions.AppSecret;
var requestUrl =
"https://graph.facebook.com/debug_token?access_token=" + appToken + "&input_token=" + accessToken;

Check warning on line 55 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L52-L55

Added lines #L52 - L55 were not covered by tests
try
{
var client = _httpClientFactory.CreateClient();
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, requestUrl));

Check warning on line 59 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L57-L59

Added lines #L57 - L59 were not covered by tests

var result = await response.Content.ReadAsStringAsync();

Check warning on line 61 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L61

Added line #L61 was not covered by tests
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.LogError("{Message}", response.ToString());
return false;

Check warning on line 65 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L63-L65

Added lines #L63 - L65 were not covered by tests
}

if (response.IsSuccessStatusCode)
{
var verifyUserInfo = JsonConvert.DeserializeObject<VerifyFacebookResultResponse>(result);

Check warning on line 70 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L69-L70

Added lines #L69 - L70 were not covered by tests
if (verifyUserInfo == null)
{
_logger.LogError("Verify Facebook userInfo fail.");
return false;

Check warning on line 74 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L72-L74

Added lines #L72 - L74 were not covered by tests
}

if (!verifyUserInfo.Data.IsValid)
{
_logger.LogError("Verify accessToken from Facebook fail.");
return false;

Check warning on line 80 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L78-L80

Added lines #L78 - L80 were not covered by tests
}

if (verifyUserInfo.Data.ExpiresAt >= DateTimeOffset.UtcNow.ToUnixTimeSeconds())
{
return true;

Check warning on line 85 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L84-L85

Added lines #L84 - L85 were not covered by tests
}

_logger.LogError("Token Expired");
return false;

Check warning on line 89 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L88-L89

Added lines #L88 - L89 were not covered by tests
}
}
catch (Exception e)
{
_logger.LogError(e, "Verify AccessToken failed,AccessToken is {accessToken}", accessToken);
return false;

Check warning on line 95 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L91-L95

Added lines #L91 - L95 were not covered by tests
}

return false;
}

Check warning on line 99 in src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/FaceBookRevokeCodeValidator.cs#L98-L99

Added lines #L98 - L99 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Threading.Tasks;
using CAVerifierServer.Account.Dtos;
using CAVerifierServer.Grains.Grain.ThirdPartyVerification;
using Microsoft.Extensions.Logging;
using Orleans;

namespace CAVerifierServer.VerifyRevokeCode;

public class GoogleRevokeCodeValidator : IVerifyRevokeCodeValidator
{

private readonly IClusterClient _clusterClient;
private readonly ILogger<GoogleRevokeCodeValidator> _logger;

public GoogleRevokeCodeValidator(IClusterClient clusterClient, ILogger<GoogleRevokeCodeValidator> logger)
{
_clusterClient = clusterClient;
_logger = logger;
}

public string Type => "Google";
public async Task<bool> VerifyRevokeCodeAsync(VerifyRevokeCodeDto revokeCodeDto)
{
var grain = _clusterClient.GetGrain<IThirdPartyVerificationGrain>(revokeCodeDto.VerifyCode);

try
{
await grain.GetUserInfoFromGoogleAsync(revokeCodeDto.VerifyCode);
return true;
}
catch (Exception e)
{
_logger.LogError(e,"validate google Token error,{error}",e.Message);
return false;

Check warning on line 35 in src/CAVerifierServer.Application/VerifyRevokeCode/GoogleRevokeCodeValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/CAVerifierServer.Application/VerifyRevokeCode/GoogleRevokeCodeValidator.cs#L32-L35

Added lines #L32 - L35 were not covered by tests
}

}
}
Loading