Documentation
¶
Overview ¶
Package oauth is a library for Go client applications that need to perform OAuth authorization against a server, typically GitHub.com.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRefreshTokenInvalid = errors.New("refresh token is invalid or expired")
ErrRefreshTokenInvalid is returned when the server rejects a refresh token because it is invalid, expired, or already used.
Functions ¶
func Refresh ¶ added in v1.3.0
func Refresh(opts RefreshOptions) (*api.AccessToken, error)
Refresh exchanges a refresh token for a new access token and refresh token. If the server omits a refresh token from the response, the returned token carries the refresh token from opts, since RFC 6749 section 6 allows the server to keep the previous one valid. Applications that require rotation must enforce it themselves.
Example ¶
package main
import (
"fmt"
"net/http"
"os"
"github.com/cli/oauth"
)
func main() {
host, err := oauth.NewGitHubHost("https://github.com")
if err != nil {
panic(err)
}
flow := &oauth.Flow{
Host: host,
ClientID: os.Getenv("OAUTH_CLIENT_ID"),
ClientSecret: os.Getenv("OAUTH_CLIENT_SECRET"), // only applicable to web app flow
Scopes: []string{"repo", "read:org", "gist"},
RequestRefreshToken: true,
}
accessToken, err := flow.DeviceFlow()
if err != nil {
panic(err)
}
refreshedToken, err := oauth.Refresh(oauth.RefreshOptions{
Host: host,
ClientID: flow.ClientID,
ClientSecret: flow.ClientSecret, // only applicable to web app flow
RefreshToken: accessToken.RefreshToken,
HTTPClient: http.DefaultClient,
})
if err != nil {
panic(err)
}
// Persist the complete refreshed token because the previous token pair is no longer usable.
fmt.Printf("Refreshed refresh token: %s\n", refreshedToken.RefreshToken)
fmt.Printf("Refreshed access token: %s\n", refreshedToken.Token)
}
Output:
Types ¶
type Flow ¶
type Flow struct {
// The hostname to authorize the app with.
//
// Deprecated: Use Host instead.
Hostname string
// Host configuration to authorize the app with.
Host *Host
// OAuth scopes to request from the user.
Scopes []string
// OAuth audience to request from the user.
Audience string
// OAuth application ID.
ClientID string
// OAuth application secret. Only applicable in web application flow.
ClientSecret string
// The localhost URI for web application flow callback, e.g. "http://127.0.0.1/callback".
CallbackURI string
// RequestRefreshToken opts this authorization into receiving an expiring access token and a
// refresh token by requesting the "offline_access" scope.
//
// Servers that do not support expiring tokens may ignore this request and return a token that
// does not expire and has no refresh token.
RequestRefreshToken bool
// Display a one-time code to the user. Receives the code and the browser URL as arguments. Defaults to printing the
// code to the user on Stdout with instructions to copy the code and to press Enter to continue in their browser.
DisplayCode func(string, string) error
// Open a web browser at a URL. Defaults to opening the default system browser.
BrowseURL func(string) error
// Render an HTML page to the user upon completion of web application flow. The default is to
// render a simple message that informs the user they can close the browser tab and return to the app.
WriteSuccessHTML func(io.Writer)
// The HTTP client to use for API POST requests. Defaults to http.DefaultClient.
HTTPClient httpClient
// The stream to listen to keyboard input on. Defaults to os.Stdin.
Stdin io.Reader
// The stream to print UI messages to. Defaults to os.Stdout.
Stdout io.Writer
}
Flow facilitates a single OAuth authorization flow.
func (*Flow) DetectFlow ¶
func (oa *Flow) DetectFlow() (*api.AccessToken, error)
DetectFlow tries to perform Device flow first and falls back to Web application flow.
Example ¶
DetectFlow attempts to initiate OAuth Device flow with the server and falls back to OAuth Web application flow if Device flow seems unsupported. This approach isn't strictly needed for github.com, as its Device flow support is globally available, but it enables logging in to self-hosted GitHub instances as well.
package main
import (
"fmt"
"os"
"github.com/cli/oauth"
)
func main() {
host, err := oauth.NewGitHubHost("https://github.com")
if err != nil {
panic(err)
}
flow := &oauth.Flow{
Host: host,
ClientID: os.Getenv("OAUTH_CLIENT_ID"),
ClientSecret: os.Getenv("OAUTH_CLIENT_SECRET"), // only applicable to web app flow
CallbackURI: "http://127.0.0.1/callback", // only applicable to web app flow
Scopes: []string{"repo", "read:org", "gist"},
}
accessToken, err := flow.DetectFlow()
if err != nil {
panic(err)
}
fmt.Printf("Access token: %s\n", accessToken.Token)
}
Output:
func (*Flow) DeviceFlow ¶
func (oa *Flow) DeviceFlow() (*api.AccessToken, error)
DeviceFlow captures the full OAuth Device flow, including prompting the user to copy a one-time code and opening their web browser, and returns an access token upon completion.
func (*Flow) WebAppFlow ¶
func (oa *Flow) WebAppFlow() (*api.AccessToken, error)
WebAppFlow starts a local HTTP server, opens the web browser to initiate the OAuth Web application flow, blocks until the user completes authorization and is redirected back, and returns the access token.
type Host ¶ added in v0.9.0
Host defines the endpoints used to authorize against an OAuth server.
func GitHubHost
deprecated
added in
v0.9.0
func NewGitHubHost ¶ added in v1.1.0
NewGitHubHost constructs a Host from the given URL to a GitHub instance.
type RefreshOptions ¶ added in v1.3.0
type RefreshOptions struct {
// Host contains the token endpoint used for the refresh request.
Host *Host
// ClientID is the OAuth application ID.
ClientID string
// ClientSecret is the OAuth application secret. It is not required for device flow tokens.
ClientSecret string
// RefreshToken is the refresh token issued with the current access token.
RefreshToken string
// HTTPClient is the client used for the refresh request. It defaults to http.DefaultClient.
HTTPClient httpClient
}
RefreshOptions specifies parameters for exchanging a refresh token for a new token pair.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package api implements request and response parsing logic shared between different OAuth strategies.
|
Package api implements request and response parsing logic shared between different OAuth strategies. |
|
Package device facilitates performing OAuth Device Authorization Flow for client applications such as CLIs that can not receive redirects from a web site.
|
Package device facilitates performing OAuth Device Authorization Flow for client applications such as CLIs that can not receive redirects from a web site. |
|
Package webapp implements the OAuth Web Application authorization flow for client applications by starting a server at localhost to receive the web redirect after the user has authorized the application.
|
Package webapp implements the OAuth Web Application authorization flow for client applications by starting a server at localhost to receive the web redirect after the user has authorized the application. |
