|
|
|
@ -1,6 +1,8 @@ |
|
|
|
using Common.Shared.Application.DaHua; |
|
|
|
using Common.Shared.Application.DaHua; |
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
|
|
|
using MongoDB.Bson; |
|
|
|
|
|
|
|
using OfficeOpenXml.FormulaParsing.LexicalAnalysis; |
|
|
|
using Org.BouncyCastle.Crypto.Parameters; |
|
|
|
using Org.BouncyCastle.Crypto.Parameters; |
|
|
|
using Org.BouncyCastle.Security; |
|
|
|
using Org.BouncyCastle.Security; |
|
|
|
using System.Net.Http.Json; |
|
|
|
using System.Net.Http.Json; |
|
|
|
@ -34,12 +36,18 @@ namespace Common.Shared.DomainService |
|
|
|
public async Task<string> GetTokenAsync(string clientId) |
|
|
|
public async Task<string> GetTokenAsync(string clientId) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (TokenCache.TokenMap.TryGetValue(clientId, out var tokenEntry) |
|
|
|
if (TokenCache.TokenMap.TryGetValue(clientId, out var tokenEntry) |
|
|
|
&& tokenEntry.ExpireAt > DateTimeOffset.UtcNow.AddMinutes(5)) |
|
|
|
&& tokenEntry.ExpireAt > DateTimeOffset.UtcNow.AddMinutes(1)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return tokenEntry.AccessToken!; |
|
|
|
if (!IsTokenValid(tokenEntry.AccessToken!)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
_logger.LogWarning("从字典里匹配"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return tokenEntry.AccessToken!; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var tokenLock = TokenLockProvider.GetLock(clientId); |
|
|
|
var tokenLock = TokenLockProvider.GetLock(clientId); |
|
|
|
|
|
|
|
_logger.LogWarning("枷锁"); |
|
|
|
await tokenLock.WaitAsync(); |
|
|
|
await tokenLock.WaitAsync(); |
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -47,7 +55,12 @@ namespace Common.Shared.DomainService |
|
|
|
if (TokenCache.TokenMap.TryGetValue(clientId, out tokenEntry) |
|
|
|
if (TokenCache.TokenMap.TryGetValue(clientId, out tokenEntry) |
|
|
|
&& tokenEntry.ExpireAt > DateTimeOffset.UtcNow.AddMinutes(5)) |
|
|
|
&& tokenEntry.ExpireAt > DateTimeOffset.UtcNow.AddMinutes(5)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return tokenEntry.AccessToken!; |
|
|
|
if (!IsTokenValid(tokenEntry.AccessToken!)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
_logger.LogWarning("再次匹配"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return tokenEntry.AccessToken!; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var refreshed = await TryRefreshOrLoginAsync(clientId, tokenEntry); |
|
|
|
var refreshed = await TryRefreshOrLoginAsync(clientId, tokenEntry); |
|
|
|
@ -137,7 +150,8 @@ namespace Common.Shared.DomainService |
|
|
|
dto.ClientSecret = _configuration["DahuaAuth:ClientSecret"]!; |
|
|
|
dto.ClientSecret = _configuration["DahuaAuth:ClientSecret"]!; |
|
|
|
dto.Password = _configuration["DahuaAuth:Password"]!; |
|
|
|
dto.Password = _configuration["DahuaAuth:Password"]!; |
|
|
|
dto.Username = _configuration["DahuaAuth:Username"]!; |
|
|
|
dto.Username = _configuration["DahuaAuth:Username"]!; |
|
|
|
|
|
|
|
_logger.LogWarning("在GetDaHToken方法中的记录:Dahua Host = {Host}, ClientId = {ClientId}", |
|
|
|
|
|
|
|
_configuration["DahuaAuth:Host"], _configuration["DahuaAuth:ClientId"], _configuration["DahuaAuth:ClientSecret"]); |
|
|
|
DaHApiResult<LoginResDto> loginResult = await GetToken(dto); |
|
|
|
DaHApiResult<LoginResDto> loginResult = await GetToken(dto); |
|
|
|
|
|
|
|
|
|
|
|
TokenEntry refreshed = new() |
|
|
|
TokenEntry refreshed = new() |
|
|
|
@ -207,10 +221,11 @@ namespace Common.Shared.DomainService |
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
{ |
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/evo-oauth/1.0.0/oauth/public-key"; |
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/evo-oauth/1.0.0/oauth/public-key"; |
|
|
|
|
|
|
|
_logger.LogWarning($"获取的url{url}"); |
|
|
|
using var resp = await _http.GetAsync(url); |
|
|
|
using var resp = await _http.GetAsync(url); |
|
|
|
resp.EnsureSuccessStatusCode(); |
|
|
|
resp.EnsureSuccessStatusCode(); |
|
|
|
var json = await resp.Content.ReadAsStringAsync(); |
|
|
|
var json = await resp.Content.ReadAsStringAsync(); |
|
|
|
|
|
|
|
_logger.LogWarning($"获取密钥的结果json{json}"); |
|
|
|
var envelope = JsonSerializer.Deserialize<DaHApiResult<PublicKeyDto>>(json, new JsonSerializerOptions |
|
|
|
var envelope = JsonSerializer.Deserialize<DaHApiResult<PublicKeyDto>>(json, new JsonSerializerOptions |
|
|
|
{ |
|
|
|
{ |
|
|
|
PropertyNameCaseInsensitive = true |
|
|
|
PropertyNameCaseInsensitive = true |
|
|
|
@ -230,7 +245,7 @@ namespace Common.Shared.DomainService |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
{ |
|
|
|
_logger.LogWarning(ex, "大华平台获取公钥出错"); |
|
|
|
_logger.LogWarning(ex, $"大华平台获取公钥出错{ex.Message}"); |
|
|
|
result.Success = false; |
|
|
|
result.Success = false; |
|
|
|
result.Code = "1001"; |
|
|
|
result.Code = "1001"; |
|
|
|
result.Msg = "获取大华公钥失败"; |
|
|
|
result.Msg = "获取大华公钥失败"; |
|
|
|
@ -267,33 +282,44 @@ namespace Common.Shared.DomainService |
|
|
|
{ |
|
|
|
{ |
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/evo-oauth/1.0.0/oauth/extend/token"; |
|
|
|
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/evo-oauth/1.0.0/oauth/extend/token"; |
|
|
|
//必须加密 |
|
|
|
//必须加密 |
|
|
|
|
|
|
|
_logger.LogWarning($"获取token的url{url}"); |
|
|
|
|
|
|
|
_logger.LogWarning($"获取token的dto的json{dto.ToJson()}"); |
|
|
|
dto.Password = EncryptByPublicKey(dto.Password, dto.PublicKey!); |
|
|
|
dto.Password = EncryptByPublicKey(dto.Password, dto.PublicKey!); |
|
|
|
using var resp = await _http.PostAsJsonAsync(url, dto); |
|
|
|
using var resp = await _http.PostAsJsonAsync(url, dto); |
|
|
|
resp.EnsureSuccessStatusCode(); |
|
|
|
resp.EnsureSuccessStatusCode(); |
|
|
|
|
|
|
|
_logger.LogWarning("这是正式请求的:Dahua Host = {Host}, ClientId = {ClientId},ClientSecret={ClientSecret}", _configuration["DahuaAuth:Host"], _configuration["DahuaAuth:ClientId"], _configuration["DahuaAuth:ClientSecret"]); |
|
|
|
var tokenInfo = await resp.Content.ReadFromJsonAsync<DaHApiResult<LoginResDto>>(); |
|
|
|
var tokenInfo = await resp.Content.ReadFromJsonAsync<DaHApiResult<LoginResDto>>(); |
|
|
|
|
|
|
|
_logger.LogWarning($"获取大华登录令牌返回结果:{tokenInfo.ToJson()}"); |
|
|
|
if (tokenInfo == null || !result.Success || result.Code != "0") |
|
|
|
if (tokenInfo == null || !result.Success || result.Code != "0") |
|
|
|
{ |
|
|
|
{ |
|
|
|
result.Success = false; |
|
|
|
result.Success = false; |
|
|
|
result.Code = "1004"; |
|
|
|
result.Code = "1004"; |
|
|
|
result.Msg = "获取大华登录令牌失败"; |
|
|
|
result.Msg = "获取大华登录令牌失败"; |
|
|
|
_logger.LogWarning("获取大华登录令牌失败,返回结果:{Result}", result); |
|
|
|
_logger.LogWarning($"获取大华登录令牌失败,返回结果:{result}"); |
|
|
|
} |
|
|
|
} |
|
|
|
result = tokenInfo!; |
|
|
|
result = tokenInfo!; |
|
|
|
//固定的拼接方式 |
|
|
|
//固定的拼接方式 |
|
|
|
result.Data!.AccessToken = string.Concat(tokenInfo?.Data!.TokenType, " ", tokenInfo?.Data!.AccessToken); |
|
|
|
if (tokenInfo?.Data!.AccessToken != null && tokenInfo?.Data!.AccessToken.Length > 1) |
|
|
|
|
|
|
|
|
|
|
|
TokenEntry refreshed = new TokenEntry |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
AccessToken = string.Concat(result!.Data.TokenType, " ", result.Data.AccessToken), |
|
|
|
result.Data!.AccessToken = string.Concat(tokenInfo?.Data!.TokenType, " ", tokenInfo?.Data!.AccessToken); |
|
|
|
|
|
|
|
TokenEntry refreshed = new TokenEntry |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
AccessToken = string.Concat(result!.Data.TokenType, " ", result.Data.AccessToken), |
|
|
|
|
|
|
|
|
|
|
|
ExpireAt = DateTimeOffset.UtcNow.AddSeconds(result.Data.ExpiresIn) |
|
|
|
ExpireAt = DateTimeOffset.UtcNow.AddSeconds(result.Data.ExpiresIn) |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
result.Success = false; |
|
|
|
|
|
|
|
result.Code = "1006"; |
|
|
|
|
|
|
|
result.Msg = "没拿到token"; |
|
|
|
|
|
|
|
_logger.LogWarning($"获取大华登录令牌失败,返回结果:{result}"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "获取大华登录令牌出错"); |
|
|
|
_logger.LogError(ex, $"获取大华登录令牌出错{ex.Message}"); |
|
|
|
result.Success = false; |
|
|
|
result.Success = false; |
|
|
|
result.Code = "1004"; |
|
|
|
result.Code = "1004"; |
|
|
|
result.Msg = "获取大华登录令牌失败"; |
|
|
|
result.Msg = "获取大华登录令牌失败"; |
|
|
|
|