auth

package
v2.11.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 12 Imported by: 0 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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoginURL

func LoginURL() string

LoginURL returns the SSO login URL path. The frontend getter prepends REST_API_URL + "/" before this value.

func Middleware

func Middleware(tokens *TokenStore, enabled bool) func(http.Handler) http.Handler

Middleware checks the X-Auth-Token header and injects the user into context. When auth is enabled, requests without a valid token get 401. When auth is disabled, this middleware is a no-op.

func SplitScopes

func SplitScopes(scopes string) []string

SplitScopes splits a comma-separated scopes string.

Types

type Handlers

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

Handlers provides HTTP handlers for the auth flow.

func NewHandlers

func NewHandlers(oauth *OAuthClient, tokens *TokenStore) *Handlers

NewHandlers creates auth HTTP handlers.

func (*Handlers) RegisterProtectedRoutes

func (h *Handlers) RegisterProtectedRoutes(mux *http.ServeMux, authMiddleware func(http.Handler) http.Handler)

RegisterProtectedRoutes registers routes that need auth middleware.

func (*Handlers) RegisterRoutes

func (h *Handlers) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers auth endpoints on the mux. These routes are public (no auth middleware).

type OAuthClient

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

OAuthClient wraps an oauth2.Config and provider-specific user mapping.

func NewOAuthClient

func NewOAuthClient(provider, providerURL, clientID, clientSecret, callbackURL, scopes string) (*OAuthClient, error)

NewOAuthClient builds an OAuthClient from auth configuration. For OIDC-compatible providers it attempts discovery via .well-known/openid-configuration. For GitHub it uses hardcoded endpoints.

func (*OAuthClient) AuthCodeURL

func (c *OAuthClient) AuthCodeURL(state string) string

AuthCodeURL returns the URL to redirect the user to for authorization.

func (*OAuthClient) Exchange

func (c *OAuthClient) Exchange(code string) (*User, error)

Exchange trades an authorization code for tokens and fetches user info.

type ProviderEndpoints

type ProviderEndpoints struct {
	AuthURL     string
	TokenURL    string
	UserInfoURL string
}

ProviderEndpoints holds OAuth2 endpoint URLs and user info URL.

type TokenClaims

type TokenClaims struct {
	jwt.RegisteredClaims
	Provider string `json:"provider"`
	Username string `json:"username"`
	Email    string `json:"email"`
	Avatar   string `json:"avatar"`
}

TokenClaims are the JWT claims stored in internal tokens.

type TokenStore

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

TokenStore creates and validates internal JWT tokens.

func NewTokenStore

func NewTokenStore(secret string) *TokenStore

NewTokenStore creates a new TokenStore with the given HMAC secret.

func (*TokenStore) Create

func (s *TokenStore) Create(u User) (string, error)

Create issues a new JWT token for the given user.

func (*TokenStore) ExpiresAt

func (s *TokenStore) ExpiresAt(tokenStr string) (time.Time, error)

ExpiresAt validates a token and returns when it expires (zero: never).

func (*TokenStore) Validate

func (s *TokenStore) Validate(tokenStr string) (*User, error)

Validate parses and validates a JWT token string, returning the user.

type User

type User struct {
	Provider string `json:"provider"`
	Username string `json:"username"`
	Email    string `json:"email"`
	Avatar   string `json:"avatar"`
}

User represents an authenticated user.

func GuestUser

func GuestUser() User

GuestUser returns the default guest user when auth is disabled.

func UserFromContext

func UserFromContext(ctx context.Context) *User

UserFromContext extracts the authenticated user from the request context.

Jump to

Keyboard shortcuts

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