mirror of
https://github.com/yawaflua/Discord.Net.git
synced 2025-12-11 15:56:21 +02:00
Refactor and rename project to yawaflua.Discord.Net; add core entities and interfaces for Discord OAuth2 integration
This commit is contained in:
@@ -1,16 +1,69 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using yawaflua.Discord.Net.Interfaces.Models;
|
||||
|
||||
namespace x3rt.DiscordOAuth2.Entities;
|
||||
namespace yawaflua.Discord.Net.Entities;
|
||||
|
||||
public class OAuthToken
|
||||
internal class OAuthToken (HttpClient client, ulong ClientId, string ClientSecret) : IToken
|
||||
{
|
||||
[JsonProperty("access_token")] public string AccessToken { get; set; }
|
||||
[JsonPropertyName("access_token")] public string AccessToken { get; set; }
|
||||
|
||||
[JsonProperty("expires_in")] public int ExpiresIn { get; set; }
|
||||
[JsonPropertyName("expires_in")] public int ExpiresIn { get; set; }
|
||||
|
||||
[JsonProperty("refresh_token")] public string RefreshToken { get; set; }
|
||||
[JsonPropertyName("refresh_token")] public string RefreshToken { get; set; }
|
||||
|
||||
[JsonProperty("scope")] public string Scope { get; set; }
|
||||
[JsonPropertyName("scope")] public string Scope { get; set; }
|
||||
|
||||
[JsonProperty("token_type")] public string TokenType { get; set; }
|
||||
[JsonPropertyName("token_type")] public string TokenType { get; set; }
|
||||
public Task RevokeAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, "https://discord.com/api/oauth2/token/revoke")
|
||||
{
|
||||
Content = new FormUrlEncodedContent(new Dictionary<string, string>
|
||||
{
|
||||
{ "token", AccessToken },
|
||||
{ "client_id", ClientId.ToString() },
|
||||
{ "client_secret", ClientSecret }
|
||||
})
|
||||
};
|
||||
return client.SendAsync(request, cancellationToken)
|
||||
.ContinueWith(task =>
|
||||
{
|
||||
if (!task.Result.IsSuccessStatusCode)
|
||||
{
|
||||
throw new Exception("Failed to revoke token.");
|
||||
}
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task RefreshAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, "https://discord.com/api/oauth2/token")
|
||||
{
|
||||
Content = new FormUrlEncodedContent(new Dictionary<string, string>
|
||||
{
|
||||
{ "grant_type", "refresh_token" },
|
||||
{ "refresh_token", RefreshToken },
|
||||
})
|
||||
};
|
||||
var byteArray = System.Text.Encoding.ASCII.GetBytes($"{ClientId}:{ClientSecret}");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
|
||||
var req = await client.SendAsync(request, cancellationToken);
|
||||
if (!req.IsSuccessStatusCode)
|
||||
{
|
||||
throw new Exception("Failed to refresh token.");
|
||||
}
|
||||
var responseString = await req.Content.ReadAsStringAsync();
|
||||
var newToken = JsonSerializer.Deserialize<OAuthToken>(responseString);
|
||||
if (newToken is null)
|
||||
{
|
||||
throw new Exception("Failed to deserialize token.");
|
||||
}
|
||||
AccessToken = newToken.AccessToken;
|
||||
ExpiresIn = newToken.ExpiresIn;
|
||||
RefreshToken = newToken.RefreshToken;
|
||||
Scope = newToken.Scope;
|
||||
TokenType = newToken.TokenType;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user