Documentation
¶
Overview ¶
Package auth provides HTTP middleware for token-based authentication.
Package auth implements SSH key-based authentication and stateless token signing/verification for ussycode.
Tokens use the format: base64url(JSON_payload).base64url(ssh_wire_signature) Signed with SSH private keys, verified with SSH public keys.
Index ¶
- Constants
- func FingerprintKey(key ssh.PublicKey) string
- func GenerateHandle() (string, error)
- func Middleware(resolve KeyResolver) func(http.Handler) http.Handler
- func ParsePublicKey(authorizedKey string) (ssh.PublicKey, string, error)
- func SignToken(signer ssh.Signer, subject string, ttl time.Duration, perms []string) (string, error)
- type KeyResolver
- type TokenPayload
Constants ¶
const (
// ContextKeyPayload is the context key for the authenticated token payload.
ContextKeyPayload contextKey = "auth.payload"
)
Variables ¶
This section is empty.
Functions ¶
func FingerprintKey ¶
FingerprintKey returns the SHA256 fingerprint of an SSH public key.
func GenerateHandle ¶
GenerateHandle creates a random short handle for token storage. Returns a 22-character base64url string (16 bytes of entropy).
func Middleware ¶
func Middleware(resolve KeyResolver) func(http.Handler) http.Handler
Middleware returns HTTP middleware that validates Bearer tokens. It extracts the token from the Authorization header, verifies it, and stores the payload in the request context.
func ParsePublicKey ¶
ParsePublicKey parses an SSH public key from authorized_keys format.
Types ¶
type KeyResolver ¶
KeyResolver returns the trusted public keys for a given subject (user handle).
type TokenPayload ¶
type TokenPayload struct {
Subject string `json:"sub"` // user handle
IssuedAt int64 `json:"iat"` // unix timestamp
ExpiresAt int64 `json:"exp"` // unix timestamp
NotBefore int64 `json:"nbf"` // unix timestamp
Perms []string `json:"perms,omitempty"` // permission scopes
Nonce string `json:"nonce"` // replay prevention
}
TokenPayload is the claims embedded in a signed token.
func PayloadFromContext ¶
func PayloadFromContext(ctx context.Context) (*TokenPayload, bool)
PayloadFromContext extracts the token payload from the request context.
func VerifyToken ¶
func VerifyToken(token string, trustedKeys []ssh.PublicKey) (*TokenPayload, error)
VerifyToken verifies a signed token against a set of trusted public keys. Returns the payload if valid, or an error if verification fails.