using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using SimApi.Communications;
using static SimApi.Helpers.SimApiError;
namespace SimApi.AuthSDK;
public class SimApiAuthCenter(SimApiAuthClient simapi)
{
public SimApiAuthClient Client { get; } = simapi;
#region 公共
///
/// 委托AuthCenter进行应用签名验证
///
///
///
///
///
public void VerifySign(string appId, string timestamp, string nonce, string sign)
{
var http = new HttpClient();
var url = $"{Client.Server}/api/auth/sign/verify?appId={appId}×tamp={timestamp}&nonce={nonce}&sign={sign}";
var result = http.PostAsJsonAsync(url, new { }).Result;
var resp = result.Content.ReadFromJsonAsync().Result;
ErrorWhenNull(resp, 400, "签名验证失败");
ErrorWhenFalse(resp.Code == 200, 400, "签名验证失败");
}
#endregion
#region 群组相关
///
/// 根据profileId获取群组列表
///
///
///
public SimApiAuthCenterDto.GroupRelatedItem[]? GroupRelated(string profileId)
{
return Client.SignQuery("/api/auth/group/related", new { profileId });
}
///
/// 根据关键字搜索群组,输入群组ID精准搜索
///
///
///
///
///
public SimApiAuthCenterDto.AppAndProfileItem[]? GroupSearch(string keyword, int skip = 0, int take = 20)
{
return Client.SignQuery("/api/auth/group/search",
new { keyword, skip, take });
}
///
/// 使用组ID以及组内成员,管理员profile获取组的详细树结构
///
///
///
///
public SimApiAuthCenterDto.GroupDetailTreeNode? GroupDetail(string groupId, string profileId)
{
return Client.SignQuery("/api/auth/group/detail",
new { profileId, groupId });
}
///
/// 获取profile在本组的所有子组
///
///
///
///
public string[]? GroupRelatedIndex(string groupId, string profileId)
{
return Client.SignQuery("/api/auth/internal/group/related-group-ids", new { groupId, profileId });
}
#endregion
#region Profile相关
///
/// 根据关键字,搜索用户Profile,参数支持精准ID,用户手机号,用户邮箱,Profile名称模糊搜索
///
///
///
///
///
public SimApiAuthCenterDto.AppAndProfileItem[]? ProfileSearch(string keyword, int skip = 0, int take = 20)
{
return Client.SignQuery("/api/auth/profile/search",
new { keyword, skip, take });
}
///
/// 通过id,可以批量获取用户的基本信息
///
///
///
public SimApiAuthCenterDto.AppAndProfileItem[]? ProfileList(string[] ids)
{
return Client.SignQuery("/api/auth/profile/list", new { ids });
}
#endregion
#region AuthGate内部应用专用 - 注意: 只有内部应用可以调用
///
/// 获取是否为App的拥有者
///
///
///
///
public bool CheckIsAppOwner(string profileId, string applicationId)
{
return Client.SignQuery("/api/auth/internal/app/check-owner", new
{
ProfileId = profileId,
AppId = applicationId,
});
}
///
/// 获取应用列表,根据用户profileId 和 提供的appIds
///
///
///
///
public SimApiAuthCenterDto.AppAndProfileItem[]? GetAppList(string profileId, IEnumerable appIds)
{
return Client.SignQuery("/api/auth/internal/app/related", new
{
ProfileId = profileId,
AllowedAppIds = appIds,
});
}
#endregion
#region 系统登录
///
/// 获取登录授权CODE
///
///
///
///
///
public SimApiAuthCenterDto.GetCodeResponse GetLoginCode(string? scene = null,
Dictionary? data = null,
string? backUrl = null)
{
var code = Client.SignQuery("/api/auth/login/code",
new { scene, data, backUrl });
return new SimApiAuthCenterDto.GetCodeResponse()
{
Code = code!,
Server = Client.Server,
FullUrl = $"{Client.Server}/auth?code={code}"
};
}
///
/// 使用code获取登录信息
///
///
///
///
public SimApiAuthCenterDto.LoginInfoResponse GetLoginInfo(string code, string? scene = null)
{
var resp = Client.SignQuery("/api/auth/login/get", new { code });
ErrorWhenNull(resp, 400232, "登录信息获取失败");
ErrorWhen(resp.Scene != scene, 403003, "登录场景不匹配");
return resp;
}
#endregion
#region 安全验证
///
/// 获取安全验证代码
///
///
///
///
///
///
public SimApiAuthCenterDto.GetCodeResponse GetConfirmCode(string scene, string userId,
Dictionary? data = null,
string? backUrl = null)
{
var code = Client.SignQuery("/api/auth/confirm/code",
new { scene, data, backUrl, profileId = userId });
return new SimApiAuthCenterDto.GetCodeResponse()
{
Code = code!,
Server = Client.Server,
FullUrl = $"{Client.Server}/confirm?code={code}"
};
}
///
/// 使用安全验证code 获取验证结果
///
///
///
///
///
public SimApiAuthCenterDto.ConfirmResponse Confirm(string code, string scene, string? userId = null)
{
var resp = Client.SignQuery("/api/auth/confirm/get", new { code });
ErrorWhenNull(resp, 403001, "安全确认码无效");
ErrorWhen(resp.ProfileId != userId, 403002, "安全确认身份不匹配");
ErrorWhen(resp.Scene != scene, 403003, "安全确认场景不匹配");
return resp;
}
#endregion
}