oauthproxy

package module
v0.0.0-...-95199a2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 35 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Oauth Proxy

This proxy is created to allow multiple clients (programs, servers) to use the same Oauth token to be used simultaneously and keeping the latest (refresh) token in sync in multiple processes.

This is a proxy specifically for Oauth token calls. It intercepts calls to the token endpoint of a Oauth provider. The proxy keeps a local database of the tokens and expire times. Whenever a token is to expire, the proxy refreshes the token with a call to the original provider endpoint and stores the new tokens (refresh token and access token) centrally. So is ensured every client uses the same refresh token, access token and expire time. The proxy takes care of making calls to the original token endpoint.

       +----------+
       |          |
       |  Oauth   |
       | provider |
       |          |
       +----+-----+
            ^
            |
            v
       +----+-----+
       |          |
       |  Oauth   |
       |  proxy   |
       |          |
       +-+------+-+
         ^      ^
         |      |
     +---+      +---+
     |              |
+----+-----+   +----+-----+
|          |   |          |
|  Oauth   |   |  Oauth   |
|  client  |   |  client  |
|          |   |          |
+----------+   +----------+

Installation

go get github.com/omniboost/oauth-proxy/bin/oauth-proxy

Grafana events

Request logs and secret read/write events are buffered and sent to Loki in a single push at the end of each token or revoke request, before the handler returns. This makes sure they're not lost when Lambda freezes the execution environment after the response.

Each request owns its own buffer; events are never shared between requests. Buffering does not perform network I/O; sending the batch at the end of the handler waits for Loki, with a 5-second timeout.

Secret reads and writes

Every read and write of a stored token is logged, so we know how often secrets are read from and written to the database (IN-5149). Token values are never logged.

Operation When
read a token row is looked up: once per token request (also when it isn't found or expired) and refresh token revoke
write a token is saved and its access or refresh token changed (refresh_token_changed, access_token_changed), or a token is revoked; also when that failed

The lookup while saving a token is part of the write and isn't counted, so a refresh counts as one read and one write and a code exchange as one write. Changes to only e.g. the expiry don't count as a write.

All events have app (the provider) and grant_type as structured metadata. Examples:

# reads and writes per hour
sum by (operation) (count_over_time({service="oauth-proxy", event="secret"} [1h]))

# reads per provider per minute
sum by (app) (count_over_time({service="oauth-proxy", event="secret", operation="read"} [1m]))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GRAFANA_LOKI_URL   = os.Getenv("GRAFANA_LOKI_URL")
	GRAFANA_LOKI_USER  = os.Getenv("GRAFANA_LOKI_USER")
	GRAFANA_LOKI_TOKEN = os.Getenv("GRAFANA_LOKI_TOKEN")
)

Functions

This section is empty.

Types

type ErrorResponse

type ErrorResponse struct {
	// acceptable:
	// - invalid_request
	// - invalid_client
	// - invalid_grant
	// - invalid_scope
	// - unauthorized_client
	// - unsupported_grant_type
	Error            string `json:"error"`
	ErrorDescription string `json:"error_description,omitempty"`
	ErrorURI         string `json:"error_uri,omitempty"`
}

type GrafanaEvents

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

GrafanaEvents holds events for one token request. The token worker finishes writing before returning its result; the handler then adds its request log and sends the batch. A buffer must not be shared between requests.

func NewGrafanaEvents

func NewGrafanaEvents() *GrafanaEvents

func (*GrafanaEvents) QueueRequestLog

func (events *GrafanaEvents) QueueRequestLog(r *http.Request, metrics httpsnoop.Metrics)

QueueRequestLog queues a log entry for a handled HTTP request

func (*GrafanaEvents) QueueSecretRead

func (events *GrafanaEvents) QueueSecretRead(app, grantType string)

QueueSecretRead queues an event when a token row is looked up in the database, whether it's found or not. The lookup while saving a token is part of the write and isn't counted.

func (*GrafanaEvents) QueueSecretRevoked

func (events *GrafanaEvents) QueueSecretRevoked(app, grantType string)

QueueSecretRevoked queues one event per revoke that marks a token as revoked in the database, also when that failed.

func (*GrafanaEvents) QueueSecretSaved

func (events *GrafanaEvents) QueueSecretSaved(dbToken mysql.OauthToken, refreshTokenChanged, accessTokenChanged bool)

QueueSecretSaved queues an event when a new refresh token and/or access token is written to the database, also when saving failed. Changes to only e.g. the expiry don't count as a write.

type RawMessages

type RawMessages map[string]json.RawMessage

type RevokeRequest

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

type RoundTripperWithSave

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

func NewRoundTripperWithSave

func NewRoundTripperWithSave(rtp http.RoundTripper) *RoundTripperWithSave

func (*RoundTripperWithSave) LastResponseBody

func (rt *RoundTripperWithSave) LastResponseBody() io.Reader

func (*RoundTripperWithSave) RoundTrip

func (rt *RoundTripperWithSave) RoundTrip(req *http.Request) (*http.Response, error)

type Server

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

func NewServer

func NewServer() (*Server, error)

func (*Server) Addr

func (s *Server) Addr() string

func (*Server) ErrorResponse

func (s *Server) ErrorResponse(w http.ResponseWriter, err error)

func (*Server) GetTokenRequestParamsFromFormRequest

func (s *Server) GetTokenRequestParamsFromFormRequest(r *http.Request) (providers.TokenRequestParams, error)

func (*Server) GetTokenRequestParamsFromJSONRequest

func (s *Server) GetTokenRequestParamsFromJSONRequest(r *http.Request) (providers.TokenRequestParams, error)

func (*Server) GetTokenRequestParamsFromRequest

func (s *Server) GetTokenRequestParamsFromRequest(r *http.Request) (providers.TokenRequestParams, error)

func (*Server) GetTokenRevokeParamsFromRequest

func (s *Server) GetTokenRevokeParamsFromRequest(r *http.Request) (TokenRevokeParams, error)

func (*Server) NewClient

func (s *Server) NewClient() *http.Client

func (*Server) NewDB

func (s *Server) NewDB() (*sql.DB, error)

func (*Server) NewHTTP

func (s *Server) NewHTTP() *http.Server

func (*Server) NewProviderRevokeHandler

func (s *Server) NewProviderRevokeHandler(provider providers.RevokeProvider) http.HandlerFunc

func (*Server) NewProviderTokenHandler

func (s *Server) NewProviderTokenHandler(provider providers.Provider) http.HandlerFunc

func (*Server) NewProviders

func (s *Server) NewProviders() providers.Providers

func (*Server) NewRouter

func (s *Server) NewRouter() *http.ServeMux

func (*Server) PushEventsToGrafana

func (s *Server) PushEventsToGrafana(events *GrafanaEvents) error

PushEventsToGrafana consumes and sends this request's events in a single push.

func (*Server) RequestToken

func (s *Server) RequestToken(provider providers.Provider, params providers.TokenRequestParams, events *GrafanaEvents, hub *sentry.Hub) (*Token, error)

func (*Server) RevokeToken

func (s *Server) RevokeToken(provider providers.RevokeProvider, params TokenRevokeParams, events *GrafanaEvents, hub *sentry.Hub) (*http.Response, error)

func (*Server) SetClient

func (s *Server) SetClient(client *http.Client)

func (*Server) SetDB

func (s *Server) SetDB(db *sql.DB)

func (*Server) SetHTTP

func (s *Server) SetHTTP(http *http.Server)

func (*Server) SetPort

func (s *Server) SetPort(port int)

func (*Server) SetProviders

func (s *Server) SetProviders(pp providers.Providers)

func (*Server) SetRouter

func (s *Server) SetRouter(r *http.ServeMux)

func (*Server) Start

func (s *Server) Start() error

func (*Server) StartLambda

func (s *Server) StartLambda() error

func (*Server) StartLocal

func (s *Server) StartLocal() error

func (*Server) Stop

func (s *Server) Stop() error

type Token

type Token struct {
	*oauth2.Token
	Raw map[string]json.RawMessage
}

type TokenRequest

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

type TokenRequestBody

type TokenRequestBody struct {
	RefreshToken string `json:"refresh_token"`
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`
	Code         string `json:"code"`
	RedirectURL  string `json:"redirect_uri"`
	CodeVerifier string `json:"code_verifier,omitempty"`
	GrantType    string
	Username     string
	Password     string

	RawMessages
}

func (*TokenRequestBody) UnmarshalJSON

func (rb *TokenRequestBody) UnmarshalJSON(data []byte) error

func (TokenRequestBody) Validate

func (rb TokenRequestBody) Validate() []error

type TokenRequestResult

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

type TokenRequester

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

func NewTokenRequester

func NewTokenRequester(db *sql.DB, provider providers.Provider) *TokenRequester

func (*TokenRequester) AuthorizationTokenFromDB

func (tr *TokenRequester) AuthorizationTokenFromDB(db mysql.DB, params providers.TokenRequestParams) (*mysql.OauthToken, error)

func (*TokenRequester) ClientCredentialsTokenFromDB

func (tr *TokenRequester) ClientCredentialsTokenFromDB(db mysql.DB, params providers.TokenRequestParams) (*mysql.OauthToken, error)

func (*TokenRequester) CodeExchange

func (tr *TokenRequester) CodeExchange(req TokenRequest) (*Token, error)

func (*TokenRequester) DBTokenToOauth2Token

func (tr *TokenRequester) DBTokenToOauth2Token(dbToken *mysql.OauthToken) (*Token, error)

func (*TokenRequester) FetchNewTokenAuthorizationCode

func (tr *TokenRequester) FetchNewTokenAuthorizationCode(params providers.TokenRequestParams) (*oauth2.Token, error)

func (*TokenRequester) FetchNewTokenClientCredentials

func (tr *TokenRequester) FetchNewTokenClientCredentials(params providers.TokenRequestParams) (*oauth2.Token, error)

func (*TokenRequester) FetchNewTokenPassword

func (tr *TokenRequester) FetchNewTokenPassword(params providers.TokenRequestParams) (*oauth2.Token, error)

func (*TokenRequester) IncrementNrOfSubsequentProviderErrors

func (tr *TokenRequester) IncrementNrOfSubsequentProviderErrors(db mysql.DB, token *mysql.OauthToken) error

func (*TokenRequester) Listen

func (tr *TokenRequester) Listen()

func (*TokenRequester) NewTokenRequest

func (tr *TokenRequester) NewTokenRequest(params providers.TokenRequestParams, events *GrafanaEvents, hub *sentry.Hub) TokenRequest

func (*TokenRequester) PasswordTokenFromDB

func (tr *TokenRequester) PasswordTokenFromDB(db mysql.DB, params providers.TokenRequestParams) (*mysql.OauthToken, error)

func (*TokenRequester) Request

func (tr *TokenRequester) Request(params providers.TokenRequestParams, events *GrafanaEvents, hub *sentry.Hub) (*Token, error)

func (*TokenRequester) SaveAuthorizationToken

func (tr *TokenRequester) SaveAuthorizationToken(db mysql.DB, token *Token, params providers.TokenRequestParams, events *GrafanaEvents) (mysql.OauthToken, error)

func (*TokenRequester) SaveClientCredentialsToken

func (tr *TokenRequester) SaveClientCredentialsToken(db mysql.DB, token *Token, params providers.TokenRequestParams, events *GrafanaEvents) (mysql.OauthToken, error)

func (*TokenRequester) SavePasswordToken

func (tr *TokenRequester) SavePasswordToken(db mysql.DB, token *Token, params providers.TokenRequestParams, events *GrafanaEvents) (mysql.OauthToken, error)

func (*TokenRequester) Start

func (tr *TokenRequester) Start()

func (*TokenRequester) Stop

func (tr *TokenRequester) Stop()

func (*TokenRequester) TokenRefresh

func (tr *TokenRequester) TokenRefresh(req TokenRequest) (*Token, error)

func (*TokenRequester) TokenRefreshAuthorizationCode

func (tr *TokenRequester) TokenRefreshAuthorizationCode(req TokenRequest) (*Token, error)

func (*TokenRequester) TokenRefreshClientCredentials

func (tr *TokenRequester) TokenRefreshClientCredentials(req TokenRequest) (*Token, error)

func (*TokenRequester) TokenRefreshPassword

func (tr *TokenRequester) TokenRefreshPassword(req TokenRequest) (*Token, error)

func (*TokenRequester) VerifyIDToken

func (tr *TokenRequester) VerifyIDToken(token *oauth2.Token, params providers.TokenRequestParams) error

type TokenResponseBody

type TokenResponseBody struct {
	TokenType    string `json:"token_type"`
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	ExpiresIn    int    `json:"expires_in"`

	RawMessages `json:"-"`
}

func (TokenResponseBody) MarshalJSON

func (rb TokenResponseBody) MarshalJSON() ([]byte, error)

func (*TokenResponseBody) UnmarshalJSON

func (rb *TokenResponseBody) UnmarshalJSON(data []byte) error

type TokenRevokeParams

type TokenRevokeParams struct {
	ClientID     string `schema:"client_id"`
	ClientSecret string `schema:"client_secret"`

	Token         string `schema:"token"`
	TokenTypeHint string `schema:"token_type_hint"`
	Request       *http.Request
}

type TokenRevokeResult

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

type TokenRevoker

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

func NewTokenRevoker

func NewTokenRevoker(db *sql.DB, provider providers.RevokeProvider) *TokenRevoker

func (*TokenRevoker) Listen

func (tr *TokenRevoker) Listen()

func (*TokenRevoker) NewTokenRevoke

func (tr *TokenRevoker) NewTokenRevoke(params TokenRevokeParams, events *GrafanaEvents, hub *sentry.Hub) RevokeRequest

func (*TokenRevoker) Revoke

func (tr *TokenRevoker) Revoke(params TokenRevokeParams, events *GrafanaEvents, hub *sentry.Hub) (*http.Response, error)

func (*TokenRevoker) Start

func (tr *TokenRevoker) Start()

Directories

Path Synopsis
bin
oauth-proxy command
Package mysql contains generated code for schema 'oauth_proxy'.
Package mysql contains generated code for schema 'oauth_proxy'.

Jump to

Keyboard shortcuts

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