oauthflows

package module
v0.0.0-...-a3a7c00 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 18 Imported by: 5

README

OAuth2 Browser Flows for CLIs

Obtain OAuth2 tokens by sending users through the OAuth2 flow in a local webserver.

scopes := []string{"openid", "profile"}
client, err := oauthflows.NewClient(oauthflows.WithClientSecretsFile(clientSecretsPath, scopes), oauthflows.WithFileTokenStore())
if err != nil {
log.Fatal(err)
}

res, err := client.Get("https://www.googleapis.com/oauth2/v1/userinfo?alt=json")
defer res.Body.Close()
if err != nil {
log.Fatal(err)
}

data, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}

fmt.Println(string(data))

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrMissingScopes = errors.New("missing scopes")

Functions

func ExecuteOAuth2BrowserTokenFlow

func ExecuteOAuth2BrowserTokenFlow(ctx context.Context, conf *oauth2.Config) (*oauth2.Token, error)

func NewBrowserFlowTokenSource

func NewBrowserFlowTokenSource(ctx context.Context, conf *oauth2.Config) (oauth2.TokenSource, error)

func NewCachedTokenSource

func NewCachedTokenSource(ctx context.Context, cacheName string, store TokenStore, source oauth2.TokenSource) (oauth2.TokenSource, error)

func NewClient

func NewClient(configer ...ClientOption) (*http.Client, error)
Example
scopes := []string{"openid", "profile"}
client, err := NewClient(WithClientSecretsFile(clientSecretsPath, scopes), WithFileTokenStore())
if err != nil {
	log.Fatal(err)
}

res, err := client.Get("https://www.googleapis.com/oauth2/v1/userinfo?alt=json")
defer res.Body.Close()
if err != nil {
	log.Fatal(err)
}

data, err := io.ReadAll(res.Body)
if err != nil {
	log.Fatal(err)
}

fmt.Println(string(data))
Output:

Types

type ClientOption

type ClientOption func(c *config) error

func FailOnMissingScopes

func FailOnMissingScopes(doFail bool) ClientOption

func WithClientSecretsFile

func WithClientSecretsFile(filepath string, scopes []string) ClientOption

func WithConfig

func WithConfig(conf *oauth2.Config) ClientOption

func WithFileTokenStore

func WithFileTokenStore() ClientOption

func WithTokenStore

func WithTokenStore(t TokenStore) ClientOption

type TokenStore

type TokenStore interface {
	Get(key string) (*oauth2.Token, error)
	Put(key string, token *oauth2.Token) error
}

Jump to

Keyboard shortcuts

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