添加了授权

This commit is contained in:
CortexCore
2023-09-21 03:49:27 +08:00
parent c59d513cca
commit c65fe87a6c
10 changed files with 317 additions and 20 deletions

View File

@@ -0,0 +1,58 @@
[gd_scene load_steps=7 format=3 uid="uid://c44tc60r5df3j"]
[ext_resource type="Script" path="res://BITKit/Scripts/Components/OpenUrl.cs" id="1_pw177"]
[ext_resource type="Script" path="res://BITKit/Scripts/ECS/Core/Entity.cs" id="2_j02yv"]
[ext_resource type="Script" path="res://BITKit/Scripts/Net/HttpListener_GodotBased.cs" id="3_kek8q"]
[ext_resource type="Script" path="res://Artists/Scripts/License/LicenseService.cs" id="4_lycvf"]
[ext_resource type="Script" path="res://Mods/工业数据采集与分析应用分享/Scripts/IDIS_License.cs" id="5_ic0bm"]
[ext_resource type="PackedScene" uid="uid://bu5w3n4me3xj2" path="res://Mods/教育平台/教育平台主菜单.tscn" id="6_6ccua"]
[node name="InitialLicense" type="Node"]
[node name="ColorRect" type="ColorRect" parent="."]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0.0117647, 0.0117647, 0.0117647, 1)
[node name="CenterContainer" type="CenterContainer" parent="ColorRect"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="VBoxContainer" type="VBoxContainer" parent="ColorRect/CenterContainer"]
layout_mode = 2
[node name="Label" type="Label" parent="ColorRect/CenterContainer/VBoxContainer"]
layout_mode = 2
text = "等待激活许可证中"
[node name="Button" type="Button" parent="ColorRect/CenterContainer/VBoxContainer"]
layout_mode = 2
text = "点击开始加载许可证"
script = ExtResource("1_pw177")
url = "http://server.bitfall.icu:5252/oauth"
[node name="授权服务" type="Node" parent="."]
script = ExtResource("2_j02yv")
[node name="HttpListener" type="Node" parent="授权服务"]
script = ExtResource("3_kek8q")
autoStart = true
port = 5348
[node name="License" type="Node" parent="授权服务"]
script = ExtResource("4_lycvf")
_requestAuthorizeUrl = "http://server.bitfall.icu:5252/api/authorize"
[node name="IDIS License" type="Node" parent="授权服务" node_paths=PackedStringArray("licenseNode")]
script = ExtResource("5_ic0bm")
initialScene = ExtResource("6_6ccua")
licenseNode = NodePath("../../ColorRect")
[connection signal="pressed" from="ColorRect/CenterContainer/VBoxContainer/Button" to="ColorRect/CenterContainer/VBoxContainer/Button" method="Execute"]

View File

@@ -0,0 +1,126 @@
using Godot;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Web;
using BITKit;
using BITKit.Auth;
using BITKit.Net.Http;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Environment = System.Environment;
using HttpClient = System.Net.Http.HttpClient;
namespace IDIS.License;
public class LicenseBin
{
public DateTime ExpirationDate=DateTime.Now;
public string Token;
}
public partial class LicenseService : EntityComponent,IAuthService
{
private static readonly HttpClient _httpClient = new();
[Export] private string _requestAuthorizeUrl;
[Export] private string _revokeAuthorizeUrl;
public bool IsAuthorized { get;private set; }
public bool IsAuthorizing { get;private set; }
public event Action<string> OnAuthorize;
public event Action<string> OnAuthorized;
public event Action<string> UnAuthorize;
public event Action<string> OnAuthorizeFailure;
private CancellationTokenSource _cancellationTokenSource;
private CancellationToken _cancellationToken=>_cancellationTokenSource.Token;
private IHttpListenerService _httpListenerService;
private readonly string path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"iFactory",
"License.bin"
);
public override void BuildService(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<IAuthService>(this);
}
public override void _EnterTree()
{
_cancellationTokenSource = new CancellationTokenSource();
}
public override void _ExitTree()
{
_cancellationTokenSource.Cancel();
}
public override void OnAwake()
{
_httpListenerService = Entity.ServiceProvider.GetRequiredService<IHttpListenerService>();
_httpListenerService.OnRequest += OnRequest;
}
public override void OnStart()
{
BIT4Log.Log<LicenseService>("准备文件中,二进制授权文件路径:"+path);
if (IsAuthorized || IsAuthorizing) return;
if (File.Exists(path) is false) return;
var json = File.ReadAllText(path);
var licenseBin = JsonConvert.DeserializeObject<LicenseBin>(json);
if(licenseBin.ExpirationDate<DateTime.Now) return;
AuthorizeAsync(licenseBin.Token).Forget();
}
private HttpContent OnRequest(HttpListenerRequest arg)
{
var token = arg.RawUrl!.Split('/').Last();
AuthorizeAsync(token).Forget();
return new StringContent("success");
}
public async UniTask AuthorizeAsync(string token)
{
var rawToken = token;
OnAuthorize?.Invoke(token);
token = JsonConvert.SerializeObject(token);
var response = await _httpClient.PostAsync(_requestAuthorizeUrl,new StringContent( token,Encoding.UTF8, "application/json"), _cancellationToken);
var message = await response.Content.ReadAsStringAsync(_cancellationToken);
if (response.IsSuccessStatusCode)
{
OnAuthorized?.Invoke(message);
BIT4Log.Log<IAuthService>($"授权成功:{message}");
var licenseBin = new LicenseBin
{
Token = rawToken,
ExpirationDate = DateTime.Now.AddDays(7)
};
PathHelper.EnsureDirectoryCreated(path);
await File.WriteAllTextAsync(path, JsonConvert.SerializeObject(licenseBin), _cancellationToken);
IsAuthorized = true;
}
else
{
OnAuthorizeFailure?.Invoke(message);
BIT4Log.Log<IAuthService>($"当前Token:{rawToken}");
BIT4Log.Log<IAuthService>($"授权失败:{message}");
}
}
public UniTask CancelAuthorizationAsync()
{
if (IsAuthorized is false)
{
throw new InvalidOperationException("未授权");
}
var url = _revokeAuthorizeUrl.Replace("{x}",_requestAuthorizeUrl);
UnAuthorize?.Invoke("取消授权");
IsAuthorized = false;
_httpClient.PostAsync(url, new StringContent(""), _cancellationToken).AsUniTask().Forget();
return UniTask.CompletedTask;
}
}

View File

@@ -0,0 +1,16 @@
[gd_scene load_steps=4 format=3 uid="uid://c3mhpoxbs142w"]
[ext_resource type="Script" path="res://BITKit/Scripts/ECS/Core/Entity.cs" id="1_52733"]
[ext_resource type="Script" path="res://BITKit/Scripts/Net/HttpListener_GodotBased.cs" id="2_8gsq5"]
[ext_resource type="Script" path="res://Artists/Scripts/License/LicenseService.cs" id="3_pvdt4"]
[node name="LicenseService" type="Node"]
script = ExtResource("1_52733")
[node name="HttpListener" type="Node" parent="."]
script = ExtResource("2_8gsq5")
autoStart = true
port = 7001
[node name="LicenseService" type="Node" parent="."]
script = ExtResource("3_pvdt4")

File diff suppressed because one or more lines are too long