I am trying to integrate Firebase authenticate to Asp.core.
I can interact with Firebase,however, face issue when authenticate the api.
Error description on JWT event:
OnAuthenticationFailed: IDX10503: Signature validation failed. Token does not have a kid. Keys tried: '[PII of type 'System.Text.StringBuilder' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'. Number of keys in TokenValidationParameters: '3'.
Number of keys in Configuration: '0'.
Exceptions caught:
'[PII of type 'System.Text.StringBuilder' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'.
token: '[PII of type 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'. See https://aka.ms/IDX10503 for details.
What I have done: I mostly follow instruction on youtube
Nuget: <PackageReference Include="FirebaseAdmin" Version="3.0.0" />
My configuration:
Try the securetoken URL
My JWT config:
public static IServiceCollection FireBaseJWT(this IServiceCollection services, WebApplicationBuilder builder)
{
Console.WriteLine(Path.Combine(Directory.GetCurrentDirectory()));
//Initialize Firebase Admin SDK
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile(Path.Combine(Directory.GetCurrentDirectory(), "Config/firebase.json")),
});
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = builder.Configuration["FirebaseJwt:Firebase:ValidIssuer"];
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = builder.Configuration["FirebaseJwt:Firebase:ValidIssuer"],
ValidateAudience = true,
ValidAudience = builder.Configuration["FirebaseJwt:Firebase:ValidAudience"],
ValidateLifetime = true,
ValidateIssuerSigningKey = true
};
options.Events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
Console.WriteLine("OnAuthenticationFailed: " + context.Exception.Message);
return Task.CompletedTask;
},
OnTokenValidated = context =>
{
Console.WriteLine("OnTokenValidated: " + context.SecurityToken);
return Task.CompletedTask;
}
};
});
return services;
}
Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.FireBaseJWT(builder);
builder.Services.AddAuthorization();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.MySwaggerConfig();
builder.Services.MyDIConfig();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
What I been considering:
- Normally, there should be
IssuerSigningKey
inTokenValidationParameters
which is our private key, I wonder if it has anything to do with firebase authen key in this image - This Document say we need to manually verify the token, there also a video build a middleware for authenticate handler. Does this worth it when it come to authorization handler since we need to deal with Claims stuffs.