Files
new-api/relay/channel/task/jsplugin/auth.go
T
CaIon 14a9acd368 feat(custom): CNY native billing, daily check-in button, subscription plans page, usage docs and promo banner
Based on QuantumNous/new-api v1.0.0-rc.36 (shallow history squashed).

- CNY quota display mode: RMB-denominated top-up and crediting across Epay/Waffo channels
- New CheckinButton in both authenticated and public headers (Turnstile supported)
- New /subscribe page with public plan snapshot, Alipay/balance payment, auto group upgrade/reset
- New /docs 12-section beginner guide with anchor navigation
- New home PromoBanner linking to /docs; i18n synced (en/zh/zh-TW/fr/ja/ru/vi)
- README: document custom features, live demo https://ai.88531.cn
2026-09-23 16:28:52 +08:00

46 lines
1.4 KiB
Go

package jsplugin
import (
"fmt"
"strings"
"sync"
"time"
"github.com/QuantumNous/new-api/common"
pluginruntime "github.com/QuantumNous/new-api/pkg/jsplugin"
vertexcore "github.com/QuantumNous/new-api/relay/channel/vertex"
)
type cachedAuth struct {
header, projectID string
expiresAt time.Time
}
var pluginAuthCache sync.Map
var acquireAccessToken = vertexcore.AcquireAccessToken
func resolveAuth(meta pluginruntime.AuthMeta, apiKey, proxy string) (map[string]any, error) {
typeName := strings.TrimSpace(meta.Type)
if typeName == "" || typeName == "none" || typeName == "api_key" {
return map[string]any{"authHeader": apiKey}, nil
}
cacheKey := apiKey + "\x00" + proxy
if value, ok := pluginAuthCache.Load(cacheKey); ok {
entry := value.(cachedAuth)
if time.Now().Before(entry.expiresAt) {
return map[string]any{"authHeader": entry.header, "projectId": entry.projectID}, nil
}
}
var credentials vertexcore.Credentials
if err := common.Unmarshal([]byte(apiKey), &credentials); err != nil {
return nil, fmt.Errorf("decode oauth2_jwt credentials: %w", err)
}
token, err := acquireAccessToken(credentials, proxy)
if err != nil {
return nil, err
}
entry := cachedAuth{header: "Bearer " + token, projectID: credentials.ProjectID, expiresAt: time.Now().Add(25 * time.Minute)}
pluginAuthCache.Store(cacheKey, entry)
return map[string]any{"authHeader": entry.header, "projectId": entry.projectID}, nil
}