{"id":20932884,"url":"https://github.com/jferrl/go-githubauth","last_synced_at":"2025-05-13T20:30:49.982Z","repository":{"id":242239644,"uuid":"809040130","full_name":"jferrl/go-githubauth","owner":"jferrl","description":"Go package that provides utilities for GitHub authentication","archived":false,"fork":false,"pushed_at":"2025-03-18T08:50:55.000Z","size":46,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T07:35:12.224Z","etag":null,"topics":["github","github-authentication","go","oauth2"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jferrl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-01T14:13:13.000Z","updated_at":"2025-03-18T08:48:26.000Z","dependencies_parsed_at":"2024-09-09T15:25:51.527Z","dependency_job_id":null,"html_url":"https://github.com/jferrl/go-githubauth","commit_stats":null,"previous_names":["jferrl/go-githubauth"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jferrl%2Fgo-githubauth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jferrl%2Fgo-githubauth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jferrl%2Fgo-githubauth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jferrl%2Fgo-githubauth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jferrl","download_url":"https://codeload.github.com/jferrl/go-githubauth/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254020902,"owners_count":22000805,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["github","github-authentication","go","oauth2"],"created_at":"2024-11-18T21:53:40.154Z","updated_at":"2025-05-13T20:30:49.675Z","avatar_url":"https://github.com/jferrl.png","language":"Go","readme":"# go-githubauth\n\n[![GoDoc](https://img.shields.io/static/v1?label=godoc\u0026message=reference\u0026color=blue)](https://pkg.go.dev/github.com/jferrl/go-githubauth)\n[![Test Status](https://github.com/jferrl/go-githubauth/workflows/tests/badge.svg)](https://github.com/jferrl/go-githubauth/actions?query=workflow%3Atests)\n[![codecov](https://codecov.io/gh/jferrl/go-githubauth/branch/main/graph/badge.svg?token=68I4BZF235)](https://codecov.io/gh/jferrl/go-githubauth)\n[![Go Report Card](https://goreportcard.com/badge/github.com/jferrl/go-githubauth)](https://goreportcard.com/report/github.com/jferrl/go-githubauth)\n\n`go-githubauth` is a Go package that provides utilities for GitHub authentication, including generating and using GitHub App tokens and installation tokens.\n\n## Features\n\n`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.\n\n- 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)\n- 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)\n\nThis package is designed to be used with the `golang.org/x/oauth2` package, which provides support for OAuth2 authentication.\n\n## Installation\n\nTo use `go-githubauth` in your project, you need to have Go installed. You can get the package via:\n\n```bash\ngo get -u github.com/jferrl/go-githubauth\n```\n\n## Usage\n\n### Usage with [go-github](https://github.com/google/go-github)  and [oauth2](golang.org/x/oauth2)\n\n```go\npackage main\n\nimport (\n \"context\"\n \"fmt\"\n \"os\"\n \"strconv\"\n\n \"github.com/google/go-github/v62/github\"\n \"github.com/jferrl/go-githubauth\"\n \"golang.org/x/oauth2\"\n)\n\nfunc main() {\n privateKey := []byte(os.Getenv(\"GITHUB_APP_PRIVATE_KEY\"))\n appID, _ := strconv.ParseInt(os.Getenv(\"GITHUB_APP_ID\"), 10, 64)\n installationID, _ := strconv.ParseInt(os.Getenv(\"GITHUB_INSTALLATION_ID\"), 10, 64)\n\n appTokenSource, err := githubauth.NewApplicationTokenSource(appID, privateKey)\n if err != nil {\n  fmt.Println(\"Error creating application token source:\", err)\n  return\n }\n\n installationTokenSource := githubauth.NewInstallationTokenSource(installationID, appTokenSource)\n\n // oauth2.NewClient create a new http.Client that adds an Authorization header with the token.\n // Transport src use oauth2.ReuseTokenSource to reuse the token.\n // The token will be reused until it expires.\n // The token will be refreshed if it's expired.\n httpClient := oauth2.NewClient(context.Background(), installationTokenSource)\n\n githubClient := github.NewClient(httpClient)\n\n _, _, err = githubClient.PullRequests.CreateComment(context.Background(), \"owner\", \"repo\", 1, \u0026github.PullRequestComment{\n  Body: github.String(\"Awesome comment!\"),\n })\n if err != nil {\n  fmt.Println(\"Error creating comment:\", err)\n  return\n }\n}\n```\n\n### Generate GitHub Application Token\n\nFirst of all you need to create a GitHub App and generate a private key.\n\nTo authenticate as a GitHub App, you need to generate a 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)\n\n```go\npackage main\n\nimport (\n \"fmt\"\n \"os\"\n \"time\"\n\n \"github.com/jferrl/go-githubauth\"\n)\n\nfunc main() {\n privateKey := []byte(os.Getenv(\"GITHUB_APP_PRIVATE_KEY\"))\n appID, _ := strconv.ParseInt(os.Getenv(\"GITHUB_APP_ID\"), 10, 64)\n\n tokenSource, err := githubauth.NewApplicationTokenSource(appID, privateKey, githubauth.WithApplicationTokenExpiration(5*time.Minute))\n if err != nil {\n  fmt.Println(\"Error creating token source:\", err)\n  return\n }\n\n token, err := tokenSource.Token()\n if err != nil {\n  fmt.Println(\"Error generating token:\", err)\n  return\n }\n\n fmt.Println(\"Generated token:\", token.AccessToken)\n}\n```\n\n### Generate GitHub App Installation Token\n\nTo authenticate as a GitHub App installation, you need to obtain an installation token.\n\n```go\npackage main\n\nimport (\n \"fmt\"\n \"os\"\n \"strconv\"\n\n \"github.com/jferrl/go-githubauth\"\n)\n\nfunc main() {\n privateKey := []byte(os.Getenv(\"GITHUB_APP_PRIVATE_KEY\"))\n appID, _ := strconv.ParseInt(os.Getenv(\"GITHUB_APP_ID\"), 10, 64)\n installationID, _ := strconv.ParseInt(os.Getenv(\"GITHUB_INSTALLATION_ID\"), 10, 64)\n\n appTokenSource, err := githubauth.NewApplicationTokenSource(appID, privateKey)\n if err != nil {\n  fmt.Println(\"Error creating application token source:\", err)\n  return\n }\n\n installationTokenSource := githubauth.NewInstallationTokenSource(installationID, appTokenSource)\n\n token, err := installationTokenSource.Token()\n if err != nil {\n  fmt.Println(\"Error generating installation token:\", err)\n  return\n }\n\n fmt.Println(\"Generated installation token:\", token.AccessToken)\n}\n```\n\n## Contributing\n\nContributions are welcome! Please open an issue or submit a pull request on GitHub.\n\n## License\n\nThis project is licensed under the MIT License. See the LICENSE file for details.\n","funding_links":[],"categories":["Authentication and Authorization"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjferrl%2Fgo-githubauth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjferrl%2Fgo-githubauth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjferrl%2Fgo-githubauth/lists"}