oauth2store

package
v2.6.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EncryptedFileTokenStore added in v2.6.2

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

An EncryptedFileTokenStore stores an encrypted oauth2.Token to a file.

func NewEncryptedFileTokenStore added in v2.6.2

func NewEncryptedFileTokenStore(path, passphrase string, expiration time.Duration) *EncryptedFileTokenStore

NewEncryptedFileTokenStore returns an EncryptedFileTokenStore that saved a token to path, using passphrase to generate the encryption key. Tokens written older than the expiration date are considered expired and are not loaded.

func (EncryptedFileTokenStore) Load added in v2.6.2

Load returns a stored token. If the token is too old (as specified by the expiration parameter), an error is returned.

func (EncryptedFileTokenStore) Save added in v2.6.2

func (e EncryptedFileTokenStore) Save(token *oauth2.Token) error

Save stores a token to disk.

type TokenSource added in v2.6.2

type TokenSource struct {
	oauth2.TokenSource
	TokenStore
	// contains filtered or unexported fields
}

TokenSource is an oauth2.TokenSource that saves the token whenever the underlying TokenSource returns a new token.

The main use case is to persist tokens that require manual action to create (e.g. device authentication flow), so that a previously created token may be reused if the application is restarted.

Example
package main

import (
	"context"
	"fmt"
	"github.com/clambin/tado/v2/oauth2store"
	"golang.org/x/oauth2"
	"time"
)

func main() {
	var cfg oauth2.Config // application-specific oauth2 configuration
	ctx := context.Background()

	// create a store to save the token
	store := oauth2store.NewEncryptedFileTokenStore("token.enc", "my-very-secret-passphrase", 24*time.Hour)
	token, err := store.Load()
	if err != nil {
		// store does not contain a valid token. let's create one ...
		var devAuthResponse *oauth2.DeviceAuthResponse
		if devAuthResponse, err = cfg.DeviceAuth(ctx); err != nil {
			panic(err)
		}
		fmt.Println("Confirm login: ", devAuthResponse.VerificationURIComplete)
		if token, err = cfg.DeviceAccessToken(ctx, devAuthResponse); err != nil {
			panic(err)
		}
	}

	// now that we have a token, create a TokenSource that saves the token to our store whenever it changes:
	pts := oauth2store.TokenSource{
		TokenSource: cfg.TokenSource(ctx, token),
		TokenStore:  store,
	}
	_ = oauth2.NewClient(ctx, &pts)
}

func (*TokenSource) Token added in v2.6.2

func (ts *TokenSource) Token() (token *oauth2.Token, err error)

Token implements interface oauth2.TokenSource. It gets a token from the underlying TokenSource and, if the token is new, stores the token in its TokenStore.

type TokenStore added in v2.6.2

type TokenStore interface {
	Save(token *oauth2.Token) error
	Load() (*oauth2.Token, error)
}

The TokenStore interface saves and loads oauth2 Tokens. In conjunction with this package's TokenSource, it allows calling applications to persist tokens and reuse them between runs.

The main use case is to persist tokens that require manual action to create (e.g. device authentication flow), so that a previously created token may be reused if the application is restarted.

Jump to

Keyboard shortcuts

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