-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
docs: Provide easy alternative to create App JWT token #2937
Changes from 1 commit
b898a5f
5401f35
b99c46e
ef9bc48
26da3f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,38 @@ That concludes the walkthrough! | |
### A Note on JWT Tokens | ||
Octokit.net aims to have no external dependencies, therefore we do not currently have the ability to generate/sign JWT tokens for you, and instead expect that you will pass in the appropriately signed JWT token required to authenticate the `GitHubApp`. | ||
|
||
Luckily one of our contributors [@adriangodong](https://github.com/adriangodong) has created a library `GitHubJwt` ( [GitHub](https://github.com/adriangodong/githubjwt) | [NuGet](https://www.nuget.org/packages/githubjwt) ) which you can use as per the following example. | ||
In order to create the token, you can create it manually using the following snippet. | ||
|
||
``` csharp | ||
var rsaPrivateKey = "..."; // RSA private key from the App configuration page | ||
var appId = 1; // The GitHub App Id | ||
|
||
using var rsa = RSA.Create(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the advantage of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the |
||
rsa.ImportFromPem(rsaPrivateKey); | ||
var signingCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256) | ||
{ | ||
CryptoProviderFactory = new CryptoProviderFactory | ||
{ | ||
CacheSignatureProviders = false | ||
} | ||
}; | ||
|
||
var now = DateTime.UtcNow; | ||
var expiresAt = now + TokenLifetime; | ||
var jwt = new JwtSecurityToken( | ||
notBefore: now, | ||
expires: now + TimeSpan.FromMinutes(10), | ||
signingCredentials: signingCredentials, | ||
claims: new[] | ||
{ | ||
new Claim("iat", new DateTimeOffset(now).ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer), | ||
new Claim("iss", appId.ToString(), ClaimValueTypes.Integer), | ||
} | ||
); | ||
var token = new JwtSecurityTokenHandler().WriteToken(jwt); | ||
``` | ||
|
||
Alternatively, one of our contributors [@adriangodong](https://github.com/adriangodong) has created a library `GitHubJwt` ( [GitHub](https://github.com/adriangodong/githubjwt) | [NuGet](https://www.nuget.org/packages/githubjwt) ) which you can use as per the following example. | ||
rasmus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
``` csharp | ||
// Use GitHubJwt library to create the GitHubApp Jwt Token using our private certificate PEM file | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the key itself or the path to the key? I think this could be made more clear.