logto 客户端模式对接测试完成
continuous-integration/drone/push Build is passing Details

IRC_NewDev
hang 2024-10-29 23:11:37 +08:00
parent c6c8daaadf
commit c713d0bc95
3 changed files with 89 additions and 21 deletions

View File

@ -1,7 +1,9 @@
using Amazon.Auth.AccessControlPolicy;
using Amazon.SecurityToken;
using Azure.Core;
using IRaCIS.Application.Contracts;
using IRaCIS.Application.Interfaces;
using IRaCIS.Core.API.OAuth;
using IRaCIS.Core.Application.Auth;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Helper;
@ -11,15 +13,18 @@ using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infra.EFCore;
using IRaCIS.Core.Infrastructure.Extention;
using MassTransit;
using MassTransit.Futures.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using ZiggyCreatures.Caching.Fusion;
@ -65,15 +70,17 @@ namespace IRaCIS.Api.Controllers
BasicInfoView = await _doctorService.GetBasicInfo(inDto.doctorId),
EmploymentView = await _doctorService.GetEmploymentInfo(inDto.doctorId),
AttachmentList = await attachmentService.GetAttachments(inDto.doctorId),
SummarizeInfo = await _doctorService.GetSummarizeInfo(new GetSummarizeInfoInDto() {
DoctorId = inDto.doctorId,
TrialId=inDto.TrialId
SummarizeInfo = await _doctorService.GetSummarizeInfo(new GetSummarizeInfoInDto()
{
DoctorId = inDto.doctorId,
TrialId = inDto.TrialId
}),
PaymentModeInfo = await _doctorService.GetPaymentMode(inDto.doctorId),
EducationList = education.EducationList,
PostgraduateList = education.PostgraduateList,
TrialExperienceView = await _trialExperienceService.GetTrialExperience(new TrialExperienceModelIndto() {
TrialExperienceView = await _trialExperienceService.GetTrialExperience(new TrialExperienceModelIndto()
{
DoctorId = inDto.doctorId,
TrialId = inDto.TrialId
}),
@ -426,33 +433,72 @@ namespace IRaCIS.Api.Controllers
#endregion
#region 客户端凭证方式获取尝试
var client = new RestClient();
var request = new RestRequest("https://logto.test.extimaging.com//oidc/token", Method.Post)
#region 客户端方式获取logto 里面的信息
var baseUrl = "https://logto.test.extimaging.com";
var appId = "v2mr2ndxwkxz0xpsuc1th";
var appSecret = "yq9jUxl70QoOmwHxJ37h1rDoyJ5iz92Q";
var apiAddress = "https://default.logto.app/api"; //这里是个坑
var scope = "all";
var opts = new RestClientOptions(baseUrl);
using var client = new RestClient(opts);
var request = new RestRequest("oidc/token", Method.Post);
request
.AddHeader("Content-Type", "application/x-www-form-urlencoded")
.AddParameter("grant_type", "client_credentials")
.AddParameter("client_id", appId)
.AddParameter("client_secret", appSecret)
.AddParameter("resource", apiAddress); //必须在界面上配置
if (scope is not null)
{
RequestFormat = DataFormat.Json
};
request.AddParameter("scope", scope);
}
////request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
////request.AddParameter("grant_type", "client_credentials");
////request.AddParameter("client_id", clientId);
////request.AddParameter("client_secret", clientSecret);
////request.AddParameter("resource", resource);
////request.AddParameter("scope", scope);
var response = await client.ExecuteAsync<LogtoTokenResponse>(request);
if (response.StatusCode == HttpStatusCode.OK)
{
var tokenResponse = response.Data;
Console.WriteLine(tokenResponse.ToJsonStr());
#region 获取应用信息
var applicationRequest = new RestRequest($"/api/applications", Method.Get)
.AddHeader("Authorization", $"Bearer {tokenResponse.AccessToken}");
var applicationResponse = await client.ExecuteAsync(applicationRequest);
#endregion
#region 获取用户信息
//curl \
// -X GET https://[tenant_id].logto.app/api/users/{userId} \
// -H "Authorization: Bearer $ACCESS_TOKEN"
var userId = "4fqx4cb3438k";
var userInfoRequest = new RestRequest($"api/users/{userId}", Method.Get)
.AddHeader("Authorization", $"Bearer {tokenResponse.AccessToken}");
var userResponse = await client.ExecuteAsync<LogtoUser>(userInfoRequest);
Console.WriteLine(userResponse.Content);
#endregion
}
////var response = await client.ExecuteAsync<AuthenticationResponse>(request);
//if (!response.IsSuccessful || response.Data == null)
//{
// throw new InvalidOperationException("Authentication failed.");
//}
#endregion
return ResponseOutput.Ok();
}
#endregion

View File

@ -34,3 +34,4 @@ public class LogtoTokenResponse
[JsonPropertyName("id_token")]
public string? IdToken { get; set; } = null;
}

View File

@ -0,0 +1,21 @@
using Org.BouncyCastle.Tls;
using System.Collections.Generic;
namespace IRaCIS.Core.API.OAuth;
public class LogtoUser
{
public string Id { get; set; }
public string Username { get; set; }
public string PrimaryEmail { get; set; }
public string PrimaryPhone { get; set; }
public string Name { get; set; }
public string Avatar { get; set; }
public Dictionary<string, object> CustomData { get; set; } // Assuming customData can be any object
public Dictionary<string, object> Identities { get; set; }
public Dictionary<string, object> Profile { get; set; }
public string ApplicationId { get; set; }
public bool IsSuspended { get; set; }
public bool HasPassword { get; set; }
}