Skip to content

Instantly share code, notes, and snippets.

@Chriz76
Last active March 9, 2021 12:27
Show Gist options
  • Save Chriz76/cacec657e02d670b2dc82d3e8c6f45dc to your computer and use it in GitHub Desktop.
Save Chriz76/cacec657e02d670b2dc82d3e8c6f45dc to your computer and use it in GitHub Desktop.
using Google.Apis.Auth;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace AuthService.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
public class AuthenticateRequest
{
[Required]
public string IdToken { get; set; }
}
private readonly JwtGenerator _jwtGenerator;
public UserController(IConfiguration configuration)
{
_jwtGenerator = new JwtGenerator(configuration.GetValue<string>("JwtPrivateSigningKey"));
}
[AllowAnonymous]
[HttpPost("authenticate")]
public IActionResult Authenticate([FromBody] AuthenticateRequest data)
{
GoogleJsonWebSignature.ValidationSettings settings = new GoogleJsonWebSignature.ValidationSettings();
// Change this to your google client ID
settings.Audience = new List<string>() { "708313847097-qqhkk449k8ut39q0uf0290rhvgm4cthh.apps.googleusercontent.com" };
GoogleJsonWebSignature.Payload payload = GoogleJsonWebSignature.ValidateAsync(data.IdToken, settings).Result;
return Ok(new { AuthToken = _jwtGenerator.CreateUserAuthToken(payload.Email) });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment