Merge pull request #92: fix: add Discord OAuth token exchange logging for production diagnostics
This commit is contained in:
@@ -6,7 +6,7 @@ on:
|
|||||||
- main
|
- main
|
||||||
|
|
||||||
env:
|
env:
|
||||||
VERSION: 2.8.0
|
VERSION: 2.8.1
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами)
|
# ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<Project>
|
<Project>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Version>2.8.0</Version>
|
<Version>2.8.1</Version>
|
||||||
<TargetFramework>net10.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<LangVersion>preview</LangVersion>
|
<LangVersion>preview</LangVersion>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
|||||||
+1
-1
@@ -49,7 +49,7 @@ services:
|
|||||||
crond -f
|
crond -f
|
||||||
|
|
||||||
bot:
|
bot:
|
||||||
image: git.codeanddice.ru/toutsu/gmrelay-bot:2.8.0
|
image: git.codeanddice.ru/toutsu/gmrelay-bot:2.8.1
|
||||||
restart: always
|
restart: always
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
|
|||||||
@@ -66,7 +66,7 @@
|
|||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<div class="nav-version">v2.8.0</div>
|
<div class="nav-version">v2.8.1</div>
|
||||||
</div>
|
</div>
|
||||||
</Authorized>
|
</Authorized>
|
||||||
<NotAuthorized>
|
<NotAuthorized>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ using System.Text.Json.Serialization;
|
|||||||
|
|
||||||
namespace GmRelay.Web.Services;
|
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();
|
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);
|
var response = await client.PostAsync("https://discord.com/api/oauth2/token", content);
|
||||||
if (!response.IsSuccessStatusCode)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var json = await response.Content.ReadAsStringAsync();
|
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);
|
return JsonSerializer.Deserialize<DiscordTokenResponse>(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Text.Json;
|
|||||||
using GmRelay.Web;
|
using GmRelay.Web;
|
||||||
using GmRelay.Web.Services;
|
using GmRelay.Web.Services;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
|
||||||
namespace GmRelay.Bot.Tests.Web;
|
namespace GmRelay.Bot.Tests.Web;
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ public class DiscordAuthServiceTests
|
|||||||
})
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var service = new DiscordAuthService(new TestHttpClientFactory(), config);
|
var service = new DiscordAuthService(new TestHttpClientFactory(), config, NullLogger<DiscordAuthService>.Instance);
|
||||||
var url = service.BuildAuthorizeUrl("state123");
|
var url = service.BuildAuthorizeUrl("state123");
|
||||||
|
|
||||||
Assert.Contains("client_id=12345", url);
|
Assert.Contains("client_id=12345", url);
|
||||||
@@ -33,7 +34,7 @@ public class DiscordAuthServiceTests
|
|||||||
public void BuildAuthorizeUrl_WithMissingConfig_ThrowsInvalidOperationException()
|
public void BuildAuthorizeUrl_WithMissingConfig_ThrowsInvalidOperationException()
|
||||||
{
|
{
|
||||||
var config = new ConfigurationBuilder().Build();
|
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"));
|
Assert.Throws<InvalidOperationException>(() => service.BuildAuthorizeUrl("state"));
|
||||||
}
|
}
|
||||||
@@ -74,7 +75,7 @@ public class DiscordAuthServiceTests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var factory = new TestHttpClientFactory(handler);
|
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");
|
var result = await service.ExchangeCodeAsync("valid_code");
|
||||||
|
|
||||||
@@ -102,7 +103,7 @@ public class DiscordAuthServiceTests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var factory = new TestHttpClientFactory(handler);
|
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");
|
var result = await service.ExchangeCodeAsync("invalid_code");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user