feat(assessment): assign the demonstration category and keep result history

This commit is contained in:
shira committed 2026-10-09 00:35:13 +03:00
1 parent b3c6dc870b
commit 8277133712
61 files changed
+4338 -268

No files matched your search

@@ -31,6 +31,7 @@ const (
labelAnswerFingerprint = "lincodex/assessment/answer-fingerprint/v1"
labelSignalSession = "lincodex/assessment/signal-session/v1"
labelSignalNetwork = "lincodex/assessment/signal-network/v1"
labelResultCursor = "lincodex/assessment/result-history-cursor/v1"
)
// Keyring computes the keyed digests of the anonymous identity operations:
@@ -49,6 +50,7 @@ type Keyring struct {
answerKey []byte
signalSessionKey []byte
signalNetworkKey []byte
resultCursorKey []byte
}
// NewKeyring derives the purpose keys from secret.
@@ -67,6 +69,7 @@ func NewKeyring(secret []byte) (*Keyring, error) {
answerKey: derive(secret, labelAnswerFingerprint),
signalSessionKey: derive(secret, labelSignalSession),
signalNetworkKey: derive(secret, labelSignalNetwork),
resultCursorKey: derive(secret, labelResultCursor),
}, nil
}
@@ -103,6 +106,17 @@ func (keyring *Keyring) IdempotencyScope(operation, key, normalizedEmail string)
return [outbound.IdempotencyDigestSize]byte(mac.Sum(nil))
}
// ResultHistoryCursorSignature signs the position of a result history page for
// one candidate under a key of its own, so a cursor can never be replayed as an
// idempotency scope or the other way round.
func (keyring *Keyring) ResultHistoryCursorSignature(position, candidateID string) [outbound.IdempotencyDigestSize]byte {
mac := hmac.New(sha256.New, keyring.resultCursorKey)
mac.Write([]byte(position))
mac.Write([]byte{0})
mac.Write([]byte(candidateID))
return [outbound.IdempotencyDigestSize]byte(mac.Sum(nil))
}
// Fingerprint identifies a request by its canonical body.
func (keyring *Keyring) Fingerprint(canonicalBody []byte) [outbound.IdempotencyDigestSize]byte {
mac := hmac.New(sha256.New, keyring.fingerprintKey)
@@ -3,6 +3,7 @@ package identity
import (
"bytes"
"crypto/sha256"
"strings"
"testing"
"github.com/google/uuid"
@@ -123,3 +124,22 @@ func TestAssessmentSessionAndNetworkDigestsAreBoundToTheAttempt(t *testing.T) {
t.Fatal("session and network digests share a key")
}
}
func TestResultHistoryCursorKeyIsSeparateFromIdempotencyAndBoundToTheCandidate(t *testing.T) {
keyring, err := NewKeyring([]byte(strings.Repeat("k", MinKeySecretBytes)))
if err != nil {
t.Fatalf("NewKeyring() error = %v", err)
}
signature := keyring.ResultHistoryCursorSignature("position", "candidate-a")
if signature != keyring.ResultHistoryCursorSignature("position", "candidate-a") {
t.Fatal("the cursor signature is not deterministic")
}
if signature == keyring.ResultHistoryCursorSignature("position", "candidate-b") ||
signature == keyring.ResultHistoryCursorSignature("other", "candidate-a") {
t.Fatal("the cursor signature does not depend on the position and the candidate")
}
if signature == keyring.IdempotencyScope("position", "", "candidate-a") ||
signature == keyring.IdempotencyScope("assessment.result-history.cursor.v1", "position", "candidate-a") {
t.Fatal("the cursor signature shares a key with the idempotency scope")
}
}