From f7ed8b22600cc5cdbac216ecaa136e6fa0587978 Mon Sep 17 00:00:00 2001 From: jorge-ferrero-ag Date: Sat, 1 Jun 2024 18:43:06 +0200 Subject: [PATCH 1/3] chore: update readme to include go-githubauth example --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b243c795bd9..a6d3f80f2e4 100644 --- a/README.md +++ b/README.md @@ -101,8 +101,8 @@ For API methods that require HTTP Basic Authentication, use the #### As a GitHub App #### -GitHub Apps authentication can be provided by the [ghinstallation](https://github.com/bradleyfalzon/ghinstallation) -package. +GitHub Apps authentication can be provided by different pkgs like [ghinstallation](https://github.com/bradleyfalzon/ghinstallation) +package or [go-githubauth](github.com/jferrl/go-githubauth). > **Note**: Most endpoints (ex. [`GET /rate_limit`]) require access token authentication > while a few others (ex. [`GET /app/hook/deliveries`]) require [JWT] authentication. @@ -111,6 +111,9 @@ package. [`GET /app/hook/deliveries`]: https://docs.github.com/en/rest/apps/webhooks#list-deliveries-for-an-app-webhook [JWT]: https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-a-github-app +`ghinstallation` provides `Transport`, which implements `http.RoundTripper` to provide authentication as an installation for GitHub Apps. + +Here is an example of how to authenticate as a GitHub App using the `ghinstallation` package: ```go import ( @@ -138,6 +141,44 @@ func main() { } ``` +`go-githubauth` implements a set of `oauth2.TokenSource` to be used with `oauth2.Client`. An `oauth2.Client` can be injected into the `github.Client` to authenticate requests. + +Other example using `go-githubauth`: + +```go +package main + +import ( + "context" + "fmt" + "os" + "strconv" + + "github.com/google/go-github/v62/github" + "github.com/jferrl/go-githubauth" + "golang.org/x/oauth2" +) + +func main() { + privateKey := []byte(os.Getenv("GITHUB_APP_PRIVATE_KEY")) + + appTokenSource, err := githubauth.NewApplicationTokenSource("app-id", privateKey) + if err != nil { + fmt.Println("Error creating application token source:", err) + return + } + + installationTokenSource := githubauth.NewInstallationTokenSource(20, appTokenSource) + + // oauth2.NewClient uses oauth2.ReuseTokenSource to reuse the token until it expires. + // The token will be automatically refreshed when it expires. + // InstallationTokenSource has the mechanism to refresh the token when it expires. + httpClient := oauth2.NewClient(context.Background(), installationTokenSource) + + client := github.NewClient(httpClient) +} +``` + *Note*: In order to interact with certain APIs, for example writing a file to a repo, one must generate an installation token using the installation ID of the GitHub app and authenticate with the OAuth method mentioned above. See the examples. From 3f9309788a9f6981f543d97d0ecab4580a4a63c8 Mon Sep 17 00:00:00 2001 From: jorge-ferrero-ag Date: Sat, 1 Jun 2024 18:49:19 +0200 Subject: [PATCH 2/3] chore: fix typo --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index a6d3f80f2e4..cea9ee450c7 100644 --- a/README.md +++ b/README.md @@ -101,8 +101,7 @@ For API methods that require HTTP Basic Authentication, use the #### As a GitHub App #### -GitHub Apps authentication can be provided by different pkgs like [ghinstallation](https://github.com/bradleyfalzon/ghinstallation) -package or [go-githubauth](github.com/jferrl/go-githubauth). +GitHub Apps authentication can be provided by different pkgs like [ghinstallation](https://github.com/bradleyfalzon/ghinstallation) or [go-githubauth](github.com/jferrl/go-githubauth). > **Note**: Most endpoints (ex. [`GET /rate_limit`]) require access token authentication > while a few others (ex. [`GET /app/hook/deliveries`]) require [JWT] authentication. From b2717c0c53ccb9b0aebf76cbd04fb9891980419b Mon Sep 17 00:00:00 2001 From: jorge-ferrero-ag Date: Sun, 2 Jun 2024 00:12:10 +0200 Subject: [PATCH 3/3] chore: update go code example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cea9ee450c7..06922ab780c 100644 --- a/README.md +++ b/README.md @@ -161,13 +161,13 @@ import ( func main() { privateKey := []byte(os.Getenv("GITHUB_APP_PRIVATE_KEY")) - appTokenSource, err := githubauth.NewApplicationTokenSource("app-id", privateKey) + appTokenSource, err := githubauth.NewApplicationTokenSource(1112, privateKey) if err != nil { fmt.Println("Error creating application token source:", err) return } - installationTokenSource := githubauth.NewInstallationTokenSource(20, appTokenSource) + installationTokenSource := githubauth.NewInstallationTokenSource(1113, appTokenSource) // oauth2.NewClient uses oauth2.ReuseTokenSource to reuse the token until it expires. // The token will be automatically refreshed when it expires.