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.
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) }
type TokenStore ¶ added in v2.6.2
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.