Update
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
using System.Diagnostics;
|
||||
using BackOffice.BFF.Application.Common.Interfaces;
|
||||
using BackOffice.BFF.Infrastructure.Services;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
@@ -11,6 +12,7 @@ public static class ConfigureServices
|
||||
{
|
||||
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddSingleton<IAfrinoIdpService, AfrinoIdpService>();
|
||||
services.AddSingleton<IApplicationContractContext, ApplicationContractContext>();
|
||||
services.AddInfrastructureGrpcServices(configuration);
|
||||
#region AddAuthentication
|
||||
@@ -19,11 +21,24 @@ public static class ConfigureServices
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(jwtBearerOptions =>
|
||||
{
|
||||
jwtBearerOptions.Authority = configuration["Authentication:Authority"];
|
||||
jwtBearerOptions.Audience = configuration["Authentication:Audience"];
|
||||
jwtBearerOptions.TokenValidationParameters.ValidateAudience = false;
|
||||
jwtBearerOptions.TokenValidationParameters.ValidateIssuer = true;
|
||||
jwtBearerOptions.TokenValidationParameters.ValidateIssuerSigningKey = false;
|
||||
//jwtBearerOptions.Authority = configuration["Authentication:Authority"];
|
||||
//jwtBearerOptions.Audience = configuration["Authentication:Audience"];
|
||||
//jwtBearerOptions.TokenValidationParameters.ValidateAudience = false;
|
||||
//jwtBearerOptions.TokenValidationParameters.ValidateIssuer = true;
|
||||
//jwtBearerOptions.TokenValidationParameters.ValidateIssuerSigningKey = false;
|
||||
|
||||
jwtBearerOptions.SaveToken = true;
|
||||
jwtBearerOptions.RequireHttpsMetadata = false;
|
||||
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = configuration["JwtIssuer"],
|
||||
ValidAudience = configuration["JwtAudience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JwtSecurityKey"]))
|
||||
};
|
||||
try
|
||||
{
|
||||
jwtBearerOptions.Events = new JwtBearerEvents
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
using BackOffice.BFF.Application.Common.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BackOffice.BFF.Infrastructure.Services;
|
||||
public class AfrinoIdpService : IAfrinoIdpService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<AfrinoIdpService> _logger;
|
||||
|
||||
public AfrinoIdpService(HttpClient httpClient, ILogger<AfrinoIdpService> logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
|
||||
_httpClient.BaseAddress = new Uri("https://ids.afrino.co");
|
||||
}
|
||||
|
||||
public async Task<bool> SendOtp(string mobile)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mobile))
|
||||
{
|
||||
_logger.LogError(message: "شماره موبایل نمیتواند خالی باشد!!");
|
||||
return false;
|
||||
}
|
||||
|
||||
var response = await _httpClient.PostAsJsonAsync<SendOtpRequestDto>("api/Auth/send-otp", new() { PhoneNumber = mobile });
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorMessage = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"خطا در ارسال OTP: {errorMessage}");
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger.LogInformation("OTP با موفقیت ارسال شد.");
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<string> VerifyOtp(string mobile, string otpCode)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mobile))
|
||||
{
|
||||
_logger.LogError("شماره موبایل نمیتواند خالی باشد!!");
|
||||
throw new ArgumentException("شماره موبایل نمیتواند خالی باشد!!");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(otpCode))
|
||||
{
|
||||
_logger.LogError("رمز پویا نمیتواند خالی باشد!!");
|
||||
throw new ArgumentException("رمز پویا نمیتواند خالی باشد!!");
|
||||
}
|
||||
|
||||
var response = await _httpClient.PostAsJsonAsync<VerifyOtpRequestDto>("api/Auth/verify-otp", new() { PhoneNumber = mobile, OtpCode = otpCode });
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorMessage = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"خطا در فعال سازی OTP: {errorMessage}");
|
||||
throw new HttpRequestException($"خطا در فعال سازی OTP: {errorMessage}");
|
||||
}
|
||||
|
||||
var result = await response.Content.ReadFromJsonAsync<VerifyOtpResponseDto>();
|
||||
if (result == null || string.IsNullOrEmpty(result.Token))
|
||||
{
|
||||
_logger.LogError("توکن دریافتی از سرور خالی است.");
|
||||
throw new Exception("توکن دریافتی از سرور خالی است.");
|
||||
}
|
||||
|
||||
return result.Token;
|
||||
}
|
||||
|
||||
public async Task<string> CreateUser(string mobile)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mobile))
|
||||
{
|
||||
_logger.LogError(message: "شماره موبایل نمیتواند خالی باشد!!");
|
||||
throw new ArgumentException("شماره موبایل نمیتواند خالی باشد!!");
|
||||
}
|
||||
|
||||
var response = await _httpClient.PostAsJsonAsync<CreateUserRequestDto>("api/Auth/create-user", new() { PhoneNumber = mobile });
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorMessage = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"خطا : {errorMessage}");
|
||||
throw new HttpRequestException($"خطا : {errorMessage}");
|
||||
}
|
||||
|
||||
var result = await response.Content.ReadFromJsonAsync<CreateUserResponseDto>();
|
||||
if (result == null || string.IsNullOrEmpty(result.UserId))
|
||||
{
|
||||
_logger.LogError("شناسه کاربر دریافتی از سرور خالی است.");
|
||||
throw new Exception("شناسه کاربر دریافتی از سرور خالی است.");
|
||||
}
|
||||
|
||||
return result.UserId;
|
||||
}
|
||||
|
||||
public async Task<bool> AddRole(string mobile, string role)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mobile))
|
||||
{
|
||||
_logger.LogError(message: "شماره موبایل نمیتواند خالی باشد!!");
|
||||
return false;
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(role))
|
||||
{
|
||||
_logger.LogError(message: "نقش نمیتواند خالی باشد!!");
|
||||
return false;
|
||||
}
|
||||
var response = await _httpClient.PostAsJsonAsync<AddRoleRequestDto>("api/Auth/add-role", new() { PhoneNumber = mobile, Role = role });
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorMessage = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"خطا : {errorMessage}");
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger.LogInformation("نقش با موفقیت اضافه شد.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class SendOtpRequestDto
|
||||
{
|
||||
public string PhoneNumber { get; set; }
|
||||
}
|
||||
|
||||
public class VerifyOtpRequestDto
|
||||
{
|
||||
public string PhoneNumber { get; set; }
|
||||
public string OtpCode { get; set; }
|
||||
}
|
||||
public class VerifyOtpResponseDto
|
||||
{
|
||||
public string Token { get; set; }
|
||||
}
|
||||
public class CreateUserRequestDto
|
||||
{
|
||||
public string PhoneNumber { get; set; }
|
||||
}
|
||||
|
||||
public class CreateUserResponseDto
|
||||
{
|
||||
public string UserId { get; set; }
|
||||
}
|
||||
|
||||
public class AddRoleRequestDto
|
||||
{
|
||||
public string PhoneNumber { get; set; }
|
||||
public string Role { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user