Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions server/config/picoclaw_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package config

import (
"os"
"os/user"
"path/filepath"
"strings"
"sync"

"gopkg.in/yaml.v3"
)

const (
Expand All @@ -21,10 +24,18 @@ func GetPicoclawInternalToken() (string, error) {
picoclawInternalToken.mu.Lock()
defer picoclawInternalToken.mu.Unlock()

// Always try to read the live pico token from security.yml first
if token := readPicoclawSecurityToken(); token != "" {
picoclawInternalToken.value = token
return token, nil
}

// Fall back to cached value
if picoclawInternalToken.value != "" {
return picoclawInternalToken.value, nil
}

// Fall back to the legacy token file
if token, err := readPicoclawInternalToken(); err == nil {
if token != "" {
picoclawInternalToken.value = token
Expand Down Expand Up @@ -59,3 +70,44 @@ func readPicoclawInternalToken() (string, error) {

return strings.TrimSpace(string(data)), nil
}

type picoclawSecurityYAML struct {
ChannelList map[string]struct {
Token string `yaml:"token,omitempty"`
Settings *struct {
Token string `yaml:"token,omitempty"`
} `yaml:"settings,omitempty"`
} `yaml:"channel_list,omitempty"`
}

func readPicoclawSecurityToken() string {
home := os.Getenv("PICOCLAW_HOME")
if home == "" {
u, err := user.Current()
if err != nil {
return ""
}
home = filepath.Join(u.HomeDir, ".picoclaw")
}

data, err := os.ReadFile(filepath.Join(home, ".security.yml"))
if err != nil {
return ""
}

var sec picoclawSecurityYAML
if err := yaml.Unmarshal(data, &sec); err != nil {
return ""
}

entry, ok := sec.ChannelList["pico"]
if !ok {
return ""
}
if entry.Settings != nil {
if t := strings.TrimSpace(entry.Settings.Token); t != "" {
return t
}
}
return strings.TrimSpace(entry.Token)
}