Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Add more certificate options for AddJWTBearerAuth Asymmetric #624

Closed
SpaceOgre opened this issue Feb 28, 2024 · 1 comment
Labels
enhancement New feature or request implemented requested feature has been implemented

Comments

@SpaceOgre
Copy link

Hi,

When switching from Symmetric to Asymmetric signing we ran into a problem that our public key that we first created could not be used by FastEndpoints since it only supports RSA public keys (so PKCS1 I think), meaning keys that starts with:

-----BEGIN RSA PUBLIC KEY-----

We created a PEM key (PKCS8) that have this header:

-----BEGIN PUBLIC KEY-----

Since then we have solved it and was able to convert the key to PKCS1, using this if anyone else finds this issue searching for a solution:

openssl rsa -pubin -in public.key -RSAPublicKey_out -out public_pkcs1.pem

It is posible to import a PEM key using the RSA class like this:

var rsaPublicKey = RSA.Create();
rsaPublicKey.ImportFromPem(publicKeyString);

So adding an option for specifying the key type would be great, I could look into it if you feel like this is something that would fit for FE.

Otherwise just adding some more information to the documentation about what type of key that is required would be good.

dj-nitehawk added a commit that referenced this issue Feb 28, 2024
@dj-nitehawk
Copy link
Member

starting v5.22.0.18-beta you can now do this:

builder.Services.AddAuthenticationJwtBearer(
       s =>
       {
           s.SigningKey = "public key pem data";
           s.SigningStyle = TokenSigningStyle.Asymmetric;
           s.KeyIsPemEncoded = true;
       })

token creation can also specify pem:

var token = JwtBearer.CreateToken(
    o =>
    {
        o.SigningKey = "private key pem data";
        o.SigningStyle = TokenSigningStyle.Asymmetric;
        o.KeyIsPemEncoded = true;
        o.AsymmetricKeyAlgorithm = SecurityAlgorithms.Sha256;
        o.ExpireAt = DateTime.UtcNow.AddDays(1);
        o.User.Permissions.Add("Some_Permission");
        o.User.Roles.Add("Admin");
        o.User.Claims.Add(("UserId", "001"));
    });

check it out and let me know if you encounter any issues.

thanks!

@dj-nitehawk dj-nitehawk added enhancement New feature or request implemented requested feature has been implemented labels Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request implemented requested feature has been implemented
Development

No branches or pull requests

2 participants