-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
2,439 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using BUTR.Site.NexusMods.ServerClient; | ||
|
||
namespace BUTR.Site.NexusMods.Client.Models | ||
{ | ||
public sealed record GitHubUserInfo2 | ||
{ | ||
public string Url { get; init; } | ||
public string Name { get; init; } | ||
public bool NeedsRelink { get; init; } | ||
|
||
public GitHubUserInfo2(GitHubUserInfo? userInfo) | ||
{ | ||
if (userInfo is null) | ||
{ | ||
NeedsRelink = true; | ||
Url = string.Empty; | ||
Name = string.Empty; | ||
} | ||
else | ||
{ | ||
Url = $"https://github.com/{userInfo.Login}"; | ||
Name = userInfo.Login; | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/BUTR.Site.NexusMods.Client/Pages/GitHub/LinkedRole.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@attribute [Authorize] | ||
@page "/github-linked-role" | ||
|
||
@inject IGitHubClient _gitHubClient; | ||
@inject ILocalStorageService _localStorage; | ||
@inject NavigationManager _navigationManager; | ||
|
||
@code { | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await base.OnInitializedAsync(); | ||
|
||
var response = await _gitHubClient.GetOAuthUrlAsync(); | ||
if (response.Value?.Url is null) | ||
{ | ||
_navigationManager.NavigateTo("profile"); | ||
return; | ||
} | ||
|
||
await _localStorage.SetItemAsync("github_state", response.Value.State); | ||
_navigationManager.NavigateTo(response.Value.Url); | ||
} | ||
|
||
} |
127 changes: 127 additions & 0 deletions
127
src/BUTR.Site.NexusMods.Client/Pages/GitHub/OAuthCallback.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
@attribute [Authorize] | ||
@page "/github-oauth-callback" | ||
|
||
@inject NavigationManager _navigationManager; | ||
@inject ILocalStorageService _localStorage; | ||
@inject AuthenticationProvider _authenticationProvider; | ||
@inject IGitHubClient _gitHubClient; | ||
|
||
<Container> | ||
<Row Flex="@Flex.JustifyContent.Center"> | ||
<Column ColumnSize="@ColumnSize.Is7.OnWidescreen.IsAuto.OnDesktop"> | ||
<Card Margin="@Margin.Is5.OnDesktop.Is4.OnTablet.Is3.OnMobile" Border="@Border.Is0.Rounded" Shadow="@Shadow.Small"> | ||
<CardBody> | ||
<Heading Padding="@Padding.Is3" Size="@HeadingSize.Is5" TextAlignment="@TextAlignment.Center" TextTransform="@TextTransform.Uppercase" TextWeight="@TextWeight.Bold">@_status</Heading> | ||
<Divider/> | ||
@if (_userInfo is not null) | ||
{ | ||
<Row Margin="@Margin.Is2"> | ||
<Span><Anchor Style="text-decoration: none" To="@_userInfo.Url" Target="@Target.Blank">@_userInfo.Name</Anchor> was successfully linked with the BUTR Site!</Span> | ||
</Row> | ||
} | ||
else | ||
{ | ||
<Row Margin="@Margin.Is2"> | ||
<Spinner/> | ||
</Row> | ||
} | ||
|
||
@if (!string.IsNullOrEmpty(_message)) | ||
{ | ||
<Row Margin="@Margin.Is2"> | ||
<Span>@_message</Span> | ||
</Row> | ||
} | ||
|
||
@if (!string.IsNullOrEmpty(_image)) | ||
{ | ||
<Row Margin="@Margin.Is2"> | ||
<FigureImage Margin="@Margin.Is0" Source="@_image" AlternateText="A meme image displaying success or failure. Success is Brent Rambo giving a thumbs up. Failure is a horse failing to play with a gymnastics by kinda lying onto it ball and falling."></FigureImage> | ||
</Row> | ||
} | ||
|
||
<Row Margin="@Margin.Is2"> | ||
<Span>Use the "Linked Roles" option in servers with the BUTR Discord bot to claim your roles.</Span> | ||
</Row> | ||
<Row Margin="@Margin.Is2"> | ||
<Button Border="@Border.RoundedPill" | ||
TextTransform="@TextTransform.Uppercase" | ||
Padding="@Padding.Is2" | ||
TextWeight="@TextWeight.Bold" | ||
Color="@Color.Primary" | ||
Type="@ButtonType.Link" | ||
To="discord://-/" | ||
rel="noreferrer" | ||
Target="@Target.Blank"> | ||
<Figure Margin="@Margin.Is0" Size="@FigureSize.Is32x32"> | ||
<FigureImage Margin="@Margin.Is0" AlternateText="Discord app icon." Source="images/discord.svg"/> | ||
</Figure> | ||
Back to Discord | ||
</Button> | ||
</Row> | ||
</CardBody> | ||
</Card> | ||
</Column> | ||
</Row> | ||
</Container> | ||
|
||
@code { | ||
|
||
private const string Success = "images/success.gif"; | ||
private const string Failure = "images/failure.gif"; | ||
|
||
private string _status = string.Empty; | ||
private string _message = string.Empty; | ||
private string _image = string.Empty; | ||
private GitHubUserInfo2? _userInfo; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await base.OnInitializedAsync(); | ||
|
||
if (!await _localStorage.ContainKeyAsync("github_state")) | ||
{ | ||
_status = "FAILURE"; | ||
_message = "State verification failed."; | ||
_image = Failure; | ||
return; | ||
} | ||
|
||
var queries = _navigationManager.QueryString(); | ||
var queryStatRaw = queries["state"]; | ||
var queryCode = queries["code"]; | ||
|
||
try | ||
{ | ||
var state = await _localStorage.GetItemAsync<Guid>("github_state"); | ||
if (!Guid.TryParse(queryStatRaw, out var queryState) || state != queryState) | ||
{ | ||
_status = "FAILURE"; | ||
_message = "State verification failed."; | ||
_image = Failure; | ||
return; | ||
} | ||
|
||
await _gitHubClient.LinkAsync(code: queryCode); | ||
_ = await _authenticationProvider.ValidateAsync(); | ||
|
||
if (await _gitHubClient.GetUserInfoAsync() is { Value: var userInfo }) | ||
{ | ||
_userInfo = new GitHubUserInfo2(userInfo); | ||
_status = "SUCCESS"; | ||
_image = Success; | ||
} | ||
else | ||
{ | ||
_status = "FAILURE"; | ||
_message = "Failed to link!"; | ||
_image = Failure; | ||
} | ||
} | ||
finally | ||
{ | ||
await _localStorage.RemoveItemAsync("github_state"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...BUTR.Site.NexusMods.Server/Contexts/Configs/IntegrationGitHubTokensEntityConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using BUTR.Site.NexusMods.Server.Models; | ||
using BUTR.Site.NexusMods.Server.Models.Database; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace BUTR.Site.NexusMods.Server.Contexts.Configs; | ||
|
||
public class IntegrationGitHubTokensEntityConfiguration : BaseEntityConfiguration<IntegrationGitHubTokensEntity> | ||
{ | ||
protected override void ConfigureModel(EntityTypeBuilder<IntegrationGitHubTokensEntity> builder) | ||
{ | ||
builder.Property<NexusModsUserId>(nameof(NexusModsUserEntity.NexusModsUserId)).HasColumnName("integration_github_tokens_id").HasValueObjectConversion().ValueGeneratedNever(); | ||
builder.Property(x => x.GitHubUserId).HasColumnName("github_user_id"); | ||
builder.Property(x => x.AccessToken).HasColumnName("access_token"); | ||
builder.ToTable("integration_github_tokens", "integration").HasKey(nameof(NexusModsUserEntity.NexusModsUserId)); | ||
|
||
builder.HasOne(x => x.NexusModsUser) | ||
.WithOne() | ||
.HasForeignKey<IntegrationGitHubTokensEntity>(nameof(NexusModsUserEntity.NexusModsUserId)) | ||
.HasPrincipalKey<NexusModsUserEntity>(x => x.NexusModsUserId) | ||
.OnDelete(DeleteBehavior.Cascade); | ||
|
||
builder.HasOne(x => x.UserToGitHub) | ||
.WithOne(x => x.ToTokens) | ||
.HasForeignKey<IntegrationGitHubTokensEntity>(x => x.GitHubUserId) | ||
.HasPrincipalKey<NexusModsUserToIntegrationGitHubEntity>(x => x.GitHubUserId) | ||
.OnDelete(DeleteBehavior.Cascade); | ||
|
||
builder.Navigation(x => x.NexusModsUser).AutoInclude(); | ||
|
||
base.ConfigureModel(builder); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
....NexusMods.Server/Contexts/Configs/NexusModsUserToIntegrationGitHubEntityConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using BUTR.Site.NexusMods.Server.Models; | ||
using BUTR.Site.NexusMods.Server.Models.Database; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace BUTR.Site.NexusMods.Server.Contexts.Configs; | ||
|
||
public class NexusModsUserToIntegrationGitHubEntityConfiguration : BaseEntityConfiguration<NexusModsUserToIntegrationGitHubEntity> | ||
{ | ||
protected override void ConfigureModel(EntityTypeBuilder<NexusModsUserToIntegrationGitHubEntity> builder) | ||
{ | ||
builder.Property<NexusModsUserId>(nameof(NexusModsUserEntity.NexusModsUserId)).HasColumnName("nexusmods_user_to_github_id").HasValueObjectConversion().ValueGeneratedNever(); | ||
builder.Property(x => x.GitHubUserId).HasColumnName("github_user_id"); | ||
builder.ToTable("nexusmods_user_to_integration_github", "nexusmods_user").HasKey(nameof(NexusModsUserEntity.NexusModsUserId)); | ||
|
||
builder.HasOne(x => x.NexusModsUser) | ||
.WithOne(x => x.ToGitHub) | ||
.HasForeignKey<NexusModsUserToIntegrationGitHubEntity>(nameof(NexusModsUserEntity.NexusModsUserId)) | ||
.HasPrincipalKey<NexusModsUserEntity>(x => x.NexusModsUserId) | ||
.OnDelete(DeleteBehavior.Cascade); | ||
|
||
builder.Navigation(x => x.NexusModsUser).AutoInclude(); | ||
|
||
base.ConfigureModel(builder); | ||
} | ||
} |
Oops, something went wrong.