Documentation
¶
Index ¶
- Constants
- Variables
- func CheckSkipProxy(r *http.Request, cfg Config) bool
- func Decrypt(secret string, ciphertext string) ([]byte, error)
- func Encrypt(secret string, plaintext []byte) (string, error)
- func ResolveCurrentURL(r *http.Request, cfg Config) (*url.URL, error)
- func StripTrailingSlash(u string) string
- type Config
- type Option
- type PassthroughPayload
- type Plugin
- func (p *Plugin) Config() Config
- func (p *Plugin) CreatePassthroughPayload(payload *PassthroughPayload) (string, error)
- func (p *Plugin) CreateStatePackage(state, callbackURL, currentURL string) (string, error)
- func (p *Plugin) ID() string
- func (p *Plugin) Init(ctx *plugin.Context) error
- func (p *Plugin) InterceptCallback(next http.Handler) http.Handler
- func (p *Plugin) InterceptSignIn(next http.Handler) http.Handler
- func (p *Plugin) ParsePassthroughPayload(encryptedPayload string) (*PassthroughPayload, error)
- func (p *Plugin) ParseStatePackage(encryptedState string) (*StatePackage, error)
- func (p *Plugin) ServeOAuthProxyCallback(w http.ResponseWriter, r *http.Request)
- type StatePackage
Constants ¶
const PluginID = "oauth-proxy"
PluginID is the unique string identifier for the OAuth Proxy plugin ("oauth-proxy").
Variables ¶
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") )
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 ¶
CheckSkipProxy determines if the proxy handling should be bypassed for a request.
func Decrypt ¶
Decrypt decrypts a Base64 URL-safe ciphertext using AES-256-GCM with a key derived from secret.
func Encrypt ¶
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 ¶
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 ¶
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 ¶
WithCurrentURL configures the explicit preview deployment URL.
func WithMaxAge ¶
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 ¶
WithProductionURL configures the production server base URL.
func WithProxyCallbackPath ¶
WithProxyCallbackPath configures the HTTP endpoint path on the preview server for proxy callbacks.
func WithSecret ¶
WithSecret configures the shared secret key used for AES-256-GCM encryption.
func WithSkipProxyHeader ¶
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 (*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 ¶
CreateStatePackage serializes and encrypts a StatePackage struct into a URL-safe Base64 string.
func (*Plugin) InterceptCallback ¶
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 ¶
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.