I'm using ASP.NET Core to serve an API to an Android client. Android signs in as a Google account and passes a JWT, the ID Token, to API as a bearer token. I have the app working, it does pass the auth checks, but I don't think it's validating the token signature.
Per Google's documents, I can call this url to do it: https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123, but I can't find the appropriate hooks on the server side to do it. Also according to the Google docs, I can somehow use the Client Access APIs to do it without calling to the server every time.
My configuration code:
app.UseJwtBearerAuthentication( new JwtBearerOptions()
{
Authority = "https://accounts.google.com",
Audience = "hiddenfromyou.apps.googleusercontent.com",
TokenValidationParameters = new TokenValidationParameters()
{
ValidateAudience = true,
ValidIssuer = "accounts.google.com"
},
RequireHttpsMetadata = false,
AutomaticAuthenticate = true,
AutomaticChallenge = false,
});
How do I get the JWTBearer middleware to validate the signature? I'm close to giving up on using the MS middleware and rolling my own.
There are a couple of different ways in which you can validate the integrity of the ID token on the server side:
iss
one; the main advantage (albeit a small one in my opinion) I see here is that you can minimize the number of requests sent to Google.https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={0}
Here's how the second one could look:
GoogleApiTokenInfo class:
Google states in the documentation for openId connect
You should not be using that endpoint to validate your JWT.
Validation of an ID token requires several steps:
There is an official sample project on how to validate them here. Unfortunately we have not added this to the Google .Net Client library yet. It has been logged as an issue
So, what I found is that as the OpenIDConnect specs have a /.well-known/ url that contains the information that you need to validate a token. This includes access to the public keys for the signature. The JWT middleware forms that .well-known url from the authority, retrieves the information, and proceeds to validate it on it's own.
The short answer to the question is that the validation is already happening in the middleware, there's nothing left to do.
According to this github issue, you can now use
GoogleJsonWebSignature.ValidateAsync
method to validate a Google-signed JWT. Simply pass theidToken
string to the method.If it is not a valid one, it will return
null
.Note that to use this method, you need to install Google.Apis.Auth nuget firsthand.