mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-28 17:25:51 +08:00
feat/auth-hook: add post auth hook
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
@@ -12,6 +13,18 @@ import (
|
||||
baseauth "github.com/router-for-me/CLIProxyAPI/v6/internal/auth"
|
||||
)
|
||||
|
||||
// PostAuthHook defines a function that is called after an Auth record is created
|
||||
// but before it is persisted to storage. This allows for modification of the
|
||||
// Auth record (e.g., injecting metadata) based on external context.
|
||||
type PostAuthHook func(context.Context, *Auth) error
|
||||
|
||||
// RequestInfo holds information extracted from the HTTP request.
|
||||
// It is injected into the context passed to PostAuthHook.
|
||||
type RequestInfo struct {
|
||||
Query map[string]string
|
||||
Headers map[string]string
|
||||
}
|
||||
|
||||
// Auth encapsulates the runtime state and metadata associated with a single credential.
|
||||
type Auth struct {
|
||||
// ID uniquely identifies the auth record across restarts.
|
||||
|
||||
@@ -153,6 +153,16 @@ func (b *Builder) WithLocalManagementPassword(password string) *Builder {
|
||||
return b
|
||||
}
|
||||
|
||||
// WithPostAuthHook registers a hook to be called after an Auth record is created
|
||||
// but before it is persisted to storage.
|
||||
func (b *Builder) WithPostAuthHook(hook coreauth.PostAuthHook) *Builder {
|
||||
if hook == nil {
|
||||
return b
|
||||
}
|
||||
b.serverOptions = append(b.serverOptions, api.WithPostAuthHook(hook))
|
||||
return b
|
||||
}
|
||||
|
||||
// Build validates inputs, applies defaults, and returns a ready-to-run service.
|
||||
func (b *Builder) Build() (*Service, error) {
|
||||
if b.cfg == nil {
|
||||
|
||||
Reference in New Issue
Block a user