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 _logger; public AuthenticationMutation( IGitHubOauthService guGitHubOauthService, ILogger logger) { _guGitHubOauthService = guGitHubOauthService; _logger = logger; } /// /// This method accepts GitHub oauth code, and returns JWT token with GutHub user as claim /// /// OAuth code can be generated by this URL https://github.com/login/oauth/authorize?client_id={yourClientId} public async Task 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; } } } }