Documentation
¶
Overview ¶
Package oauth2cli provides better user experience on OAuth 2.0 and OpenID Connect (OIDC) on CLI. It allows simple and easy user interaction with Authorization Code Grant Flow and a local server.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthCodeFlow ¶
type AuthCodeFlow struct {
Config oauth2.Config // OAuth2 config.
AuthCodeOptions []oauth2.AuthCodeOption // OAuth2 options.
LocalServerPort int // Local server port. Default to a random port.
SkipOpenBrowser bool // If set, skip opening browser.
// Called when the local server is started. Default to none.
ShowLocalServerURL func(url string)
// Middleware for the local server. Default to none.
LocalServerMiddleware func(h http.Handler) http.Handler
}
AuthCodeFlow provides the flow with OAuth 2.0 Authorization Code Grant. See https://tools.ietf.org/html/rfc6749#section-4.1
Example ¶
package main
import (
"context"
"log"
"github.com/int128/oauth2cli"
"golang.org/x/oauth2"
)
var endpoint = oauth2.Endpoint{
AuthURL: "https://example.com/oauth2/auth",
TokenURL: "https://example.com/oauth2/token",
}
func main() {
ctx := context.Background()
flow := oauth2cli.AuthCodeFlow{
Config: oauth2.Config{
ClientID: "YOUR_CLIENT_ID",
ClientSecret: "YOUR_CLIENT_SECRET",
Endpoint: endpoint,
Scopes: []string{"email"},
},
}
token, err := flow.GetToken(ctx)
if err != nil {
log.Fatalf("Could not get a token: %s", err)
}
log.Printf("Got a token: %+v", token)
}
func (*AuthCodeFlow) GetToken ¶
GetToken performs the Authorization Grant Flow and returns a token got from the provider.
This does the following steps:
- Start a local server at the port.
- Open a browser and navigate it to the local server.
- Wait for the user authorization.
- Receive a code via an authorization response (HTTP redirect).
- Exchange the code and a token.
- Return the code.
Note that this will change Config.RedirectURL to "http://localhost:port" if it is empty.
Click to show internal directories.
Click to hide internal directories.