feat: publish v1.0.0-rc.36-custom.1
Publish Docker image (Multi-arch) / Build & push (amd64) (push) Failing after 9m20s
Release (Linux, macOS, Windows) / Linux Release (push) Failing after 55m7s
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Canceled after 0s
Release (Linux, macOS, Windows) / macOS Release (push) Canceled after 0s
Release (Linux, macOS, Windows) / Windows Release (push) Canceled after 0s
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Canceled after 0s
Publish Docker image (Multi-arch) / Build & push (amd64) (push) Failing after 9m20s
Release (Linux, macOS, Windows) / Linux Release (push) Failing after 55m7s
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Canceled after 0s
Release (Linux, macOS, Windows) / macOS Release (push) Canceled after 0s
Release (Linux, macOS, Windows) / Windows Release (push) Canceled after 0s
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Canceled after 0s
This commit is contained in:
commit
bb7968b612
2381 files changed
+551494
No files matched your search
@@ -0,0 +1,45 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user