Documentation ¶
Overview ¶
Package oauth provides authentication context support for APIs that require OAuth 2.0 auth.
Index ¶
- Variables
- func Extra(names ...string) func(*config) error
- func GetParams(f func(context map[string]string) url.Values) func(*config) error
- func InitAuthCode(clientID string, authorizeURL string, tokenURL string, ...)
- func InitClientCredentials(tokenURL string, options ...func(*config) error)
- func Scopes(scopes ...string) func(*config) error
- func TokenHandler(source oauth2.TokenSource, log *zerolog.Logger, request *http.Request) error
- func TokenMiddleware(source oauth2.TokenSource, ctx *context.Context, h context.Handler)
- type AuthCodeHandler
- type AuthorizationCodeTokenSource
- type ClientCredentialsHandler
- type RefreshTokenSource
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidContext = errors.New("invalid context")
ErrInvalidContext is thrown when a context is missing or invalid.
Functions ¶
func Extra ¶
Extra provides the names of additional parameters to use to store information in user contexts. Use `cli.GetContext("default")["name"]` to access it.
func GetParams ¶
GetParams registers a function to get additional token endpoint parameters to include in the request when fetching a new token.
func InitAuthCode ¶
func InitAuthCode(clientID string, authorizeURL string, tokenURL string, options ...func(*config) error)
InitAuthCode sets up the OAuth 2.0 authorization code with PKCE authentication flow. Must be called *after* you have called `cli.Init()`. The endpoint params allow you to pass additional info to the token URL. Pass in context-related extra variables to store them alongside the default context information.
func InitClientCredentials ¶
InitClientCredentials sets up the OAuth 2.0 client credentials authentication flow. Must be called *after* you have called `cli.Init()`. The endpoint params allow you to pass additional info to the token URL. Pass in context-related extra variables to store them alongside the default context information.
func TokenHandler ¶
TokenHandler takes a token source, gets a token, and modifies a request to add the token auth as a header. Uses the CLI cache to store tokens on a per- context basis between runs.
func TokenMiddleware ¶
TokenMiddleware is a wrapper around TokenHandler.
Types ¶
type AuthCodeHandler ¶
type AuthCodeHandler struct { ClientID string AuthorizeURL string TokenURL string Keys []string Params []string Scopes []string // contains filtered or unexported fields }
AuthCodeHandler sets up the OAuth 2.0 authorization code with PKCE authentication flow.
func (*AuthCodeHandler) ContextKeys ¶
func (h *AuthCodeHandler) ContextKeys() []string
ContextKeys returns the key names for fields to store in the context.
type AuthorizationCodeTokenSource ¶
type AuthorizationCodeTokenSource struct { ClientID string AuthorizeURL string TokenURL string EndpointParams *url.Values Scopes []string }
AuthorizationCodeTokenSource with PKCE as described in: https://www.oauth.com/oauth2-servers/pkce/ This works by running a local HTTP server on port 8484 and then having the user log in through a web browser, which redirects to the local server with an authorization code. That code is then used to make another HTTP request to fetch an auth token (and refresh token). That token is then in turn used to make requests against the API.
type ClientCredentialsHandler ¶
type ClientCredentialsHandler struct { TokenURL string Keys []string Params []string Scopes []string // contains filtered or unexported fields }
ClientCredentialsHandler implements the Client Credentials OAuth2 flow.
func NewClientCredentialsHandler ¶
func NewClientCredentialsHandler(tokenURL string, keys, params, scopes []string) *ClientCredentialsHandler
NewClientCredentialsHandler creates a new handler.
func (*ClientCredentialsHandler) ContextKeys ¶
func (h *ClientCredentialsHandler) ContextKeys() []string
ContextKeys returns the key names for fields to store in the context.
type RefreshTokenSource ¶
type RefreshTokenSource struct { // ClientID of the application ClientID string // TokenURL is used to fetch new tokens TokenURL string // EndpointParams are extra URL query parameters to include in the request EndpointParams *url.Values // RefreshToken from a cache, if available. If not, then the first time a // token is requested it will be loaded from the token source and this value // will get updated if it's present in the returned token. RefreshToken string // TokenSource to wrap to fetch new tokens if the refresh token is missing or // did not work to get a new token. TokenSource oauth2.TokenSource }
RefreshTokenSource will use a refresh token to try and get a new token before calling the original token source to get a new token.