auth

package
v20230201004.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package auth provides a way to follow a Device Authorization Grant https://datatracker.ietf.org/doc/html/rfc8628.

Usage

import "go.mongodb.org/atlas-sdk/auth"

Construct a new client Config, then use the various methods to complete a flow. For example:

config := auth.NewConfigWithOptions(nil, auth.SetClientID("my-client-id"), auth.SetScopes([]string{"openid"}))

code, _, err := config.RequestCode(ctx)
if err!= nil {
	panic(err)
}
token, _, err := config.PollToken(ctx, code)
if err!= nil {
	panic(err)
}
fmt.PrintLn(accessToken.AccessToken)

NOTE: Using the https://godoc.org/context package, one can easily pass cancellation signals and deadlines to various services of the client for handling a request. In case there is no context available, then context.Background() can be used as a starting point.

Index

Constants

This section is empty.

Variables

View Source
var ErrTimeout = errors.New("authentication timed out")

ErrTimeout is returned when polling the server for the granted token has timed out.

Functions

func IsTimeoutErr

func IsTimeoutErr(err error) bool

Types

type Config

type Config struct {
	ClientID  string
	AuthURL   *url.URL
	UserAgent string
	Scopes    []string
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig(httpClient *http.Client) *Config

func NewConfigWithOptions

func NewConfigWithOptions(httpClient *http.Client, opts ...ConfigOpt) (*Config, error)

func (*Config) Do

func (c *Config) Do(ctx context.Context, req *http.Request, v interface{}) (*core.Response, error)

func (Config) GetToken

func (c Config) GetToken(ctx context.Context, deviceCode string) (*Token, *core.Response, error)

GetToken gets a device token.

func (*Config) NewRequest

func (c *Config) NewRequest(ctx context.Context, method, urlStr string, v url.Values) (*http.Request, error)

func (Config) PollToken

func (c Config) PollToken(ctx context.Context, code *DeviceCode) (*Token, *core.Response, error)

PollToken polls the server until an access token is granted or denied.

func (Config) RefreshToken

func (c Config) RefreshToken(ctx context.Context, token string) (*Token, *core.Response, error)

RefreshToken takes a refresh token and gets a new access token.

func (Config) RegistrationConfig

func (c Config) RegistrationConfig(ctx context.Context) (*RegistrationConfig, *core.Response, error)

RegistrationConfig retrieves the config used for registration.

func (Config) RequestCode

func (c Config) RequestCode(ctx context.Context) (*DeviceCode, *core.Response, error)

RequestCode initiates the authorization flow by requesting a code.

func (Config) RevokeToken

func (c Config) RevokeToken(ctx context.Context, token, tokenTypeHint string) (*core.Response, error)

RevokeToken takes an access or refresh token and revokes it.

type ConfigOpt

type ConfigOpt func(*Config) error

func SetAuthURL

func SetAuthURL(bu string) ConfigOpt

SetAuthURL is a config option for setting the base URL.

func SetClientID

func SetClientID(clientID string) ConfigOpt

SetClientID is a config option for setting the ClientID.

func SetScopes

func SetScopes(scopes []string) ConfigOpt

SetScopes is a config option for setting the Scopes.

func SetUserAgent

func SetUserAgent(ua string) ConfigOpt

SetUserAgent is a config option for setting the user agent.

func SetWithRaw

func SetWithRaw() ConfigOpt

SetWithRaw is a client option for getting raw atlas server response within Response structure.

type DeviceCode

type DeviceCode struct {
	UserCode        string `json:"user_code"`        // UserCode is the code presented to users
	VerificationURI string `json:"verification_uri"` // VerificationURI is the URI where users will need to confirm the code
	DeviceCode      string `json:"device_code"`      // DeviceCode is the internal code to confirm the status of the flow
	ExpiresIn       int    `json:"expires_in"`       // ExpiresIn when the code will expire
	Interval        int    `json:"interval"`         // Interval how often to verify the status of the code
	// contains filtered or unexported fields
}

DeviceCode holds information about the authorization-in-progress.

type RegistrationConfig

type RegistrationConfig struct {
	RegistrationURL string `json:"registrationUrl"`
}

type Token

type Token struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	Scope        string `json:"scope"`
	IDToken      string `json:"id_token"`
	TokenType    string `json:"token_type"`
	ExpiresIn    int    `json:"expires_in"`
	Expiry       time.Time
}

func (*Token) SetAuthHeader

func (t *Token) SetAuthHeader(r *http.Request)

func (*Token) Valid

func (t *Token) Valid() bool

type TokenSource

type TokenSource interface {
	// Token returns a token or an error.
	// Token must be safe for concurrent use by multiple goroutines.
	// The returned Token must not be modified.
	Token() (*Token, error)
}

A TokenSource is anything that can return a token.

Jump to

Keyboard shortcuts

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