webapp

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2021 License: MIT Imports: 10 Imported by: 0

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

type CodeResponse struct {
	Code  string
	State string
}

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 InitFlow

func InitFlow() (*Flow, error)

InitFlow creates a new Flow instance by detecting a locally available port number.

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.

func (*Flow) StartServer

func (flow *Flow) StartServer(writeSuccess func(io.Writer)) error

StartServer starts the localhost server and blocks until it has received the web redirect. The writeSuccess function can be used to render a HTML page to the user upon completion.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL