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

Improve documentation #75

Open
daviddesmet opened this issue Jun 1, 2022 · 0 comments
Open

Improve documentation #75

daviddesmet opened this issue Jun 1, 2022 · 0 comments
Assignees
Labels

Comments

@daviddesmet
Copy link
Owner

Improve the documentation, especially around the use of the keys.

See here for an example.

Excerpt:

...

I'm using Paseto.Core Nuget from https://github.com/daviddesmet/paseto-dotnet, and I'm trying to create v4 public PASETO token with this. My code:

public async Task<TokenResponse> GenerateAsync(Client client, TokenRequest tokenRequest, string issuer, string audience)
   {
       var privateEd25519Key = await File.ReadAllBytesAsync("private.pem");

       var pasetoToken = new PasetoBuilder()
           .Use(ProtocolVersion.V4, Purpose.Public)
           .WithKey(privateEd25519Key, Encryption.AsymmetricSecretKey)
           .Issuer(issuer)
           .Subject(tokenRequest.ClientId)
           .Audience(audience)
           .NotBefore(DateTime.UtcNow)
           .IssuedAt(DateTime.UtcNow)
           .Expiration(DateTime.UtcNow.AddSeconds(client.AccessTokenLifetime))
           .TokenIdentifier(Guid.NewGuid().ToString())
           .AddClaim("client_id", tokenRequest.ClientId)
           .AddClaim("scopes", tokenRequest.Scopes)
           .Encode();

       return new TokenResponse
       {
           AccessToken = pasetoToken,
           Lifetime = client.AccessTokenLifetime,
           Scope = tokenRequest.Scopes
       };
   }
}

Besides, due to the rather unclear documentation, I don't know if I'm really creating a token signed with a private key or encrypted.

...

Here is my solution:

public async Task<TokenResponse> GenerateAsync(Client client, TokenRequest tokenRequest, string issuer, string audience)
    {
        var ed25519pkcs8 = await File.ReadAllTextAsync("private.pem");

        var privatePemReader = new PemReader(new StringReader(ed25519pkcs8));
        var ed25519pkcs8Parameters = (Ed25519PrivateKeyParameters)privatePemReader.ReadObject();
        ISigner signer = new Ed25519Signer();
        signer.Init(true, ed25519pkcs8Parameters);

        var pasetoToken = new PasetoBuilder()
            .Use(ProtocolVersion.V4, Purpose.Public)
            .WithKey(signer.GenerateSignature(), Encryption.AsymmetricSecretKey)
            .Issuer(issuer)
            .Subject(tokenRequest.ClientId)
            .Audience(audience)
            .NotBefore(DateTime.UtcNow)
            .IssuedAt(DateTime.UtcNow)
            .Expiration(DateTime.UtcNow.AddSeconds(client.AccessTokenLifetime))
            .TokenIdentifier(Guid.NewGuid().ToString())
            .AddClaim("client_id", tokenRequest.ClientId)
            .AddClaim("scopes", tokenRequest.Scopes)
            .Encode();

        return new TokenResponse
        {
            AccessToken = pasetoToken,
            Lifetime = client.AccessTokenLifetime,
            Scope = tokenRequest.Scopes
        };
    }

It turned out that WithKey doesn't support PEM files, so you had to get the private key out of PKCS#8.

@daviddesmet daviddesmet self-assigned this Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant