feat: self-service worker enrollment bound to a user account

Let a signed-in user turn their own machine into a worker without the
shared token. The coordinator already binds a JWT-authenticated
registration to owner_id as untrusted; this adds the missing pieces.

userservice: long-lived worker keys (scimesh_wk_live_*, hash-at-rest)
with create/list/revoke and a public /worker-tokens/exchange that trades
a key for a short-lived JWT carrying the owner current role/verified.

python worker: SCIMESH_WORKER_KEY + SCIMESH_USERSERVICE_URL; a token
provider exchanges the key and refreshes the JWT proactively and on 401,
so a long-running worker survives token expiry. Static bearer token path
is unchanged.

coordinator UI: an "add your machine" page that mints a key and shows a
ready-to-run command, proxying key management to the userservice; the
dashboard gains an owner-scoped "my machines" section.

docs: how to run a worker from your account, plus the untrusted/quorum/
verified trust model.
This commit is contained in:
Efremenko Arhip
2026-07-27 16:11:07 +03:00
parent 172ff76fb8
commit 3a1461315f
34 changed files with 1829 additions and 78 deletions
+8
View File
@@ -7,6 +7,14 @@ var (
ErrEmailExists = errors.New("email already registered")
ErrUserNotFound = errors.New("user not found")
// ErrWorkerKeyNotFound is returned by WorkerKeyRepository when no live key
// matches (by id for revoke, by hash for exchange).
ErrWorkerKeyNotFound = errors.New("worker key not found")
// ErrInvalidWorkerKey is surfaced to the transport layer for a key that does
// not exchange (unknown, revoked, or owner gone). Deliberately opaque so a
// caller cannot distinguish the cases while probing.
ErrInvalidWorkerKey = errors.New("invalid worker key")
// Use-case errors surfaced to the transport layer.
//
// ErrInvalidCredentials is deliberately returned for both an unknown email
+19
View File
@@ -30,6 +30,25 @@ type UserRepository interface {
SetRole(ctx context.Context, id uuid.UUID, role domain.Role) error
}
// WorkerKeyRepository persists and looks up the long-lived worker keys a user
// creates to run a worker bound to their account. Implementations return the
// sentinel errors in errors.go so the use cases stay free of SQL types.
type WorkerKeyRepository interface {
// Insert stores a freshly minted key.
Insert(ctx context.Context, k *domain.WorkerKey) error
// ListByUser returns a user's live (non-revoked) keys, newest first.
ListByUser(ctx context.Context, userID uuid.UUID) ([]*domain.WorkerKey, error)
// GetActiveByHash returns the non-revoked key with the given hash, or
// ErrWorkerKeyNotFound.
GetActiveByHash(ctx context.Context, tokenHash string) (*domain.WorkerKey, error)
// Revoke retires a key the user owns, returning ErrWorkerKeyNotFound when no
// live key with that id belongs to the user.
Revoke(ctx context.Context, id, userID uuid.UUID) error
// TouchLastUsed records a successful exchange. Best-effort: a failure here
// must not fail the exchange itself.
TouchLastUsed(ctx context.Context, id uuid.UUID) error
}
// PasswordHasher hashes and verifies passwords. The bcrypt adapter satisfies it.
type PasswordHasher interface {
Hash(password string) (string, error)
+112
View File
@@ -0,0 +1,112 @@
package usecase
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"github.com/emil28092005/SciMesh/users/internal/domain"
)
// CreateWorkerKey mints a long-lived worker key for a user and returns the
// one-time plaintext to show once.
type CreateWorkerKey struct {
keys WorkerKeyRepository
clock Clock
}
func NewCreateWorkerKey(keys WorkerKeyRepository, clock Clock) *CreateWorkerKey {
return &CreateWorkerKey{keys: keys, clock: clock}
}
// Execute returns the stored key (hash only) and the plaintext secret. The
// secret is never persisted, so this is the sole moment it can be surfaced.
func (uc *CreateWorkerKey) Execute(ctx context.Context, userID uuid.UUID, name string) (*domain.WorkerKey, string, error) {
key, raw, err := domain.NewWorkerKey(userID, name, uc.clock.Now())
if err != nil {
return nil, "", err
}
if err := uc.keys.Insert(ctx, key); err != nil {
return nil, "", err
}
return key, raw, nil
}
// ListWorkerKeys returns a user's live keys for display and management.
type ListWorkerKeys struct {
keys WorkerKeyRepository
}
func NewListWorkerKeys(keys WorkerKeyRepository) *ListWorkerKeys {
return &ListWorkerKeys{keys: keys}
}
func (uc *ListWorkerKeys) Execute(ctx context.Context, userID uuid.UUID) ([]*domain.WorkerKey, error) {
return uc.keys.ListByUser(ctx, userID)
}
// RevokeWorkerKey retires one of the caller's keys.
type RevokeWorkerKey struct {
keys WorkerKeyRepository
}
func NewRevokeWorkerKey(keys WorkerKeyRepository) *RevokeWorkerKey {
return &RevokeWorkerKey{keys: keys}
}
func (uc *RevokeWorkerKey) Execute(ctx context.Context, userID, id uuid.UUID) error {
return uc.keys.Revoke(ctx, id, userID)
}
// ExchangeWorkerKey trades a valid worker key for a short-lived JWT. The JWT
// carries the owner's current role and verified flag, so a worker that refreshes
// after an admin verifies the owner picks up the upgraded trust on its next
// registration.
type ExchangeWorkerKey struct {
keys WorkerKeyRepository
users UserRepository
tokens TokenIssuer
ttl time.Duration
}
func NewExchangeWorkerKey(keys WorkerKeyRepository, users UserRepository, tokens TokenIssuer, ttl time.Duration) *ExchangeWorkerKey {
return &ExchangeWorkerKey{keys: keys, users: users, tokens: tokens, ttl: ttl}
}
// Execute returns a signed token and its lifetime in seconds. Every failure to
// resolve the key to a usable owner collapses to ErrInvalidWorkerKey so a caller
// cannot tell an unknown key from a revoked one or a deleted owner.
func (uc *ExchangeWorkerKey) Execute(ctx context.Context, rawKey string) (string, int, error) {
if rawKey == "" {
return "", 0, ErrInvalidWorkerKey
}
key, err := uc.keys.GetActiveByHash(ctx, domain.HashWorkerKey(rawKey))
if err != nil {
if errors.Is(err, ErrWorkerKeyNotFound) {
return "", 0, ErrInvalidWorkerKey
}
return "", 0, err
}
u, err := uc.users.GetByID(ctx, key.UserID)
if err != nil {
if errors.Is(err, ErrUserNotFound) {
return "", 0, ErrInvalidWorkerKey
}
return "", 0, err
}
token, err := uc.tokens.Issue(u)
if err != nil {
return "", 0, err
}
// Best-effort: a failed timestamp update must not sink an otherwise valid
// exchange the worker depends on to keep running.
_ = uc.keys.TouchLastUsed(ctx, key.ID)
return token, int(uc.ttl.Seconds()), nil
}
+186
View File
@@ -0,0 +1,186 @@
package usecase_test
import (
"context"
"errors"
"testing"
"time"
"github.com/google/uuid"
"github.com/emil28092005/SciMesh/users/internal/auth"
"github.com/emil28092005/SciMesh/users/internal/domain"
"github.com/emil28092005/SciMesh/users/internal/memstore"
"github.com/emil28092005/SciMesh/users/internal/usecase"
)
// fakeKeyRepo is an in-memory WorkerKeyRepository for the use-case tests.
type fakeKeyRepo struct {
byHash map[string]*domain.WorkerKey
byID map[uuid.UUID]*domain.WorkerKey
touched []uuid.UUID
}
func newFakeKeyRepo() *fakeKeyRepo {
return &fakeKeyRepo{byHash: map[string]*domain.WorkerKey{}, byID: map[uuid.UUID]*domain.WorkerKey{}}
}
func (r *fakeKeyRepo) Insert(_ context.Context, k *domain.WorkerKey) error {
r.byHash[k.TokenHash] = k
r.byID[k.ID] = k
return nil
}
func (r *fakeKeyRepo) ListByUser(_ context.Context, userID uuid.UUID) ([]*domain.WorkerKey, error) {
var out []*domain.WorkerKey
for _, k := range r.byID {
if k.UserID == userID && !k.Revoked() {
out = append(out, k)
}
}
return out, nil
}
func (r *fakeKeyRepo) GetActiveByHash(_ context.Context, hash string) (*domain.WorkerKey, error) {
k, ok := r.byHash[hash]
if !ok || k.Revoked() {
return nil, usecase.ErrWorkerKeyNotFound
}
return k, nil
}
func (r *fakeKeyRepo) Revoke(_ context.Context, id, userID uuid.UUID) error {
k, ok := r.byID[id]
if !ok || k.UserID != userID || k.Revoked() {
return usecase.ErrWorkerKeyNotFound
}
now := time.Now()
k.RevokedAt = &now
return nil
}
func (r *fakeKeyRepo) TouchLastUsed(_ context.Context, id uuid.UUID) error {
r.touched = append(r.touched, id)
return nil
}
func newKeyFixtures(t *testing.T) (*usecase.CreateWorkerKey, *usecase.ExchangeWorkerKey, *usecase.RevokeWorkerKey, *usecase.ListWorkerKeys, *fakeKeyRepo, *domain.User) {
t.Helper()
users := memstore.NewUserRepo()
hasher := auth.NewHasher(4)
clk := memstore.Clock{T: time.Date(2026, 7, 26, 0, 0, 0, 0, time.UTC)}
issuer := auth.NewIssuer(secret, time.Hour, nil)
keys := newFakeKeyRepo()
u, err := usecase.NewRegister(users, hasher, clk).Execute(context.Background(), "worker@example.com", "password123")
if err != nil {
t.Fatalf("seed user: %v", err)
}
return usecase.NewCreateWorkerKey(keys, clk),
usecase.NewExchangeWorkerKey(keys, users, issuer, time.Hour),
usecase.NewRevokeWorkerKey(keys),
usecase.NewListWorkerKeys(keys),
keys, u
}
func TestCreateAndExchangeWorkerKey(t *testing.T) {
create, exchange, _, _, keys, u := newKeyFixtures(t)
ctx := context.Background()
key, raw, err := create.Execute(ctx, u.ID, "home-desktop")
if err != nil {
t.Fatalf("create: %v", err)
}
if key.Name != "home-desktop" || raw == "" {
t.Fatalf("unexpected key %+v raw=%q", key, raw)
}
token, expiresIn, err := exchange.Execute(ctx, raw)
if err != nil {
t.Fatalf("exchange: %v", err)
}
if expiresIn != int((time.Hour).Seconds()) {
t.Errorf("expires_in = %d, want 3600", expiresIn)
}
claims, err := auth.NewIssuer(secret, time.Hour, nil).Verify(token)
if err != nil {
t.Fatalf("issued token does not verify: %v", err)
}
if claims.Subject != u.ID.String() {
t.Errorf("token sub = %q, want owner %q", claims.Subject, u.ID)
}
if len(keys.touched) != 1 || keys.touched[0] != key.ID {
t.Errorf("exchange did not record last-used, touched=%v", keys.touched)
}
}
func TestExchangeUnknownKeyIsInvalid(t *testing.T) {
_, exchange, _, _, _, _ := newKeyFixtures(t)
if _, _, err := exchange.Execute(context.Background(), "scimesh_wk_live_nope"); !errors.Is(err, usecase.ErrInvalidWorkerKey) {
t.Errorf("got %v, want ErrInvalidWorkerKey", err)
}
}
func TestExchangeEmptyKeyIsInvalid(t *testing.T) {
_, exchange, _, _, _, _ := newKeyFixtures(t)
if _, _, err := exchange.Execute(context.Background(), ""); !errors.Is(err, usecase.ErrInvalidWorkerKey) {
t.Errorf("got %v, want ErrInvalidWorkerKey", err)
}
}
func TestExchangeRevokedKeyIsInvalid(t *testing.T) {
create, exchange, revoke, _, _, u := newKeyFixtures(t)
ctx := context.Background()
key, raw, err := create.Execute(ctx, u.ID, "laptop")
if err != nil {
t.Fatal(err)
}
if err := revoke.Execute(ctx, u.ID, key.ID); err != nil {
t.Fatalf("revoke: %v", err)
}
if _, _, err := exchange.Execute(ctx, raw); !errors.Is(err, usecase.ErrInvalidWorkerKey) {
t.Errorf("revoked key still exchanges: %v", err)
}
}
func TestRevokeIsScopedToOwner(t *testing.T) {
create, _, revoke, _, _, u := newKeyFixtures(t)
ctx := context.Background()
key, _, err := create.Execute(ctx, u.ID, "laptop")
if err != nil {
t.Fatal(err)
}
// A different user must not be able to revoke this key.
if err := revoke.Execute(ctx, uuid.New(), key.ID); !errors.Is(err, usecase.ErrWorkerKeyNotFound) {
t.Errorf("cross-owner revoke returned %v, want ErrWorkerKeyNotFound", err)
}
}
func TestListReturnsOnlyLiveKeys(t *testing.T) {
create, _, revoke, list, _, u := newKeyFixtures(t)
ctx := context.Background()
live, _, err := create.Execute(ctx, u.ID, "keep")
if err != nil {
t.Fatal(err)
}
dead, _, err := create.Execute(ctx, u.ID, "drop")
if err != nil {
t.Fatal(err)
}
if err := revoke.Execute(ctx, u.ID, dead.ID); err != nil {
t.Fatal(err)
}
got, err := list.Execute(ctx, u.ID)
if err != nil {
t.Fatalf("list: %v", err)
}
if len(got) != 1 || got[0].ID != live.ID {
t.Errorf("list = %d keys, want only the live one", len(got))
}
}