promoauth

package
v2.10.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package promoauth is for instrumenting oauth2 flows with prometheus metrics. Specifically, it is intended to count the number of external requests made by the underlying oauth2 exchanges.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

func (*Config) AuthCodeURL

func (c *Config) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string

func (*Config) Do

func (c *Config) Do(ctx context.Context, source Oauth2Source, req *http.Request) (*http.Response, error)

func (*Config) Exchange

func (c *Config) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)

func (*Config) InstrumentHTTPClient added in v2.8.0

func (c *Config) InstrumentHTTPClient(hc *http.Client, source Oauth2Source) *http.Client

InstrumentHTTPClient will always return a new http client. The new client will match the one passed in, but will have an instrumented round tripper.

func (*Config) TokenSource

func (c *Config) TokenSource(ctx context.Context, token *oauth2.Token) oauth2.TokenSource

type Factory

type Factory struct {

	// optional replace now func
	Now func() time.Time
	// contains filtered or unexported fields
}

Factory allows us to have 1 set of metrics for all oauth2 providers. Primarily to avoid any prometheus errors registering duplicate metrics.

func NewFactory

func NewFactory(registry prometheus.Registerer) *Factory

func (*Factory) New

func (f *Factory) New(name string, under OAuth2Config) *Config

func (*Factory) NewGithub

func (f *Factory) NewGithub(name string, under OAuth2Config) *Config

NewGithub returns a new instrumented oauth2 config for github. It tracks rate limits as well as just the external request counts.

type InstrumentedOAuth2Config

type InstrumentedOAuth2Config interface {
	OAuth2Config

	// Do is provided as a convenience method to make a request with the oauth2 client.
	// It mirrors `http.Client.Do`.
	Do(ctx context.Context, source Oauth2Source, req *http.Request) (*http.Response, error)
}

InstrumentedOAuth2Config extends OAuth2Config with a `Do` method that allows external oauth related calls to be instrumented. This is to support "ValidateToken" which is not an oauth2 specified method. These calls still count against the api rate limit, and should be instrumented.

type OAuth2Config

type OAuth2Config interface {
	AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string
	Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)
	TokenSource(context.Context, *oauth2.Token) oauth2.TokenSource
}

OAuth2Config exposes a subset of *oauth2.Config functions for easier testing. *oauth2.Config should be used instead of implementing this in production.

type Oauth2Source

type Oauth2Source string
const (
	SourceValidateToken    Oauth2Source = "ValidateToken"
	SourceExchange         Oauth2Source = "Exchange"
	SourceTokenSource      Oauth2Source = "TokenSource"
	SourceAppInstallations Oauth2Source = "AppInstallations"
	SourceAuthorizeDevice  Oauth2Source = "AuthorizeDevice"

	SourceGitAPIAuthUser        Oauth2Source = "GitAPIAuthUser"
	SourceGitAPIListEmails      Oauth2Source = "GitAPIListEmails"
	SourceGitAPIOrgMemberships  Oauth2Source = "GitAPIOrgMemberships"
	SourceGitAPITeamMemberships Oauth2Source = "GitAPITeamMemberships"
)

Jump to

Keyboard shortcuts

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