🔊 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, IHttpClientFactory httpClientFactory,
AppDatabase db, AppDatabase db,
AuthService auth, AuthService auth,
ICacheService cache ICacheService cache,
ILogger<AfdianOidcService> logger
) )
: OidcService(configuration, httpClientFactory, db, auth, cache) : OidcService(configuration, httpClientFactory, db, auth, cache)
{ {
@ -89,38 +90,48 @@ public class AfdianOidcService(
private async Task<OidcUserInfo> GetUserInfoAsync(string accessToken) private async Task<OidcUserInfo> GetUserInfoAsync(string accessToken)
{ {
var config = GetProviderConfig(); try
var content = new FormUrlEncodedContent(new Dictionary<string, string>
{ {
{ "client_id", config.ClientId }, var config = GetProviderConfig();
{ "client_secret", config.ClientSecret }, var content = new FormUrlEncodedContent(new Dictionary<string, string>
{ "grant_type", "authorization_code" }, {
{ "code", accessToken }, { "client_id", config.ClientId },
{ "redirect_uri", config.RedirectUri }, { "client_secret", config.ClientSecret },
}); { "grant_type", "authorization_code" },
{ "code", accessToken },
{ "redirect_uri", config.RedirectUri },
});
var client = HttpClientFactory.CreateClient(); var client = HttpClientFactory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://afdian.com/api/oauth2/access_token"); var request = new HttpRequestMessage(HttpMethod.Post, "https://afdian.com/api/oauth2/access_token");
request.Content = content; request.Content = content;
var response = await client.SendAsync(request); var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
var afdianResponse = JsonDocument.Parse(json).RootElement; logger.LogInformation("Trying get userinfo from afdian, response: {Response}", json);
var afdianResponse = JsonDocument.Parse(json).RootElement;
var user = afdianResponse.GetProperty("data"); var user = afdianResponse.TryGetProperty("data", out var dataElement) ? dataElement : default;
var userId = user.GetProperty("user_id").GetString() ?? ""; var userId = user.TryGetProperty("user_id", out var userIdElement) ? userIdElement.GetString() ?? "" : "";
var avatar = user.TryGetProperty("avatar", out var avatarElement) ? avatarElement.GetString() : null; var avatar = user.TryGetProperty("avatar", out var avatarElement) ? avatarElement.GetString() : null;
return new OidcUserInfo return new OidcUserInfo
{
UserId = userId,
DisplayName = (user.TryGetProperty("name", out var nameElement)
? nameElement.GetString()
: null) ?? "",
ProfilePictureUrl = avatar,
Provider = ProviderName
};
}
catch (Exception ex)
{ {
UserId = userId, // Due to afidan's API isn't compliant with OAuth2, we want more logs from it to investigate.
DisplayName = (user.TryGetProperty("name", out var nameElement) logger.LogError(ex, "Failed to get user info from Afdian");
? nameElement.GetString() throw;
: null) ?? "", }
ProfilePictureUrl = avatar,
Provider = ProviderName
};
} }
} }