Files
OpenFlare/internal/apps/openflare/agent/registration.go
T
ArthurKoba 72d67c80f2 fix(agent): make discovery enrollment compatible and idempotent (#53)
* fix(agent): make discovery enrollment compatible and idempotent

* fix(agent): confirm enrollment persistence and document token aliases
2026-11-07 21:29:47 +08:00

26 lines
875 B
Go

// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package agent
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
)
const registrationKeyBytes = 32
// discoveryIdentity derives separate public identity and private credential from a random retry secret.
// Public node IDs cannot be used as registration keys to recover credentials.
func discoveryIdentity(key string) (string, string, error) {
decoded, err := hex.DecodeString(key)
if err != nil || len(decoded) != registrationKeyBytes {
return "", "", errors.New(errRegistrationKeyInvalid)
}
nodeHash := sha256.Sum256(append([]byte("openflare:discovery:node:v1:"), decoded...))
tokenHash := sha256.Sum256(append([]byte("openflare:discovery:token:v1:"), decoded...))
return "node-" + base64.RawURLEncoding.EncodeToString(nodeHash[:]), hex.EncodeToString(tokenHash[:]), nil
}