75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package grok
|
|
|
|
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 := grokLocalAuthStatus(ctx); err != nil {
|
|
return ports.AgentAuthStatusUnknown, err
|
|
} else if ok {
|
|
return status, nil
|
|
}
|
|
return authprobe.CLIStatus(ctx, binary, nil)
|
|
}
|
|
|
|
func grokLocalAuthStatus(ctx context.Context) (ports.AgentAuthStatus, bool, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return ports.AgentAuthStatusUnknown, false, err
|
|
}
|
|
if strings.TrimSpace(os.Getenv("GROK_API_KEY")) != "" || strings.TrimSpace(os.Getenv("XAI_API_KEY")) != "" {
|
|
return ports.AgentAuthStatusAuthorized, true, nil
|
|
}
|
|
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return ports.AgentAuthStatusUnknown, false, err
|
|
}
|
|
if home == "" {
|
|
return ports.AgentAuthStatusUnknown, false, nil
|
|
}
|
|
path := filepath.Join(home, ".grok", "auth.json")
|
|
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.AgentAuthStatusUnauthorized, true, nil
|
|
}
|
|
|
|
var entries map[string]json.RawMessage
|
|
if err := json.Unmarshal(data, &entries); err != nil {
|
|
return ports.AgentAuthStatusUnknown, false, err
|
|
}
|
|
if len(entries) == 0 {
|
|
return ports.AgentAuthStatusUnauthorized, true, nil
|
|
}
|
|
for key, value := range entries {
|
|
if strings.TrimSpace(key) == "" {
|
|
continue
|
|
}
|
|
trimmed := strings.TrimSpace(string(value))
|
|
if trimmed != "" && trimmed != "null" && trimmed != "{}" {
|
|
return ports.AgentAuthStatusAuthorized, true, nil
|
|
}
|
|
}
|
|
return ports.AgentAuthStatusUnauthorized, true, nil
|
|
}
|