I am developing a simple API that handles Authentication performed by Firebase for later use with Android clients.
So, in the Firebase console, I turned on the login methods on Facebook and Google and created a sample html page that I can use to check the login method - this next function is called by the button:
function loginFacebook() { var provider = new firebase.auth.FacebookAuthProvider(); var token = ""; firebase.auth().signInWithPopup(provider).then(function (result) { var token = result.credential.accessToken; var user = result.user; alert("login OK"); user.getToken().then(function (t) { token = t; loginAPI(); }); }).catch(function (error) { var errorCode = error.code; var errorMessage = error.message; alert(errorCode + " - " + errorMessage); }); }
next I use a token and send it to my API using a simple ajax call from jQuery here:
function loginAPI() { $.ajax({ url: "http://localhost:58041/v1/Users/", dataType: 'json', type: 'GET', beforeSend: function (xhr) { xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("Authorization", "Bearer " + token); }, error: function (ex) { console.log(ex.status + " - " + ex.statusText); }, success: function (data) { console.log(data); return data; } }); }
Next stop: API backend - written with .NET Core.
In the “Startup” section, I configured JwtBearer Auth ( Microsoft.AspNetCore.Authentication.JwtBearer package):
app.UseJwtBearerAuthentication(new JwtBearerOptions { AutomaticAuthenticate = true, IncludeErrorDetails = true, Authority = "https://securetoken.google.com/PROJECT-ID", TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = "https://securetoken.google.com/PROJECT-ID", ValidateAudience = true, ValidAudience = "PROJECT-ID", ValidateLifetime = true, }, });
And here is the controller code - with the [Authorize] attribute:
[Authorize] [Route("v1/[controller]")] public class UsersController : Controller { private readonly ILogger _logger; private readonly UserService _userService; public UsersController(ILogger<UsersController> logger, UserService userService) { _logger = logger; _userService = userService; } [HttpGet] public async Task<IList<User>> Get() { return await _userService.GetAll(); } }
API 200 OK HttpContext.User.Identity.IsAuthenticated ( HttpContext.User.Identity.IsAuthenticated is true inside the controller), but I think it shouldn't. My problem is that I'm not entirely sure that it is safe.
How to verify the JWT token signature part? I saw many code examples describing the x509 or RS256 algorithm, where do they fit? Should I check any certificate or private key with IssuerSigningKey or TokenDecryptionKey from the TokenValidationParameters class? What am I missing?
Relevant sources of knowledge about the problem: