go-auth
is a Go package that provides utilities for authentication, including generating and using App tokens and installation tokens.
go-auth
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.
- Generate Application JWT Generating a jwt for a app
- Obtain App installation tokens Authenticating as a App
This package is designed to be used with the golang.org/x/oauth2
package, which provides support for OAuth2 authentication.
To use go-auth
in your project, you need to have Go installed. You can get the package via:
go get -u .com/jferrl/go-auth
package main
import (
"context"
"fmt"
"os"
"strconv"
".com/google/go-/v62/"
".com/jferrl/go-auth"
"golang.org/x/oauth2"
)
func main() {
privateKey := []byte(os.Getenv("_APP_PRIVATE_KEY"))
appID, _ := strconv.ParseInt(os.Getenv("_APP_ID"), 10, 64)
installationID, _ := strconv.ParseInt(os.Getenv("_INSTALLATION_ID"), 10, 64)
appTokenSource, err := auth.NewApplicationTokenSource(appID, privateKey)
if err != nil {
fmt.Println("Error creating application token source:", err)
return
}
installationTokenSource := auth.NewInstallationTokenSource(installationID, appTokenSource)
// oauth2.NewClient create a new http.Client that adds an Authorization header with the token.
// Transport src use oauth2.ReuseTokenSource to reuse the token.
// The token will be reused until it expires.
// The token will be refreshed if it's expired.
httpClient := oauth2.NewClient(context.Background(), installationTokenSource)
Client := .NewClient(httpClient)
_, _, err = Client.PullRequests.CreateComment(context.Background(), "owner", "repo", 1, &.PullRequestComment{
Body: .String("Awesome comment!"),
})
if err != nil {
fmt.Println("Error creating comment:", err)
return
}
}
First of all you need to create a App and generate a private key.
To authenticate as a App, you need to generate a JWT. Generating a jwt for a app
package main
import (
"fmt"
"os"
"time"
".com/jferrl/go-auth"
)
func main() {
privateKey := []byte(os.Getenv("_APP_PRIVATE_KEY"))
appID, _ := strconv.ParseInt(os.Getenv("_APP_ID"), 10, 64)
tokenSource, err := auth.NewApplicationTokenSource(appID, privateKey, auth.WithApplicationTokenExpiration(5*time.Minute))
if err != nil {
fmt.Println("Error creating token source:", err)
return
}
token, err := tokenSource.Token()
if err != nil {
fmt.Println("Error generating token:", err)
return
}
fmt.Println("Generated token:", token.AccessToken)
}
To authenticate as a App installation, you need to obtain an installation token.
package main
import (
"fmt"
"os"
"strconv"
".com/jferrl/go-auth"
)
func main() {
privateKey := []byte(os.Getenv("_APP_PRIVATE_KEY"))
appID, _ := strconv.ParseInt(os.Getenv("_APP_ID"), 10, 64)
installationID, _ := strconv.ParseInt(os.Getenv("_INSTALLATION_ID"), 10, 64)
appTokenSource, err := auth.NewApplicationTokenSource(appID, privateKey)
if err != nil {
fmt.Println("Error creating application token source:", err)
return
}
installationTokenSource := auth.NewInstallationTokenSource(installationID, appTokenSource)
token, err := installationTokenSource.Token()
if err != nil {
fmt.Println("Error generating installation token:", err)
return
}
fmt.Println("Generated installation token:", token.AccessToken)
}
Contributions are welcome! Please open an issue or submit a pull request on .
This project is licensed under the MIT License. See the LICENSE file for details.