oauthproxy

package
v0.27.2 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const PluginID = "oauth-proxy"

PluginID is the unique string identifier for the OAuth Proxy plugin ("oauth-proxy").

Variables

View Source
var (
	ErrInvalidSecret    = errors.New("oauthproxy: secret key cannot be empty")
	ErrInvalidCipher    = errors.New("oauthproxy: invalid or corrupted ciphertext")
	ErrDecryptionFailed = errors.New("oauthproxy: decryption failed or secret mismatch")
)
View Source
var (
	ErrExpiredPayload = errors.New("oauthproxy: payload has expired beyond max age")
	ErrClockSkew      = errors.New("oauthproxy: payload timestamp is in the future beyond allowed tolerance")
)

Functions

func CheckSkipProxy

func CheckSkipProxy(r *http.Request, cfg Config) bool

CheckSkipProxy determines if the proxy handling should be bypassed for a request.

func Decrypt

func Decrypt(secret string, ciphertext string) ([]byte, error)

Decrypt decrypts a Base64 URL-safe ciphertext using AES-256-GCM with a key derived from secret.

func Encrypt

func Encrypt(secret string, plaintext []byte) (string, error)

Encrypt encrypts plaintext bytes using AES-256-GCM with a key derived from secret. The output is a URL-safe Base64 encoded string containing the 12-byte random nonce prepended to the ciphertext.

func ResolveCurrentURL

func ResolveCurrentURL(r *http.Request, cfg Config) (*url.URL, error)

ResolveCurrentURL resolves the base URL of the current deployment environment. It checks in order: Config.CurrentURL, vendor environment variables, and HTTP request headers.

func StripTrailingSlash

func StripTrailingSlash(u string) string

StripTrailingSlash removes trailing slashes from a URL string.

Types

type Config

type Config struct {
	// CurrentURL is the explicit URL of the preview deployment (e.g. "https://preview-123.myapp.com").
	// If empty, it will be automatically resolved from request headers or vendor environment variables.
	CurrentURL string

	// ProductionURL is the base URL of the production server (e.g. "https://myapp.com").
	ProductionURL string

	// Secret is the shared encryption key shared between preview and production environments.
	Secret string

	// MaxAge is the maximum allowed age for passthrough payloads to prevent replay attacks (default: 60s).
	MaxAge time.Duration

	// ProxyCallbackPath is the path on the preview server to handle proxy callbacks (default: "/api/auth/oauth-proxy-callback").
	ProxyCallbackPath string

	// SkipProxyHeader is the HTTP header used to bypass proxy interception (default: "X-Skip-OAuth-Proxy").
	SkipProxyHeader string

	// OnSuccess is an optional hook invoked when a preview server successfully decodes a PassthroughPayload.
	OnSuccess func(w http.ResponseWriter, r *http.Request, payload *PassthroughPayload) error
}

Config holds configuration parameters for the OAuth Proxy plugin.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default OAuth Proxy configuration.

type Option

type Option func(*Config)

Option applies a configuration setting to Config.

func WithCurrentURL

func WithCurrentURL(rawURL string) Option

WithCurrentURL configures the explicit preview deployment URL.

func WithMaxAge

func WithMaxAge(d time.Duration) Option

WithMaxAge configures the maximum allowed age for passthrough payloads before expiration.

func WithOnSuccess

func WithOnSuccess(fn func(w http.ResponseWriter, r *http.Request, payload *PassthroughPayload) error) Option

WithOnSuccess configures an optional success hook invoked upon receiving a valid payload on preview.

func WithProductionURL

func WithProductionURL(rawURL string) Option

WithProductionURL configures the production server base URL.

func WithProxyCallbackPath

func WithProxyCallbackPath(path string) Option

WithProxyCallbackPath configures the HTTP endpoint path on the preview server for proxy callbacks.

func WithSecret

func WithSecret(secret string) Option

WithSecret configures the shared secret key used for AES-256-GCM encryption.

func WithSkipProxyHeader

func WithSkipProxyHeader(header string) Option

WithSkipProxyHeader configures the HTTP header name used to bypass proxy routing.

type PassthroughPayload

type PassthroughPayload struct {
	User          entity.User    `json:"user"`
	Account       entity.Account `json:"account"`
	State         string         `json:"state,omitempty"`
	CallbackURL   string         `json:"callbackUrl,omitempty"`
	NewUserURL    string         `json:"newUserUrl,omitempty"`
	ErrorURL      string         `json:"errorUrl,omitempty"`
	DisableSignUp bool           `json:"disableSignUp,omitempty"`
	Timestamp     int64          `json:"timestamp"`
}

PassthroughPayload represents the encrypted payload transferred from Production to Preview after OAuth authentication.

type Plugin

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

Plugin implements the OAuth Proxy plugin for go-modular-auth.

func New

func New(opts ...Option) *Plugin

New creates a new OAuth Proxy plugin instance configured with options.

func (*Plugin) Config

func (p *Plugin) Config() Config

Config returns the active configuration of the OAuth Proxy plugin.

func (*Plugin) CreatePassthroughPayload

func (p *Plugin) CreatePassthroughPayload(payload *PassthroughPayload) (string, error)

CreatePassthroughPayload serializes and encrypts a PassthroughPayload struct into a URL-safe Base64 string.

func (*Plugin) CreateStatePackage

func (p *Plugin) CreateStatePackage(state, callbackURL, currentURL string) (string, error)

CreateStatePackage serializes and encrypts a StatePackage struct into a URL-safe Base64 string.

func (*Plugin) ID

func (p *Plugin) ID() string

ID returns the unique identifier for the OAuth Proxy plugin ("oauth-proxy").

func (*Plugin) Init

func (p *Plugin) Init(ctx *plugin.Context) error

Init initializes the plugin with the shared execution context.

func (*Plugin) InterceptCallback

func (p *Plugin) InterceptCallback(next http.Handler) http.Handler

InterceptCallback returns an http.Handler middleware for Production environments. It checks incoming provider callback requests for an encrypted proxy state. If present, it intercepts the response, packages the authenticated user profile, and redirects back to Preview.

func (*Plugin) InterceptSignIn

func (p *Plugin) InterceptSignIn(next http.Handler) http.Handler

InterceptSignIn returns an http.Handler middleware for Preview environments. It intercepts social/OAuth sign-in requests, wraps and encrypts the original state parameter into a StatePackage, and modifies the callback redirect_uri to point to the Production server callback URL.

func (*Plugin) ParsePassthroughPayload

func (p *Plugin) ParsePassthroughPayload(encryptedPayload string) (*PassthroughPayload, error)

ParsePassthroughPayload decrypts, deserializes, and validates the MaxAge and anti-replay timestamp of a profile payload.

func (*Plugin) ParseStatePackage

func (p *Plugin) ParseStatePackage(encryptedState string) (*StatePackage, error)

ParseStatePackage decrypts and deserializes an encrypted state parameter into a StatePackage struct.

func (*Plugin) ServeOAuthProxyCallback

func (p *Plugin) ServeOAuthProxyCallback(w http.ResponseWriter, r *http.Request)

ServeOAuthProxyCallback handles incoming HTTP requests on the preview server's proxy callback endpoint. It decrypts the "profile" query parameter, validates its expiration, triggers OnSuccess if configured, and redirects the browser to the final callback URL.

type StatePackage

type StatePackage struct {
	State       string `json:"state"`
	StateCookie string `json:"stateCookie,omitempty"`
	CallbackURL string `json:"callbackUrl,omitempty"`
	CurrentURL  string `json:"currentUrl"`
	CreatedAt   int64  `json:"createdAt"`
}

StatePackage encapsulates the original state, callback URL, and preview current URL. It is serialized to JSON and encrypted into the state query parameter sent to the OAuth provider.

Jump to

Keyboard shortcuts

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