mod_ai_token_auth

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenStatusEnabled   = 1
	TokenStatusDisabled  = 2
	TokenStatusExpired   = 3
	TokenStatusExhausted = 4
)
View Source
const (
	ActionCheckToken = "CHECK_TOKEN"
)
View Source
const (
	ModAITokenAuth = "mod_ai_token_auth"
)
View Source
const REQ_TOKEN_AUTH_CONTEXT = "tokenauth_ctx"

Variables

This section is empty.

Functions

func ActionFileCheck

func ActionFileCheck(conf *ActionFile) error

func CalcReqUsedQuota

func CalcReqUsedQuota(req *bfe_basic.Request, promptTokens, completionTokens int64) int64

func GetApiKey

func GetApiKey(req *bfe_basic.Request) string

func GetPromptToken

func GetPromptToken(req *bfe_basic.Request) int64

func GetUUID

func GetUUID() string

func ProductRuleConfLoad

func ProductRuleConfLoad(filename string) (productRuleConf, error)

func SetAiAuthInfo added in v1.8.3

func SetAiAuthInfo(req *bfe_basic.Request, rejectReason string, rejectQuotaPlans []string)

func SetApiKey

func SetApiKey(req *bfe_http.Request, apiKey string)

func SetTokenAuthContext

func SetTokenAuthContext(req *bfe_basic.Request, tok *Token, promptToken int64, tags []bfe_basic.ApikeyTag)

SetTokenAuthContext sets the token authentication context in the request

func UpdateCtxByUsage added in v1.8.3

func UpdateCtxByUsage(ctx *TokenAuthContext, data []byte)

Types

type Action

type Action ActionFile

type ActionFile

type ActionFile struct {
	Cmd string
}

type ConfModAITokenAuth

type ConfModAITokenAuth struct {
	Basic struct {
		ProductRulePath string
	}

	// redis conf
	Redis struct {
		Bns            string // bns name for redis proxy
		ConnectTimeout int    // connect timeout (ms)
		ReadTimeout    int    // read timeout (ms)
		WriteTimeout   int    // write timeout(ms)

		// max idle connections in pool
		MaxIdle int

		// redis password,ignore if not set
		Password string

		// max active connections in pool,
		// when set 0, there is no connection num limit
		MaxActive int
	}

	Log struct {
		OpenDebug bool
	}
}

func ConfLoad

func ConfLoad(filePath string, confRoot string) (*ConfModAITokenAuth, error)

func (*ConfModAITokenAuth) Check

func (cfg *ConfModAITokenAuth) Check(confRoot string) error

type ModuleAITokenAuth

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

func NewModuleAITokenAuth

func NewModuleAITokenAuth() *ModuleAITokenAuth

func (*ModuleAITokenAuth) Init

func (*ModuleAITokenAuth) Name

func (m *ModuleAITokenAuth) Name() string

func (*ModuleAITokenAuth) ValidateUserTokenByReq

func (m *ModuleAITokenAuth) ValidateUserTokenByReq(req *bfe_basic.Request) (token *Token, err *bfe_basic.AiError)

type ModuleAITokenAuthState

type ModuleAITokenAuthState struct {
	ReqTotal    *metrics.Counter
	ReqAuth     *metrics.Counter
	ReqAuthFail *metrics.Counter
}

type ProductQuotaPlanFiles added in v1.8.3

type ProductQuotaPlanFiles map[string]*quotaPlanFileList

type ProductQuotaPlans added in v1.8.3

type ProductQuotaPlans map[string]*QuotaPlanMap

type ProductRules

type ProductRules map[string]*tokenRuleList

type ProductRulesFile

type ProductRulesFile map[string]*tokenRuleFileList

type ProductTokenFiles

type ProductTokenFiles map[string]*tokenFileMap

type ProductTokens

type ProductTokens map[string]*tokenMap

type QuotaPlan added in v1.8.3

type QuotaPlan struct {
	Id          string
	Unlimited   bool
	PassNoQuota bool
	RedisKey    string
	CreateTime  int64
	ExpiredTime int64 // -1 means never expired
	Quota       int64 // 配额总量
	ResetMode   int   // 0 – 非周期性;1 – 周期性的配额包
}

func (*QuotaPlan) Deduct added in v1.8.3

func (q *QuotaPlan) Deduct(client redis_client.Client, amount int64) (int64, error)

func (*QuotaPlan) HasBalance added in v1.8.3

func (q *QuotaPlan) HasBalance(client redis_client.Client) (bool, int64, error)

type QuotaPlanMap added in v1.8.3

type QuotaPlanMap map[string]*QuotaPlan

type Token

type Token struct {
	Key            string
	Status         int
	Name           string
	UpdateTime     int64
	ExpiredTime    int64
	UnlimitedQuota bool
	Models         []string
	BlockModels    []string
	Subnet         []*net.IPNet
	Tags           []bfe_basic.ApikeyTag
	QuotaPlans     []*QuotaPlan
}

type TokenAuthContext

type TokenAuthContext struct {
	Token *Token
	// contains filtered or unexported fields
}

func GetTokenAuthContext

func GetTokenAuthContext(req *bfe_basic.Request) *TokenAuthContext

type TokenFile

type TokenFile struct {
	Key            string  `json:"key"`
	Enabled        int     `json:"enabled"`
	Status         int     `json:"status"`
	Name           string  `json:"name"`
	UpdateTime     int64   `json:"update_time"`
	ExpiredTime    int64   `json:"expired_time"` // -1 means never expired
	UnlimitedQuota bool    `json:"unlimited_quota"`
	Models         *string `json:"allow_models"` // allowed models
	BlockModels    *string `json:"block_models"` // blocked models
	Subnet         *string `json:"subnet"`       // allowed subnet
	Tags           []bfe_basic.ApikeyTag
	QuotaPlans     []string `json:"quota_plans"` // quotaPlan IDs
	// contains filtered or unexported fields
}

type TokenRuleTable

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

func NewTokenRuleTable

func NewTokenRuleTable() *TokenRuleTable

func (*TokenRuleTable) GetToken

func (t *TokenRuleTable) GetToken(product, key string) (*Token, bool)

func (*TokenRuleTable) Search

func (t *TokenRuleTable) Search(product string) (*tokenRuleList, bool)

func (*TokenRuleTable) Update

func (t *TokenRuleTable) Update(conf productRuleConf)

func (*TokenRuleTable) ValidateUserToken

func (t *TokenRuleTable) ValidateUserToken(product, key string) (token *Token, err error)

Jump to

Keyboard shortcuts

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