Files
mesh-contracts/identity-and-access/go/clients/meshnodeenrollerclient/enrolment.go
T

39 lines
1.3 KiB
Go

package meshnodeenrollerclient
import (
"fmt"
"strconv"
"strings"
"time"
)
const (
// DefaultTokenLife is a token's life when issue names none; MostTokenLife the longest it may name (ADR 0587 §4).
DefaultTokenLife = time.Hour
MostTokenLife = 24 * time.Hour
// UserPrefix is the bus user a joining machine dials as, `enrol.<node>` (ADR 0587 §4).
UserPrefix = "enrol."
)
// User is the bus user a joining machine dials as.
func User(node string) string { return UserPrefix + node }
// IssuedID is the id of the enrolment-issued event of node's enrolment issued at issued:
// `enrolment-issued.<node>.<issue time in Unix nanoseconds>`, what enrolment-expired names.
func IssuedID(node string, issued time.Time) string {
return "enrolment-issued." + node + "." + strconv.FormatInt(issued.UnixNano(), 10)
}
// TokenLife is the life an issue's `expires` asks for: a duration, one hour when empty, at most MostTokenLife.
func TokenLife(expires string) (time.Duration, error) {
if strings.TrimSpace(expires) == "" {
return DefaultTokenLife, nil
}
d, err := time.ParseDuration(strings.TrimSpace(expires))
if err != nil || d <= 0 || d > MostTokenLife {
return 0, fmt.Errorf("meshnodeenrollerclient: a token lasts a duration up to %s, such as 1h, not %q", MostTokenLife, expires)
}
return d, nil
}