Documentation
¶
Index ¶
- Constants
- func ClearOAuthStateCookie(w http.ResponseWriter, secure bool)
- func ClearSessionCookie(w http.ResponseWriter, secure bool)
- func ClientIP(r *http.Request) string
- func EffectiveOrganizationID(ctx context.Context) uuid.UUID
- func IsGlobalAdmin(u *store.User) bool
- func IsGlobalAdminRequest(ctx context.Context, u *store.User) bool
- func Middleware(s store.Storer) func(http.Handler) http.Handler
- func NewOAuthStateNonce() (string, error)
- func NewSessionID() string
- func RequestFromContext(ctx context.Context) *http.Request
- func RequireGlobalAdminForNilOrg(ctx context.Context, user *store.User, organizationID uuid.UUID) error
- func ResolveOrgID(ctx context.Context, user *store.User, inputOrgID uuid.UUID) *uuid.UUID
- func SetOAuthStateCookie(w http.ResponseWriter, payload OAuthStatePayload, secure bool) error
- func SetSessionCookie(w http.ResponseWriter, sessionID string, secure bool)
- func UserFromContext(ctx context.Context) *store.User
- func UserIDFromContext(ctx context.Context) uuid.UUID
- func UserOrgForAccess(ctx context.Context, user *store.User) uuid.UUID
- func WithEffectiveOrganization(ctx context.Context, orgID uuid.UUID) context.Context
- func WithRequest(ctx context.Context, r *http.Request) context.Context
- func WithUser(ctx context.Context, user *store.User) context.Context
- func WriteJSONError(w http.ResponseWriter, msg string, code int)
- type LoginAttemptLimiter
- type OAuthStatePayload
Constants ¶
const ( // DefaultLoginMaxAttempts is the number of failed login attempts before blocking. DefaultLoginMaxAttempts = 5 // DefaultLoginWindow is the time window in which failures are counted; counter resets after. DefaultLoginWindow = 15 * time.Minute )
const ( SessionCookieName = "ipam_session" SessionDuration = 24 * time.Hour )
const (
OAuthStateCookieName = "ipam_oauth_state"
)
Variables ¶
This section is empty.
Functions ¶
func ClearOAuthStateCookie ¶ added in v0.1.1
func ClearOAuthStateCookie(w http.ResponseWriter, secure bool)
ClearOAuthStateCookie removes the OAuth state cookie after callback.
func ClearSessionCookie ¶
func ClearSessionCookie(w http.ResponseWriter, secure bool)
ClearSessionCookie clears the session cookie. secure should match the cookie that was set (e.g. request was HTTPS).
func EffectiveOrganizationID ¶
EffectiveOrganizationID returns the effective organization for this request, or uuid.Nil if not set. When set (e.g. org-scoped API token), handlers should filter by this org and not treat the user as global admin for scope.
func IsGlobalAdmin ¶
IsGlobalAdmin returns true if the user is the global admin (no organization). Global admin can create organizations and access all org-scoped resources. OrganizationID == uuid.Nil is the global-admin sentinel; it must never be assignable by non-global-admin. Prefer IsGlobalAdminRequest for authorization decisions so org-scoped API tokens are not treated as global admin.
func IsGlobalAdminRequest ¶ added in v0.1.1
IsGlobalAdminRequest reports whether this request has unrestricted global-admin privileges. A global admin user presenting an org-scoped API token is not treated as global admin for the request.
func Middleware ¶
Middleware returns a middleware that requires a valid session or API key for /api/* except login and logout.
func NewOAuthStateNonce ¶ added in v0.1.1
NewOAuthStateNonce returns a URL-safe random nonce for OAuth state.
func RequestFromContext ¶
RequestFromContext returns the request from the context, or nil if not set.
func RequireGlobalAdminForNilOrg ¶
func RequireGlobalAdminForNilOrg(ctx context.Context, user *store.User, organizationID uuid.UUID) error
RequireGlobalAdminForNilOrg returns nil if organizationID is not Nil, or if this request has global-admin privileges. Otherwise it returns an error so that assigning "global admin" (Nil org) is never allowed for non-global-admin or org-scoped API tokens. Call this before any operation that could set a user's or invite's organization to Nil.
func ResolveOrgID ¶
ResolveOrgID returns the organization ID to use for list/create: effective org from token if set, else user's org (or optional input org for global admin). Used by env/block/alloc/reserved handlers.
func SetOAuthStateCookie ¶ added in v0.1.1
func SetOAuthStateCookie(w http.ResponseWriter, payload OAuthStatePayload, secure bool) error
SetOAuthStateCookie stores the OAuth CSRF state (must match the state query param on callback).
func SetSessionCookie ¶
func SetSessionCookie(w http.ResponseWriter, sessionID string, secure bool)
SetSessionCookie sets the session cookie on the response. secure should be true when using HTTPS.
func UserFromContext ¶
UserFromContext returns the user from the context, or nil if not set.
func UserIDFromContext ¶
UserIDFromContext returns the current user's ID, or uuid.Nil if not set.
func UserOrgForAccess ¶
UserOrgForAccess returns the organization ID to use for access checks (get/update/delete). When effective org is set (org-scoped token), returns that; else returns user.OrganizationID (Nil for global admin).
func WithEffectiveOrganization ¶
WithEffectiveOrganization sets the effective organization for this request (e.g. from an org-scoped API token). When set, the request is limited to that org even if the user is global admin.
func WithRequest ¶
WithRequest returns a context with the request attached (for use cases that need cookies etc.).
func WriteJSONError ¶
func WriteJSONError(w http.ResponseWriter, msg string, code int)
WriteJSONError writes a JSON error response.
Types ¶
type LoginAttemptLimiter ¶
type LoginAttemptLimiter struct {
// contains filtered or unexported fields
}
LoginAttemptLimiter limits failed login attempts per client IP to mitigate brute-force.
func NewLoginAttemptLimiter ¶
func NewLoginAttemptLimiter(maxAttempts int, window time.Duration) *LoginAttemptLimiter
NewLoginAttemptLimiter returns a limiter that blocks an IP after maxAttempts failed logins within the given window. Pass 0 for max or window to use defaults.
func (*LoginAttemptLimiter) IsBlocked ¶
func (l *LoginAttemptLimiter) IsBlocked(ip string) bool
IsBlocked returns true if the client IP has exceeded the failure limit and is still within the window.
func (*LoginAttemptLimiter) RecordFailure ¶
func (l *LoginAttemptLimiter) RecordFailure(ip string)
RecordFailure records a failed login attempt for the IP.
func (*LoginAttemptLimiter) RecordSuccess ¶
func (l *LoginAttemptLimiter) RecordSuccess(ip string)
RecordSuccess clears any failure count for the IP (e.g. after successful login).
type OAuthStatePayload ¶ added in v0.1.1
type OAuthStatePayload struct {
Nonce string `json:"nonce"`
Provider string `json:"provider"`
InviteToken string `json:"invite_token,omitempty"`
}
OAuthStatePayload is stored in the OAuth state cookie during the authorize redirect.
func OAuthStateFromRequest ¶ added in v0.1.1
func OAuthStateFromRequest(r *http.Request) (OAuthStatePayload, bool)
OAuthStateFromRequest reads and validates the OAuth state cookie.