cliauthorizationflow

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2020 License: MIT Imports: 13 Imported by: 0

README

Authorization Flow for Go CLI

This package makes it easier to perform the OAuth2 Authorization Flow from a command-line application.

This is a beta/prototype level package and shouldn't be used in production without understanding what this code does.

Example

This example uses the Spotify API

package main

import (
    "context"
    "io/ioutil"
    "log"
    "os"

    auth "github.com/cleverswine/cliauthorizationflow"
)

const (
    SpotifyAuthURL  = "https://accounts.spotify.com/authorize"
    SpotifyTokenURL = "https://accounts.spotify.com/api/token"
    SpotifyAPIBase  = "https://api.spotify.com/v1"
)

func main() {
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    config := &auth.Config{
        ClientID:         os.Getenv("SPOTIFY_ID"),
        ClientSecret:     os.Getenv("SPOTIFY_SECRET"),
        AuthorizationURL: SpotifyAuthURL,
        TokenURL:         SpotifyTokenURL,
        Scopes:           []string{"user-top-read"},
    }

    client, err := auth.NewClient(ctx, config, nil)
    if err != nil {
        log.Fatal(err)
    }

    // get my top tracks
    resp, err := client.Get(SpotifyAPIBase + "/me/top/tracks")
    if err != nil {
        log.Fatal(err)
    }

    // do stuff with the resp...
}

Token storage

You'll want to implement the TokenStorage interface and pass that to the NewClient(...) method in order to persist tokens. Otherwise the user will have to open a web browser and authenticate every time.

Be very careful with storing tokens if you plan to authorize multiple users.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*http.Client
	// contains filtered or unexported fields
}

Client contains an authorized http client. That is, the auth token is automatically set on all request headers.

func NewClient

func NewClient(ctx context.Context, config *Config, tokenStorage TokenStorage) (*Client, error)

NewClient creates a new client with the specified authorization parameters

func (*Client) Persist

func (a *Client) Persist()

Persist will save the current token to storage

type Config

type Config struct {
	// ClientID is the application's ID.
	ClientID string
	// ClientSecret is the application's secret.
	ClientSecret string
	// AuthorizationURL contains the resource server's authorize endpoint URL
	AuthorizationURL string
	// TokenURL contains the resource server's token endpoint URL
	TokenURL string
	// Scope specifies optional requested permissions.
	Scopes []string
	// CallbackPort specifies which local port to use for the auth callback (default: 8080)
	CallbackPort int
}

Config describes a typical 3-legged OAuth2 flow, with both the client application information and the server's endpoint URLs.

type DefaultTokenStorage added in v0.1.6

type DefaultTokenStorage struct {
	// contains filtered or unexported fields
}

DefaultTokenStorage stores tokens in a user's Home directory

func NewDefaultTokenStorage added in v0.1.6

func NewDefaultTokenStorage(appName string) *DefaultTokenStorage

NewDefaultTokenStorage returns a configured DefaultTokenStorage with a storage path of ~/.config/{appName}

func (*DefaultTokenStorage) Get added in v0.1.6

func (t *DefaultTokenStorage) Get(key string) (*oauth2.Token, error)

Get gets a token from the filesystem

func (*DefaultTokenStorage) Save added in v0.1.6

func (t *DefaultTokenStorage) Save(key string, token *oauth2.Token) error

Save saves a token to the filesystem

type TokenStorage

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

TokenStorage is used to cache and retrieve tokens

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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