Documentation ¶
Overview ¶
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.
Example ¶
This demonstrates how to perform OAuth App Authorization Flow for GitHub.com. Ensure that the OAuth app on GitHub lists the callback URL: "http://127.0.0.1/callback"
clientID := os.Getenv("OAUTH_CLIENT_ID") clientSecret := os.Getenv("OAUTH_CLIENT_SECRET") flow, err := InitFlow() if err != nil { panic(err) } params := BrowserParams{ ClientID: clientID, RedirectURI: "http://127.0.0.1/callback", Scopes: []string{"repo", "read:org"}, AllowSignup: true, } browserURL, err := flow.BrowserURL("https://github.com/login/oauth/authorize", params) if err != nil { panic(err) } // A localhost server on a random available port will receive the web redirect. go func() { _ = flow.StartServer(nil) }() // Note: the user's web browser must run on the same device as the running app. err = browser.OpenURL(browserURL) if err != nil { panic(err) } httpClient := http.DefaultClient accessToken, err := flow.AccessToken(httpClient, "https://github.com/login/oauth/access_token", clientSecret) if err != nil { panic(err) } fmt.Printf("Access token: %s\n", accessToken.Token)
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BrowserParams ¶
type BrowserParams struct { ClientID string RedirectURI string Scopes []string LoginHandle string AllowSignup bool }
BrowserParams are GET query parameters for initiating the web flow.
type CodeResponse ¶
CodeResponse represents the code received by the local server's callback handler.
type Flow ¶
type Flow struct {
// contains filtered or unexported fields
}
Flow holds the state for the steps of OAuth Web Application flow.
func (*Flow) AccessToken ¶
func (flow *Flow) AccessToken(c httpClient, tokenURL, clientSecret string) (*api.AccessToken, error)
AccessToken blocks until the browser flow has completed and returns the access token.
func (*Flow) BrowserURL ¶
func (flow *Flow) BrowserURL(baseURL string, params BrowserParams) (string, error)
BrowserURL appends GET query parameters to baseURL and returns the url that the user should navigate to in their web browser.