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/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 ¶
- Variables
- func IsTimeoutErr(err error) bool
- type Config
- func (c *Config) Do(ctx context.Context, req *http.Request, v interface{}) (*atlas.Response, error)
- func (c *Config) GetToken(ctx context.Context, deviceCode string) (*Token, *atlas.Response, error)
- func (c *Config) NewRequest(ctx context.Context, method, urlStr string, v url.Values) (*http.Request, error)
- func (c *Config) PollToken(ctx context.Context, code *DeviceCode) (*Token, *atlas.Response, error)
- func (c *Config) RefreshToken(ctx context.Context, token string) (*Token, *atlas.Response, error)
- func (c *Config) RegistrationConfig(ctx context.Context) (*RegistrationConfig, *atlas.Response, error)
- func (c *Config) RequestCode(ctx context.Context) (*DeviceCode, *atlas.Response, error)
- func (c *Config) RevokeToken(ctx context.Context, token, tokenTypeHint string) (*atlas.Response, error)
- type ConfigOpt
- type DeviceCode
- type RegistrationConfig
- type Token
- type TokenSource
Constants ¶
This section is empty.
Variables ¶
var ErrTimeout = errors.New("authentication timed out")
ErrTimeout is returned when polling the server for the granted token has timed out.
Functions ¶
func IsTimeoutErr ¶ added in v0.16.0
IsTimeoutErr checks if the given error is for the case where the device flow has expired.
Types ¶
type Config ¶
type Config struct { ClientID string AuthURL *url.URL UserAgent string Scopes []string // contains filtered or unexported fields }
func NewConfigWithOptions ¶
func (*Config) NewRequest ¶
func (*Config) RefreshToken ¶
RefreshToken takes a refresh token and gets a new access token.
func (*Config) RegistrationConfig ¶ added in v0.17.0
func (c *Config) RegistrationConfig(ctx context.Context) (*RegistrationConfig, *atlas.Response, error)
RegistrationConfig retrieves the config used for registration.
func (*Config) RequestCode ¶
RequestCode initiates the authorization flow by requesting a code.
type ConfigOpt ¶
func SetAuthURL ¶
SetAuthURL is a config option for setting the base URL.
func SetClientID ¶
SetClientID is a config option for setting the ClientID.
func SetUserAgent ¶
SetUserAgent is a config option for setting the user agent.
func SetWithRaw ¶ added in v0.17.0
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 ¶ added in v0.17.0
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 ¶
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.