mirror of
https://github.com/DrMboga/WotBlitzStatisticsBlazor.git
synced 2026-10-23 11:20:05 +02:00
feat: GitHub OAuth mechanism implemented
This commit is contained in:
1 parent
a247406a42
commit
0b5da2f7c6
9 files changed
+299
-9
No files matched your search
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using HotChocolate.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WotBlitzStatisticsPro.GraphQl.GitHubOauth;
|
||||
|
||||
|
||||
namespace WotBlitzStatisticsPro.GraphQl.Mutation
|
||||
{
|
||||
[ExtendObjectType(Name = "Mutation")]
|
||||
public class AuthenticationMutation
|
||||
{
|
||||
private readonly IGitHubOauthService _guGitHubOauthService;
|
||||
private readonly ILogger<AuthenticationMutation> _logger;
|
||||
|
||||
public AuthenticationMutation(
|
||||
IGitHubOauthService guGitHubOauthService,
|
||||
ILogger<AuthenticationMutation> logger)
|
||||
{
|
||||
_guGitHubOauthService = guGitHubOauthService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method accepts GitHub oauth code, and returns JWT token with GutHub user as claim
|
||||
/// </summary>
|
||||
/// <param name="oauthCode">OAuth code can be generated by this URL https://github.com/login/oauth/authorize?client_id={yourClientId}</param>
|
||||
public async Task<string> GitHubAuthenticate(string oauthCode)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await _guGitHubOauthService.GetGitHubUser(oauthCode);
|
||||
if (user == null)
|
||||
{
|
||||
throw new ApplicationException($"Can not find GitHub user by code {oauthCode}");
|
||||
}
|
||||
return _guGitHubOauthService.GenerateToken(user);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e,"Authentication fails");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using HotChocolate.AspNetCore.Authorization;
|
||||
using HotChocolate.Types;
|
||||
using WotBlitzStatisticsPro.Common.Model;
|
||||
using WotBlitzStatisticsPro.Logic;
|
||||
|
||||
@@ -7,6 +9,7 @@ namespace WotBlitzStatisticsPro.GraphQl.Mutation
|
||||
/// <summary>
|
||||
/// WotBlitzStatistics Mutations
|
||||
/// </summary>
|
||||
[ExtendObjectType(Name = "Mutation")]
|
||||
public class WotBlitzStatisticsMutation
|
||||
{
|
||||
private readonly IWargamingDictionaries _wargamingDictionaries;
|
||||
@@ -24,6 +27,7 @@ namespace WotBlitzStatisticsPro.GraphQl.Mutation
|
||||
/// </summary>
|
||||
/// <param name="updateDictionariesRequest">Determines what dictionaries should be updated.</param>
|
||||
/// <returns>Information about what was updated.</returns>
|
||||
[Authorize(Policy = "GithubMbogaOnly")]
|
||||
public Task<UpdateDictionariesResponseItem[]> UpdateDictionaries(
|
||||
UpdateDictionariesRequest updateDictionariesRequest)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user