86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
package pi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/aoagents/agent-orchestrator/backend/internal/adapters/agent/authprobe"
|
|
"github.com/aoagents/agent-orchestrator/backend/internal/ports"
|
|
)
|
|
|
|
var _ ports.AgentAuthChecker = (*Plugin)(nil)
|
|
|
|
// AuthStatus returns the plugin's local authentication status.
|
|
func (p *Plugin) AuthStatus(ctx context.Context) (ports.AgentAuthStatus, error) {
|
|
binary, err := p.ResolveBinary(ctx)
|
|
if err != nil {
|
|
return ports.AgentAuthStatusUnknown, err
|
|
}
|
|
if status, ok, err := piLocalAuthStatus(ctx); err != nil {
|
|
return ports.AgentAuthStatusUnknown, err
|
|
} else if ok {
|
|
return status, nil
|
|
}
|
|
return authprobe.CLIStatus(ctx, binary, nil)
|
|
}
|
|
|
|
func piLocalAuthStatus(ctx context.Context) (ports.AgentAuthStatus, bool, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return ports.AgentAuthStatusUnknown, false, err
|
|
}
|
|
configDir, ok := piConfigDir()
|
|
if !ok {
|
|
return ports.AgentAuthStatusUnknown, false, nil
|
|
}
|
|
return piAuthJSONStatus(filepath.Join(configDir, "auth.json"))
|
|
}
|
|
|
|
func piConfigDir() (string, bool) {
|
|
if configDir := strings.TrimSpace(os.Getenv("PI_CODING_AGENT_DIR")); configDir != "" {
|
|
return configDir, true
|
|
}
|
|
home, err := os.UserHomeDir()
|
|
if err != nil || home == "" {
|
|
return "", false
|
|
}
|
|
return filepath.Join(home, ".pi", "agent"), true
|
|
}
|
|
|
|
type piAuthEntry struct {
|
|
Type string `json:"type"`
|
|
Key string `json:"key"`
|
|
}
|
|
|
|
func piAuthJSONStatus(path string) (ports.AgentAuthStatus, bool, error) {
|
|
data, err := os.ReadFile(path)
|
|
if os.IsNotExist(err) {
|
|
return ports.AgentAuthStatusUnknown, false, nil
|
|
}
|
|
if err != nil {
|
|
return ports.AgentAuthStatusUnknown, false, err
|
|
}
|
|
if strings.TrimSpace(string(data)) == "" {
|
|
return ports.AgentAuthStatusUnknown, false, nil
|
|
}
|
|
|
|
var entries map[string]piAuthEntry
|
|
if err := json.Unmarshal(data, &entries); err != nil {
|
|
return ports.AgentAuthStatusUnknown, false, err
|
|
}
|
|
if len(entries) == 0 {
|
|
return ports.AgentAuthStatusUnauthorized, true, nil
|
|
}
|
|
for provider, entry := range entries {
|
|
if strings.TrimSpace(provider) == "" {
|
|
continue
|
|
}
|
|
if strings.TrimSpace(entry.Key) != "" {
|
|
return ports.AgentAuthStatusAuthorized, true, nil
|
|
}
|
|
}
|
|
return ports.AgentAuthStatusUnauthorized, true, nil
|
|
}
|