Files

47 lines
1.5 KiB
C#

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;
}
}
}
}