mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-19 16:29:45 +08:00
- Disambiguate Claude credential filenames using organization and account UUID hashes to keep multiple organizations distinct. - Migrate legacy Claude credentials during login and save flows while preserving existing metadata and deleting obsolete files. - Introduce `WithAuthCreationIntent` context policy across token stores to allow creating missing disabled credentials during login and migration. - Preserve existing `disabled` status during auth metadata merges when not explicitly specified. Closes: #5709
116 lines
3.5 KiB
Go
116 lines
3.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
claudeauth "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/claude"
|
|
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
|
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
|
)
|
|
|
|
// Manager aggregates authenticators and coordinates persistence via a token store.
|
|
type Manager struct {
|
|
authenticators map[string]Authenticator
|
|
store coreauth.Store
|
|
}
|
|
|
|
// NewManager constructs a manager with the provided token store and authenticators.
|
|
// If store is nil, the caller must set it later using SetStore.
|
|
func NewManager(store coreauth.Store, authenticators ...Authenticator) *Manager {
|
|
mgr := &Manager{
|
|
authenticators: make(map[string]Authenticator),
|
|
store: store,
|
|
}
|
|
for i := range authenticators {
|
|
mgr.Register(authenticators[i])
|
|
}
|
|
return mgr
|
|
}
|
|
|
|
// Register adds or replaces an authenticator keyed by its provider identifier.
|
|
func (m *Manager) Register(a Authenticator) {
|
|
if a == nil {
|
|
return
|
|
}
|
|
if m.authenticators == nil {
|
|
m.authenticators = make(map[string]Authenticator)
|
|
}
|
|
m.authenticators[a.Provider()] = a
|
|
}
|
|
|
|
// SetStore updates the token store used for persistence.
|
|
func (m *Manager) SetStore(store coreauth.Store) {
|
|
m.store = store
|
|
}
|
|
|
|
// Login executes the provider login flow and persists the resulting auth record.
|
|
func (m *Manager) Login(ctx context.Context, provider string, cfg *config.Config, opts *LoginOptions) (*coreauth.Auth, string, error) {
|
|
auth, ok := m.authenticators[provider]
|
|
if !ok {
|
|
return nil, "", fmt.Errorf("cliproxy auth: authenticator %s not registered", provider)
|
|
}
|
|
|
|
record, err := auth.Login(ctx, cfg, opts)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
if record == nil {
|
|
return nil, "", fmt.Errorf("cliproxy auth: authenticator %s returned nil record", provider)
|
|
}
|
|
|
|
if m.store == nil {
|
|
return record, "", nil
|
|
}
|
|
|
|
if cfg != nil {
|
|
if dirSetter, ok := m.store.(interface{ SetBaseDir(string) }); ok {
|
|
dirSetter.SetBaseDir(cfg.AuthDir)
|
|
}
|
|
if strings.TrimSpace(cfg.AuthDir) != "" {
|
|
targetFile := record.FileName
|
|
if targetFile == "" {
|
|
targetFile = record.ID
|
|
}
|
|
if targetFile != "" {
|
|
fullPath := filepath.Join(cfg.AuthDir, targetFile)
|
|
if raw, errRead := os.ReadFile(fullPath); errRead == nil && len(raw) > 0 {
|
|
var existingMap map[string]any
|
|
if errUnmarshal := json.Unmarshal(raw, &existingMap); errUnmarshal == nil && len(existingMap) > 0 {
|
|
coreauth.MergeExistingAuthMetadata(record, existingMap)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
legacyClaudeCredential, errLegacy := claudeauth.FindMatchingLegacyCredential(ctx, m.store, record)
|
|
if errLegacy != nil {
|
|
return record, "", errLegacy
|
|
}
|
|
if legacyClaudeCredential != nil {
|
|
coreauth.MergeExistingAuthMetadata(record, legacyClaudeCredential.Metadata)
|
|
}
|
|
|
|
savedPath, err := m.store.Save(coreauth.WithAuthCreationIntent(ctx), record)
|
|
if err != nil {
|
|
return record, "", err
|
|
}
|
|
if legacyClaudeCredential != nil {
|
|
if strings.TrimSpace(savedPath) == "" {
|
|
return record, "", fmt.Errorf("cliproxy auth: canonical Claude credential was not persisted; legacy credential retained")
|
|
}
|
|
legacyID := strings.TrimSpace(legacyClaudeCredential.ID)
|
|
if legacyID == "" {
|
|
legacyID = strings.TrimSpace(legacyClaudeCredential.FileName)
|
|
}
|
|
if errDelete := m.store.Delete(ctx, legacyID); errDelete != nil {
|
|
return record, savedPath, fmt.Errorf("cliproxy auth: canonical Claude credential saved but legacy credential cleanup failed: %w", errDelete)
|
|
}
|
|
}
|
|
return record, savedPath, nil
|
|
}
|