Skip to content

Commit

Permalink
Adds OAuth sample
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Jun 11, 2024
1 parent a97de95 commit 80b3772
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bld/
[Ll]ogs/

# appsettings.json
appsettings.json
appsettings.*.json

# Visual Studio 2015/2017 cache/options directory
.vs/
Expand Down
7 changes: 7 additions & 0 deletions Kook.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.Webhook.HttpListen
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.Samples.Webhook.HttpListener", "samples\Kook.Net.Samples.Webhook.HttpListener\Kook.Net.Samples.Webhook.HttpListener.csproj", "{4625F550-9A14-46E2-AD17-8D49485F46D0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kook.Net.Samples.OAuth", "samples\Kook.Net.Samples.OAuth\Kook.Net.Samples.OAuth.csproj", "{6D07EB30-874A-4BCD-870E-0C8CE77B9990}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -168,6 +170,10 @@ Global
{4625F550-9A14-46E2-AD17-8D49485F46D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4625F550-9A14-46E2-AD17-8D49485F46D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4625F550-9A14-46E2-AD17-8D49485F46D0}.Release|Any CPU.Build.0 = Release|Any CPU
{6D07EB30-874A-4BCD-870E-0C8CE77B9990}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6D07EB30-874A-4BCD-870E-0C8CE77B9990}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D07EB30-874A-4BCD-870E-0C8CE77B9990}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6D07EB30-874A-4BCD-870E-0C8CE77B9990}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{57F76D67-0E55-4609-A61C-3A4C3073CA0E} = {58B166AB-82DD-472F-AC70-9318DA4F881E}
Expand All @@ -191,5 +197,6 @@ Global
{62E149CC-9C22-41A0-B9C5-DE930FC26EC5} = {B51E68C1-B6FC-49A3-B04D-57BB0C19B192}
{5E447382-3D75-4BD0-8657-FF24900F879A} = {B51E68C1-B6FC-49A3-B04D-57BB0C19B192}
{4625F550-9A14-46E2-AD17-8D49485F46D0} = {8DE556F9-829D-48D3-A41C-FA57207CAE72}
{6D07EB30-874A-4BCD-870E-0C8CE77B9990} = {8DE556F9-829D-48D3-A41C-FA57207CAE72}
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions THIRD-PARTY-NOTICES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

License notice for AspNet.Security.OAuth.Providers
------------------------------------

Licensed under the Apache License, Version 2.0.
Available at
https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers/blob/dev/LICENSE

License notice for coverlet.collector
------------------------------------

Expand Down
14 changes: 10 additions & 4 deletions docs/demos/demos.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,28 @@ title: 示例项目

## 语音

### [Kook.Net.Samples.Audio](https://github.com/gehongyan/Kook.Net/tree/dev/samples/Kook.Net.Samples.Audio)
### [Kook.Net.Samples.Audio](https://github.com/gehongyan/Kook.Net/tree/master/samples/Kook.Net.Samples.Audio)

该示例项目演示了如何使用 Kook.Net 中的语音功能,来构建一个点歌 Bot。

## OAuth

### [Kook.Net.Samples.OAuth](https://github.com/gehongyan/Kook.Net/tree/master/samples/Kook.Net.Samples.OAuth)

该示例项目演示了如何使用 AspNet.Security.OAuth.Providers 库来添加 OAuth 认证功能,以及如何使用 Kook.Net 获取所认证用户的信息。

## 语言变体

### [Kook.Net.Samples.FSharp](https://github.com/gehongyan/Kook.Net/tree/dev/samples/Kook.Net.Samples.FSharp)
### [Kook.Net.Samples.FSharp](https://github.com/gehongyan/Kook.Net/tree/master/samples/Kook.Net.Samples.FSharp)

该示例项目演示了如何使用 F# 构建基于 Kook.Net 的 Bot 的方法。

### [Kook.Net.Samples.VisualBasic](https://github.com/gehongyan/Kook.Net/tree/dev/samples/Kook.Net.Samples.VisualBasic)
### [Kook.Net.Samples.VisualBasic](https://github.com/gehongyan/Kook.Net/tree/master/samples/Kook.Net.Samples.VisualBasic)

该示例项目演示了如何使用 Visual Basic 构建基于 Kook.Net 的 Bot 的方法。

## 部署

### [Kook.Net.Samples.Docker](https://github.com/gehongyan/Kook.Net/tree/dev/samples/Kook.Net.Samples.Docker)
### [Kook.Net.Samples.Docker](https://github.com/gehongyan/Kook.Net/tree/master/samples/Kook.Net.Samples.Docker)

该示例项目演示了如何编写 Dockerfile 来创建封装 Bot 能力的镜像。
13 changes: 13 additions & 0 deletions samples/Kook.Net.Samples.OAuth/Kook.Net.Samples.OAuth.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<Import Project="../../Kook.Net.Sample.targets" />

<ItemGroup>
<PackageReference Include="AspNet.Security.OAuth.Kook" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Kook.Net.Experimental\Kook.Net.Experimental.csproj" />
</ItemGroup>

</Project>
79 changes: 79 additions & 0 deletions samples/Kook.Net.Samples.OAuth/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Security.Claims;
using System.Text;
using AspNet.Security.OAuth.Kook;
using Kook;
using Kook.Rest;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.LoginPath = "/signin";
options.LogoutPath = "/signout";
})
.AddKook(options =>
{
options.ClientId = string.Empty;
options.ClientSecret = string.Empty;
// get_user_info is added by default
options.Scope.Add("get_user_guilds");
options.Events.OnCreatingTicket = async context =>
{
if (context.AccessToken is not { } accessToken)
return;
KookRestClient kookRestClient = new();
await kookRestClient.LoginAsync(TokenType.Bearer, accessToken);
RestSelfUser? currentUser = kookRestClient.CurrentUser;
IReadOnlyCollection<RestGuild> guilds = await kookRestClient.GetGuildsAsync();
IReadOnlyCollection<RestGuild> adminGuilds = await kookRestClient.GetAdminGuildsAsync();
};
});
WebApplication app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthentication();
app.MapGet("/signin",
() => Results.Challenge(new AuthenticationProperties { RedirectUri = "/" },
[KookAuthenticationDefaults.AuthenticationScheme]));
app.MapGet("/signout",
() => Results.SignOut(new AuthenticationProperties { RedirectUri = "/" },
[CookieAuthenticationDefaults.AuthenticationScheme]));
app.MapGet("/",
(HttpContext context) =>
{
if (context.User is { Identity.IsAuthenticated: true } claimsPrincipal)
{
StringBuilder contentBuilder = new();
contentBuilder.AppendLine($"Hello, {claimsPrincipal.Identity.Name}!<br />");
Dictionary<string, string> claims = claimsPrincipal.Claims.ToDictionary(x => x.Type, x => x.Value);
if (claims.TryGetValue(ClaimTypes.NameIdentifier, out string? nameIdentifier))
contentBuilder.AppendLine($"Identifier: {nameIdentifier}<br />");
if (claims.TryGetValue(ClaimTypes.Name, out string? name))
contentBuilder.AppendLine($"Name: {name}<br />");
if (claims.TryGetValue(ClaimTypes.MobilePhone, out string? mobilePhone))
contentBuilder.AppendLine($"Mobile phone: {mobilePhone}<br />");
if (claims.TryGetValue(KookAuthenticationConstants.Claims.IdentifyNumber, out string? identifyNumber))
contentBuilder.AppendLine($"Identify number: {identifyNumber}<br />");
if (claims.TryGetValue(KookAuthenticationConstants.Claims.OperatingSystem, out string? operatingSystem))
contentBuilder.AppendLine($"Operating system: {operatingSystem}<br />");
if (claims.TryGetValue(KookAuthenticationConstants.Claims.AvatarUrl, out string? avatarUrl))
contentBuilder.AppendLine($"Avatar URL: {avatarUrl}<br />");
if (claims.TryGetValue(KookAuthenticationConstants.Claims.BannerUrl, out string? bannerUrl))
contentBuilder.AppendLine($"Banner URL: {bannerUrl}<br />");
if (claims.TryGetValue(KookAuthenticationConstants.Claims.IsMobileVerified, out string? isMobileVerified))
contentBuilder.AppendLine($"Mobile verified: {isMobileVerified}<br />");
contentBuilder.AppendLine("<a href=\"/signout\">Sign out</a>");
return Results.Content(contentBuilder.ToString(), "text/html", Encoding.Default);
}

return Results.Content("<a href=\"/signin\">Sign in</a>", "text/html");
});

await app.RunAsync();
31 changes: 31 additions & 0 deletions samples/Kook.Net.Samples.OAuth/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5043",
"sslPort": 5044
}
},
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "https://kook.natfrp.gehongyan.cn/",
"applicationUrl": "https://localhost:5044;http://localhost:5043",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
9 changes: 9 additions & 0 deletions samples/Kook.Net.Samples.OAuth/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
9 changes: 9 additions & 0 deletions samples/Kook.Net.Samples.Webhook.AspNet/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
2 changes: 1 addition & 1 deletion src/Kook.Net.Core/Entities/Users/IUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public interface IUser : IEntity<ulong>, IMentionable, IPresence
/// <summary>
/// Gets the link to this user's BUFF avatar.
/// </summary>
string BuffAvatar { get; }
string? BuffAvatar { get; }

/// <summary>
/// Gets the link to this user's banner.
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/API/Common/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class User
public required string Avatar { get; set; }

[JsonPropertyName("vip_avatar")]
public required string BuffAvatar { get; set; }
public string? BuffAvatar { get; set; }

[JsonPropertyName("is_vip")]
public bool? HasBuff { get; set; }
Expand Down
3 changes: 1 addition & 2 deletions src/Kook.Net.Rest/Entities/Users/RestUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class RestUser : RestEntity<ulong>, IUser, IUpdateable
public string Avatar { get; internal set; }

/// <inheritdoc />
public string BuffAvatar { get; internal set; }
public string? BuffAvatar { get; internal set; }

/// <inheritdoc />
public string? Banner { get; internal set; }
Expand Down Expand Up @@ -76,7 +76,6 @@ internal RestUser(BaseKookClient kook, ulong id)
{
Username = string.Empty;
Avatar = string.Empty;
BuffAvatar = string.Empty;
Banner = string.Empty;
Nameplates = [];
Presence = new RestPresence();
Expand Down
3 changes: 1 addition & 2 deletions src/Kook.Net.WebSocket/Entities/Users/SocketGlobalUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal sealed class SocketGlobalUser : SocketUser
public override string Avatar { get; internal set; }

/// <inheritdoc />
public override string BuffAvatar { get; internal set; }
public override string? BuffAvatar { get; internal set; }

/// <inheritdoc />
public override string? Banner { get; internal set; }
Expand All @@ -56,7 +56,6 @@ public SocketGlobalUser(KookSocketClient kook, ulong id)
{
Username = string.Empty;
Avatar = string.Empty;
BuffAvatar = string.Empty;
Banner = string.Empty;
Nameplates = [];
Presence = new SocketPresence();
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.WebSocket/Entities/Users/SocketGuildUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public override string Avatar
}

/// <inheritdoc />
public override string BuffAvatar
public override string? BuffAvatar
{
get => GlobalUser.BuffAvatar;
internal set => GlobalUser.BuffAvatar = value;
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.WebSocket/Entities/Users/SocketSelfUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override string Avatar
}

/// <inheritdoc />
public override string BuffAvatar
public override string? BuffAvatar
{
get => GlobalUser.BuffAvatar;
internal set => GlobalUser.BuffAvatar = value;
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.WebSocket/Entities/Users/SocketUnknownUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SocketUnknownUser : SocketUser
public override string Avatar { get; internal set; } = string.Empty;

/// <inheritdoc />
public override string BuffAvatar { get; internal set; } = string.Empty;
public override string? BuffAvatar { get; internal set; } = string.Empty;

/// <inheritdoc />
public override string? Banner { get; internal set; } = string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.WebSocket/Entities/Users/SocketUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class SocketUser : SocketEntity<ulong>, IUser
public abstract string Avatar { get; internal set; }

/// <inheritdoc />
public abstract string BuffAvatar { get; internal set; }
public abstract string? BuffAvatar { get; internal set; }

/// <inheritdoc />
public abstract string? Banner { get; internal set; }
Expand Down

0 comments on commit 80b3772

Please sign in to comment.