oauth2

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package oauth2 implements helper functions for implementing OAuth2 Applications for provider.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHTTPClient

func GetHTTPClient(ctx context.Context, credentialsLoaders []config.CredentialsLoader) (*http.Client, error)

GetHTTPClient returns an authenticated HTTP client that always injects a valid token.

Example (Default)

This example demonstrates how to create an authenticated http client and use it call the Knowledge API.

credentialsLoaders = append(credentialsLoaders, apicfg.DefaultEnvironmentLoader)
client, err := oauth2.GetHTTPClient(context.Background(), credentialsLoaders)
if err != nil {
	log.Fatalf("failed to generate token source %v", err)
}

resp, err := client.Get("indykite.com")
if err != nil {
	log.Fatalf("unable to fetch url")
}
fmt.Println(resp.Body)
Output:

func GetRefreshableTokenSource

func GetRefreshableTokenSource(
	ctx context.Context,
	credentialsLoaders []config.CredentialsLoader,
) (oauth2.TokenSource, error)

GetRefreshableTokenSource Token returns a token or an error. Token must be safe for concurrent use by multiple goroutines. The returned Token must not be modified.

Example (Default)

This example demonstrates how to generate a token.

credentialsLoaders = append(credentialsLoaders, apicfg.DefaultEnvironmentLoader)
tokenSource, err := oauth2.GetRefreshableTokenSource(context.Background(), credentialsLoaders)
if err != nil {
	log.Fatalf("failed to generate token source %v", err)
}
token, err := tokenSource.Token()
if err != nil {
	log.Fatalf("failed to get token from tokenSource %v", err)
}
if token.Type() != "Bearer" {
	log.Fatalf("unsupported token type, must be 'Bearer' but got %s", token.Type())
}

fmt.Println(token.AccessToken)
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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