🔊 Add more logs on afdian oidc

This commit is contained in:
LittleSheep 2025-06-23 00:57:10 +08:00
parent be0b48cfd9
commit 694bc77921

View File

@ -9,7 +9,8 @@ public class AfdianOidcService(
IHttpClientFactory httpClientFactory,
AppDatabase db,
AuthService auth,
ICacheService cache
ICacheService cache,
ILogger<AfdianOidcService> logger
)
: OidcService(configuration, httpClientFactory, db, auth, cache)
{
@ -88,6 +89,8 @@ public class AfdianOidcService(
}
private async Task<OidcUserInfo> GetUserInfoAsync(string accessToken)
{
try
{
var config = GetProviderConfig();
var content = new FormUrlEncodedContent(new Dictionary<string, string>
@ -107,10 +110,11 @@ public class AfdianOidcService(
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
logger.LogInformation("Trying get userinfo from afdian, response: {Response}", json);
var afdianResponse = JsonDocument.Parse(json).RootElement;
var user = afdianResponse.GetProperty("data");
var userId = user.GetProperty("user_id").GetString() ?? "";
var user = afdianResponse.TryGetProperty("data", out var dataElement) ? dataElement : default;
var userId = user.TryGetProperty("user_id", out var userIdElement) ? userIdElement.GetString() ?? "" : "";
var avatar = user.TryGetProperty("avatar", out var avatarElement) ? avatarElement.GetString() : null;
return new OidcUserInfo
@ -123,4 +127,11 @@ public class AfdianOidcService(
Provider = ProviderName
};
}
catch (Exception ex)
{
// Due to afidan's API isn't compliant with OAuth2, we want more logs from it to investigate.
logger.LogError(ex, "Failed to get user info from Afdian");
throw;
}
}
}