package session
import (
"bytes"
"cmp"
"crypto/rand"
"encoding/base32"
"encoding/json"
"errors"
return b32.EncodeToString(b)
}
func RuntimeDir() string {
base := os.Getenv("XDG_RUNTIME_DIR")
if base == "" {
base = filepath.Join(os.TempDir(), fmt.Sprintf("tobi-%d", os.Getuid()))
} else {
base = filepath.Join(base, "tobi")
base := filepath.Join(os.TempDir(), fmt.Sprintf("tobi-%d", os.Getuid()))
if xdg := cmp.Or(os.Getenv("XDG_RUNTIME_DIR"), loginRuntimeDir()); xdg != "" {
base = filepath.Join(xdg, "tobi")
}
if fi, err := os.Lstat(base); err == nil && (fi.Mode()&os.ModeSymlink != 0 || int(fi.Sys().(*syscall.Stat_t).Uid) != os.Getuid()) {
_ = os.RemoveAll(base)
}
_ = os.Chmod(base, 0700)
return base
}
func loginRuntimeDir() string {
d := fmt.Sprintf("/run/user/%d", os.Getuid())
if fi, err := os.Stat(d); err == nil && fi.IsDir() && int(fi.Sys().(*syscall.Stat_t).Uid) == os.Getuid() {
return d
}
return ""
}
func SocketPath(id string) string {
if !ValidateID(id) {
return ""
}
604 unmodified lines
Expand all