fix: add Discord OAuth token exchange logging for production diagnostics
PR Checks / test-and-build (pull_request) Failing after 6m20s
PR Checks / test-and-build (pull_request) Failing after 6m20s
- Log status code and response body when Discord /oauth2/token fails - Helps identify why ExchangeCodeAsync returns null in production Bump version → 2.8.1 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -6,7 +6,7 @@ on:
|
||||
- main
|
||||
|
||||
env:
|
||||
VERSION: 2.8.0
|
||||
VERSION: 2.8.1
|
||||
|
||||
jobs:
|
||||
# ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<Version>2.8.0</Version>
|
||||
<Version>2.8.1</Version>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
+1
-1
@@ -49,7 +49,7 @@ services:
|
||||
crond -f
|
||||
|
||||
bot:
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-bot:2.8.0
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-bot:2.8.1
|
||||
restart: always
|
||||
depends_on:
|
||||
db:
|
||||
|
||||
@@ -66,7 +66,7 @@
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="nav-version">v2.8.0</div>
|
||||
<div class="nav-version">v2.8.1</div>
|
||||
</div>
|
||||
</Authorized>
|
||||
<NotAuthorized>
|
||||
|
||||
@@ -5,7 +5,7 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace GmRelay.Web.Services;
|
||||
|
||||
public sealed class DiscordAuthService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
|
||||
public sealed class DiscordAuthService(IHttpClientFactory httpClientFactory, IConfiguration configuration, ILogger<DiscordAuthService> logger)
|
||||
{
|
||||
private readonly DiscordOAuthOptions _options = configuration.GetSection("Discord").Get<DiscordOAuthOptions>() ?? new DiscordOAuthOptions();
|
||||
|
||||
@@ -40,10 +40,14 @@ public sealed class DiscordAuthService(IHttpClientFactory httpClientFactory, ICo
|
||||
});
|
||||
|
||||
var response = await client.PostAsync("https://discord.com/api/oauth2/token", content);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return null;
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
logger.LogError("Discord token exchange failed: {StatusCode} {Body}. client_id={ClientId}, redirect_uri={RedirectUri}",
|
||||
(int)response.StatusCode, json, _options.ClientId, _options.RedirectUri);
|
||||
return null;
|
||||
}
|
||||
|
||||
return JsonSerializer.Deserialize<DiscordTokenResponse>(json);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Text.Json;
|
||||
using GmRelay.Web;
|
||||
using GmRelay.Web.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Web;
|
||||
|
||||
@@ -20,7 +21,7 @@ public class DiscordAuthServiceTests
|
||||
})
|
||||
.Build();
|
||||
|
||||
var service = new DiscordAuthService(new TestHttpClientFactory(), config);
|
||||
var service = new DiscordAuthService(new TestHttpClientFactory(), config, NullLogger<DiscordAuthService>.Instance);
|
||||
var url = service.BuildAuthorizeUrl("state123");
|
||||
|
||||
Assert.Contains("client_id=12345", url);
|
||||
@@ -33,7 +34,7 @@ public class DiscordAuthServiceTests
|
||||
public void BuildAuthorizeUrl_WithMissingConfig_ThrowsInvalidOperationException()
|
||||
{
|
||||
var config = new ConfigurationBuilder().Build();
|
||||
var service = new DiscordAuthService(new TestHttpClientFactory(), config);
|
||||
var service = new DiscordAuthService(new TestHttpClientFactory(), config, NullLogger<DiscordAuthService>.Instance);
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => service.BuildAuthorizeUrl("state"));
|
||||
}
|
||||
@@ -74,7 +75,7 @@ public class DiscordAuthServiceTests
|
||||
.Build();
|
||||
|
||||
var factory = new TestHttpClientFactory(handler);
|
||||
var service = new DiscordAuthService(factory, config);
|
||||
var service = new DiscordAuthService(factory, config, NullLogger<DiscordAuthService>.Instance);
|
||||
|
||||
var result = await service.ExchangeCodeAsync("valid_code");
|
||||
|
||||
@@ -102,7 +103,7 @@ public class DiscordAuthServiceTests
|
||||
.Build();
|
||||
|
||||
var factory = new TestHttpClientFactory(handler);
|
||||
var service = new DiscordAuthService(factory, config);
|
||||
var service = new DiscordAuthService(factory, config, NullLogger<DiscordAuthService>.Instance);
|
||||
|
||||
var result = await service.ExchangeCodeAsync("invalid_code");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user