mirror of
https://github.com/yawaflua/yaflay.ru.git
synced 2025-12-09 20:19:32 +02:00
94 lines
4.4 KiB
Plaintext
94 lines
4.4 KiB
Plaintext
@page "{code}"
|
|
@model yawaflua.ru.Pages.AuthorizeModel
|
|
@using System.Text.Json.Nodes
|
|
@using Newtonsoft.Json
|
|
@{
|
|
string path = $"{Request.Method}//{Request.Host}";
|
|
ViewData["Title"] = "Authorize";
|
|
string authorizationUrl = $"https://discord.com/api/oauth2/authorize?client_id={Startup.clientId}&response_type=code&redirect_uri={Startup.redirectUrl}&scope=identify";
|
|
<p style="display:none;">Data: @Startup.clientId @Startup.redirectUrl [@String.Join(",", Startup.ownerId)] @Model.code</p>
|
|
if (Model.code == null)
|
|
{
|
|
if (Request.Cookies["melon"]?.ToString() == null)
|
|
{
|
|
<a href="@authorizationUrl">Login while Discord</a>
|
|
}
|
|
else
|
|
{
|
|
HttpResponseMessage message;
|
|
using (var requestMessage =
|
|
new HttpRequestMessage(HttpMethod.Get, "https://discordapp.com/api/oauth2/@me"))
|
|
{
|
|
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Request.Cookies["melon"]); ;
|
|
message = await Startup.client.SendAsync(requestMessage);
|
|
}
|
|
string responseBody = await message.Content.ReadAsStringAsync();
|
|
JsonNode response = JsonNode.Parse(responseBody);
|
|
if (response["user"] != null)
|
|
{
|
|
<h4>Вы авторизованы!</h4>
|
|
<a href="/AdminPanel"> Админка </a>
|
|
|
|
}
|
|
else
|
|
{
|
|
<h4>
|
|
Токен авторизации неправильный! Попробуйте <a href="@authorizationUrl">заново</a>
|
|
</h4>
|
|
}
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
HttpResponseMessage message;
|
|
using (var requestMessage =
|
|
new HttpRequestMessage(HttpMethod.Post, "https://discordapp.com/api/oauth2/token"))
|
|
{
|
|
requestMessage.Content = new StringContent(
|
|
@$"grant_type=authorization_code&code={Model.code}&client_id={Startup.clientId}&client_secret={Startup.clientSecret}&scope=identify&redirect_uri={Startup.redirectUrl}",
|
|
new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded")
|
|
);
|
|
message = await Startup.client.SendAsync(requestMessage);
|
|
}
|
|
string responseBody = await message.Content.ReadAsStringAsync();
|
|
JsonNode body = JsonNode.Parse(responseBody);
|
|
if (body["access_token"]?.ToString() == null)
|
|
{
|
|
<h4>Ошибка! Попробуй авторизоваться заново</h4>
|
|
|
|
Console.Error.WriteLine("debug: START \\/ \ninfo: Don't worry, this message is not bad as you think");
|
|
Console.Error.WriteLine("error: DiscordAuthorize is not worked");
|
|
Console.Error.WriteLine($"debug: Body from discord: {body}\ndebug: Sended data to discord: {message.Content.ReadAsStringAsync().Result}");
|
|
Console.Error.WriteLine($"info: Check environment data: \n ClientId={Startup.clientId}\n ClientSecret={Startup.clientSecret}\n RedirectUrl={Startup.redirectUrl}\n OwnerId={String.Join(",", Startup.ownerId)} ");
|
|
Console.Error.WriteLine("info: If any data is null and you set data in environment or appsettings.json, please create issue with this debug messages ");
|
|
Console.Error.WriteLine("debug: END /\\");
|
|
}
|
|
else
|
|
{
|
|
Response.Cookies.Append("melon", body["access_token"].ToString());
|
|
Response.Cookies.Append("watermelon", body["refresh_token"].ToString());
|
|
try
|
|
{
|
|
HttpContent bodytoApi;
|
|
bodytoApi = new StringContent(JsonConvert.SerializeObject(new yawaflua.ru.Controllers.ApiController.authorizeBody()
|
|
{
|
|
discordId = body["user"]["id"].ToString(),
|
|
melon = body["access_token"].ToString(),
|
|
type = Auth.ApiKeyTypes.Public,
|
|
watermelon = body["watermelon"].ToString()
|
|
}));
|
|
var req = await Startup.client.PostAsync(path + "/api/authorize", bodytoApi);
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Console.WriteLine(body);
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
Response.Redirect("/authorize", true);
|
|
}
|
|
}
|
|
}
|