Skip to content

Commit 16cea25

Browse files
rasmuskfcampbell
andauthored
docs: Provide easy alternative to create App JWT token (#2937)
* Provide easy alternative to create App JWT token * Make it clear that its the key content * Commit suggested changes by @kfcampbell Co-authored-by: Keegan Campbell <me@kfcampbell.com> * Add a reminder regarding the required using statements --------- Co-authored-by: Keegan Campbell <me@kfcampbell.com>
1 parent c2aee1a commit 16cea25

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

docs/github-apps.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,43 @@ That concludes the walkthrough!
100100
### A Note on JWT Tokens
101101
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`.
102102

103-
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.
103+
In order to create the token, you can create it manually using the following snippet.
104+
105+
``` csharp
106+
// Have these using statements in your file
107+
// using System.IdentityModel.Tokens.Jwt
108+
// using System.Security.Claims
109+
// using System.Security.Cryptography
110+
111+
var rsaPrivateKey = "-----BEGIN R..."; // The RSA private key content itself, read from e.g. a file
112+
var appId = 1; // The GitHub App Id
113+
114+
using var rsa = RSA.Create();
115+
rsa.ImportFromPem(rsaPrivateKey);
116+
var signingCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256)
117+
{
118+
CryptoProviderFactory = new CryptoProviderFactory
119+
{
120+
CacheSignatureProviders = false
121+
}
122+
};
123+
124+
var now = DateTime.UtcNow;
125+
var expiresAt = now + TokenLifetime;
126+
var jwt = new JwtSecurityToken(
127+
notBefore: now,
128+
expires: now + TimeSpan.FromMinutes(10),
129+
signingCredentials: signingCredentials,
130+
claims: new[]
131+
{
132+
new Claim("iat", new DateTimeOffset(now).ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer),
133+
new Claim("iss", appId.ToString(), ClaimValueTypes.Integer),
134+
}
135+
);
136+
var token = new JwtSecurityTokenHandler().WriteToken(jwt);
137+
```
138+
139+
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.
104140

105141
``` csharp
106142
// Use GitHubJwt library to create the GitHubApp Jwt Token using our private certificate PEM file

0 commit comments

Comments
 (0)