|
5 | 5 | [](https://codecov.io/gh/jferrl/go-githubauth) |
6 | 6 | [](https://goreportcard.com/report/github.com/jferrl/go-githubauth) |
7 | 7 |
|
8 | | -`go-githubauth` is a Go package that provides utilities for GitHub authentication, including generating and using GitHub App tokens and installation tokens. |
| 8 | +`go-githubauth` is a Go package that provides utilities for GitHub authentication, including generating and using GitHub App tokens, installation tokens, and personal access tokens. |
9 | 9 |
|
10 | | -**v1.3.0** introduces Go generics support for unified authentication with both numeric App IDs and alphanumeric Client IDs in a single, type-safe API. |
| 10 | +**v1.4.0** introduces personal access token support and significant performance optimizations with intelligent token caching and high-performance HTTP clients. |
11 | 11 |
|
12 | 12 | --- |
13 | 13 |
|
|
25 | 25 |
|
26 | 26 | `go-githubauth` package provides implementations of the `TokenSource` interface from the `golang.org/x/oauth2` package. This interface has a single method, Token, which returns an *oauth2.Token. |
27 | 27 |
|
28 | | -### v1.3.0 Features |
| 28 | +### v1.4.0 Features |
| 29 | + |
| 30 | +- **🔐 Personal Access Token Support**: Native support for both classic and fine-grained personal access tokens |
| 31 | +- **⚡ Advanced Token Caching**: Dual-layer caching system for optimal performance |
| 32 | + - JWT tokens cached until expiration (up to 10 minutes) |
| 33 | + - Installation tokens cached until expiration (defined by GitHub response) |
| 34 | +- **🚀 High-Performance HTTP Client**: Production-ready HTTP client with connection pooling |
| 35 | +- **📈 Performance Optimizations**: Up to 99% reduction in unnecessary GitHub API calls |
| 36 | +- **🏗️ Production Ready**: Optimized for high-throughput and enterprise applications |
| 37 | + |
| 38 | +### Other Features |
29 | 39 |
|
30 | 40 | - **🔥 Go Generics Support**: Single `NewApplicationTokenSource` function supports both `int64` App IDs and `string` Client IDs |
31 | 41 | - **🛡️ Type Safety**: Compile-time verification of identifier types through generic constraints |
|
36 | 46 |
|
37 | 47 | - Generate GitHub Application JWT [Generating a jwt for a github app](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app) |
38 | 48 | - Obtain GitHub App installation tokens [Authenticating as a GitHub App](https://docs.github.com/en/rest/authentication/authenticating-to-the-rest-api?apiVersion=2022-11-28#authenticating-with-a-token-generated-by-an-app) |
| 49 | +- Authenticate with Personal Access Tokens (classic and fine-grained) [Managing your personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens) |
39 | 50 | - RS256-signed JWTs with proper clock drift protection |
40 | 51 | - Support for both legacy App IDs and modern Client IDs (recommended by GitHub) |
| 52 | +- Intelligent token caching with automatic refresh for optimal performance |
| 53 | +- Clean HTTP clients with connection pooling and no shared state |
41 | 54 |
|
42 | 55 | ### Requirements |
43 | 56 |
|
@@ -263,6 +276,54 @@ func main() { |
263 | 276 | } |
264 | 277 | ``` |
265 | 278 |
|
| 279 | +### Personal Access Token Authentication |
| 280 | + |
| 281 | +GitHub Personal Access Tokens provide direct authentication for users and organizations. This package supports both classic personal access tokens and fine-grained personal access tokens. |
| 282 | + |
| 283 | +#### Using Personal Access Tokens with [go-github](https://github.com/google/go-github) |
| 284 | + |
| 285 | +```go |
| 286 | +package main |
| 287 | + |
| 288 | +import ( |
| 289 | + "context" |
| 290 | + "fmt" |
| 291 | + "os" |
| 292 | + |
| 293 | + "github.com/google/go-github/v73/github" |
| 294 | + "github.com/jferrl/go-githubauth" |
| 295 | + "golang.org/x/oauth2" |
| 296 | +) |
| 297 | + |
| 298 | +func main() { |
| 299 | + // Personal access token from environment variable |
| 300 | + token := os.Getenv("GITHUB_TOKEN") // e.g., "ghp_..." or "github_pat_..." |
| 301 | + |
| 302 | + // Create token source |
| 303 | + tokenSource := githubauth.NewPersonalAccessTokenSource(token) |
| 304 | + |
| 305 | + // Create HTTP client with OAuth2 transport |
| 306 | + httpClient := oauth2.NewClient(context.Background(), tokenSource) |
| 307 | + githubClient := github.NewClient(httpClient) |
| 308 | + |
| 309 | + // Use the GitHub client for API calls |
| 310 | + user, _, err := githubClient.Users.Get(context.Background(), "") |
| 311 | + if err != nil { |
| 312 | + fmt.Println("Error getting user:", err) |
| 313 | + return |
| 314 | + } |
| 315 | + |
| 316 | + fmt.Printf("Authenticated as: %s\n", user.GetLogin()) |
| 317 | +} |
| 318 | +``` |
| 319 | + |
| 320 | +#### Creating Personal Access Tokens |
| 321 | + |
| 322 | +1. **Classic Personal Access Token**: Visit [GitHub Settings > Developer settings > Personal access tokens > Tokens (classic)](https://github.com/settings/tokens) |
| 323 | +2. **Fine-grained Personal Access Token**: Visit [GitHub Settings > Developer settings > Personal access tokens > Fine-grained tokens](https://github.com/settings/personal-access-tokens/new) |
| 324 | + |
| 325 | +**Security Note**: Store your personal access tokens securely and never commit them to version control. Use environment variables or secure credential management systems. |
| 326 | + |
266 | 327 | ## Contributing |
267 | 328 |
|
268 | 329 | Contributions are welcome! Please open an issue or submit a pull request on GitHub. |
|
0 commit comments