Add six-digit verification and typed or drawn contract signatures
11 files changed,
+330
−21
docs/superpowers/specs/2026-10-26-live-contract-demo.md+25 −1internal/docs/contract_actions.go+21 −7internal/docs/contract_concurrency_test.go+45 −5internal/docs/contract_demo.go+7 −1internal/docs/contract_demo_test.go+3 −3internal/docs/contract_signature.go+105 −0internal/docs/contract_signature_test.go+47 −0internal/docs/contract_view.go+4 −1internal/docs/views/contract-signature.js+58 −0internal/docs/views/contract.css+2 −0internal/docs/views/contract.html+13 −3
CI passed — run details
diff --git a/docs/superpowers/specs/2026-10-26-live-contract-demo.md b/docs/superpowers/specs/2026-10-26-live-contract-demo.md| index 218e1e6..8c40c24 100644 |
| --- a/docs/superpowers/specs/2026-10-26-live-contract-demo.md |
| +++ b/docs/superpowers/specs/2026-10-26-live-contract-demo.md |
| @@ -102,7 +102,8 @@ verification code by real configured mail to that address, not a link that |
| grants document access. Store only its hash, bound to agreement/digest, with |
| 15-minute expiry and at most five guesses. A GET never consumes it. Successful |
| verification is an own-origin POST, then a separate consent POST signs. |
| -Codes carry 128 random bits, rendered as 32 hexadecimal characters. Verification |
| +New codes are six decimal digits; existing 32-character challenges remain valid |
| +until replaced. See the follow-up below for generation and lifetime limits. Verification |
| success lasts no longer than the original 15-minute challenge expiry, belongs |
| to the same admitted issuer/agreement/digest, and is invalidated by resend. |
| Attempt counts, challenge generation, verification and resend update the same |
| @@ -131,3 +132,26 @@ evidence refusal; restart persistence. Browser demonstration must use real |
| Fichas login and selected live record, real PDF, real email and consent. |
| Exercise no-JS forms and narrow screen. Run independent whole-branch review |
| and each repo's gate. Canary validation precedes enabling live source release. |
| + |
| +## Six-digit codes and signature marks (2026-10-26 follow-up) |
| + |
| +Paul requests six-digit email codes plus typed or drawn signatures. Generate |
| +uniform six-digit strings using crypto/rand (leading zeroes preserved), keep |
| +15-minute expiry, five guesses per code and one resend per minute, and add a |
| +20-guess lifetime ceiling per agreement which resends never reset. Existing |
| +32-character challenges remain usable until replaced; new challenges carry |
| +an explicit length of six. These codes still require the admitted owner session. |
| + |
| +The final consent form requires a signer name (trimmed, valid UTF-8, max 120 |
| +runes, no control characters). Typed is the no-JavaScript default. Drawing is |
| +progressive enhancement with a pointer canvas, clear control, and a typed |
| +alternative. Store only bounded integer point strokes, never client SVG/HTML: |
| +600 by 200 logical space, max 64 strokes and 2048 total points, each stroke |
| +at least two points and overall visible movement. Server validates bounds |
| +and method, and commits the name, method and strokes with consent, timestamp |
| +and frozen PDF digest in the existing encrypted optimistic transaction. |
| + |
| +Show the saved mark on the completed page and include it in downloadable |
| +signing evidence. The original reviewed PDF remains unchanged. Existing |
| +completed agreements remain valid with their original consent and no mark. |
| +No certificate signature or handwriting identity-verification claim is added. |
diff --git a/internal/docs/contract_actions.go b/internal/docs/contract_actions.go| index eeddaa6..cafe9c5 100644 |
| --- a/internal/docs/contract_actions.go |
| +++ b/internal/docs/contract_actions.go |
| @@ -97,7 +97,11 @@ func demoPreview(s *contractState) error { |
| } |
| func (d *contractDemo) action(w http.ResponseWriter, r *http.Request) { |
| contractHeaders(w) |
| - r.Body = http.MaxBytesReader(w, r.Body, 64<<10) |
| + limit := int64(64 << 10) |
| + if strings.HasSuffix(r.URL.Path, "/sign") { |
| + limit = 160 << 10 |
| + } // Percent-encoded bounded strokes expand on the wire. |
| + r.Body = http.MaxBytesReader(w, r.Body, limit) |
| if r.ParseForm() != nil { |
| d.fail(w, r) |
| return |
| @@ -146,22 +150,26 @@ func (d *contractDemo) action(w http.ResponseWriter, r *http.Request) { |
| s.Stage = "issued" |
| s.Issued = now |
| expires = now + 7*86400 |
| - sendCode = contractRandom(16) |
| + sendCode = contractCode() |
| + s.CodeLength = 6 |
| setDemoChallenge(&s, sendCode, now) |
| case "resend": |
| - if s.Stage != "issued" || now >= s.Issued+86400 || now-s.LastSent < 60 { |
| + if s.Stage != "issued" || now >= s.Issued+86400 || now-s.LastSent < 60 || s.TotalAttempts >= 20 { |
| d.fail(w, r) |
| return |
| } |
| - sendCode = contractRandom(16) |
| + sendCode = contractCode() |
| + s.CodeLength = 6 |
| setDemoChallenge(&s, sendCode, now) |
| case "verify": |
| - if s.Stage != "issued" || now >= s.Issued+86400 || s.ChallengeExpires <= now || s.Attempts >= 5 || s.Verified != 0 { |
| + if s.Stage != "issued" || now >= s.Issued+86400 || s.ChallengeExpires <= now || s.Attempts >= 5 || s.TotalAttempts >= 20 || s.Verified != 0 { |
| d.fail(w, r) |
| return |
| } |
| s.Attempts++ |
| - if contractHash(strings.ToLower(strings.TrimSpace(r.PostForm.Get("code")))+":"+row.ID+":"+s.Digest) == s.ChallengeHash { |
| + s.TotalAttempts++ |
| + code := strings.ToLower(strings.TrimSpace(r.PostForm.Get("code"))) |
| + if validContractCode(code, s) && contractHash(code+":"+row.ID+":"+s.Digest) == s.ChallengeHash { |
| s.Verified = now |
| s.ChallengeHash = "" |
| } |
| @@ -170,6 +178,12 @@ func (d *contractDemo) action(w http.ResponseWriter, r *http.Request) { |
| d.fail(w, r) |
| return |
| } |
| + mark, err := parseContractSignature(r.PostForm) |
| + if err != nil { |
| + d.page(w, r, row, s, "Enter your name and type or draw a signature before signing.") |
| + return |
| + } |
| + s.Signature = mark |
| s.Stage = "completed" |
| s.Signed = now |
| s.Consent = contractConsent |
| @@ -236,7 +250,7 @@ func (d *contractDemo) evidence(w http.ResponseWriter, r *http.Request) { |
| d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| - evidence := map[string]any{"version": 1, "agreement": row.ID, "title": s.Title, "sha256": s.Digest, "signer": s.Address, "issued_at": time.Unix(s.Issued, 0).UTC(), "verified_at": time.Unix(s.Verified, 0).UTC(), "signed_at": time.Unix(s.Signed, 0).UTC(), "consent_version": 1, "consent": s.Consent, "source": s.Release.Source, "source_card": s.Release.Card, "source_revision": s.Release.Revision, "release": s.Release.ID, "assurance": "Email address control and affirmative issuer-only electronic acceptance. No PDF certificate signature."} |
| + evidence := map[string]any{"signature": s.Signature, "version": 1, "agreement": row.ID, "title": s.Title, "sha256": s.Digest, "signer": s.Address, "issued_at": time.Unix(s.Issued, 0).UTC(), "verified_at": time.Unix(s.Verified, 0).UTC(), "signed_at": time.Unix(s.Signed, 0).UTC(), "consent_version": 1, "consent": s.Consent, "source": s.Release.Source, "source_card": s.Release.Card, "source_revision": s.Release.Revision, "release": s.Release.ID, "assurance": "Email address control and affirmative issuer-only electronic acceptance. No PDF certificate signature."} |
| raw, e := json.Marshal(evidence) |
| if e != nil { |
| d.fail(w, r) |
diff --git a/internal/docs/contract_concurrency_test.go b/internal/docs/contract_concurrency_test.go| index 3705a93..f3a20fa 100644 |
| --- a/internal/docs/contract_concurrency_test.go |
| +++ b/internal/docs/contract_concurrency_test.go |
| @@ -78,7 +78,43 @@ func TestContractChallengeRacesExpiryAndRestart(t *testing.T) { |
| } |
| now := time.Now().Unix() |
| issued := func() contractState { |
| - return contractState{Stage: "issued", Issued: now, Verified: now, ChallengeExpires: now + 900, LastSent: now - 61, ChallengeHash: "test-code"} |
| + return contractState{Stage: "issued", Issued: now, Verified: now, ChallengeExpires: now + 900, LastSent: now - 61, ChallengeHash: "0123456789abcdef0123456789abcdef"} |
| + } |
| + |
| + // Invalid marks cannot complete or persist evidence; a valid drawing does. |
| + markRow := makeRow(issued()) |
| + call(markRow, "sign", url.Values{"digest": {"immutable-digest"}, "consent": {"yes"}, "signature_method": {"drawn"}, "signature_name": {"Paul"}, "signature_strokes": {"[]"}}) |
| + if got := load(markRow); got.Stage != "issued" || got.Signature != nil { |
| + t.Fatal("invalid mark persisted") |
| + } |
| + if w := call(markRow, "sign", url.Values{"digest": {"immutable-digest"}, "consent": {"yes"}, "signature_method": {"drawn"}, "signature_name": {"Paul"}, "signature_strokes": {`[[{"x":10,"y":20},{"x":90,"y":70}]]`}}); w.Code != 303 { |
| + t.Fatal("drawn signature rejected") |
| + } |
| + if got := load(markRow); got.Stage != "completed" || got.Signature == nil || got.Signature.Method != "drawn" || got.Signature.Name != "Paul" { |
| + t.Fatal("drawing not persisted") |
| + } |
| + limited := issued() |
| + limited.Verified = 0 |
| + limited.TotalAttempts = 20 |
| + limitedRow := makeRow(limited) |
| + for _, action := range []string{"verify", "resend"} { |
| + if w := call(limitedRow, action, url.Values{"code": {"0123456789abcdef0123456789abcdef"}}); w.Code < 400 { |
| + t.Fatal("lifetime guess ceiling bypassed") |
| + } |
| + } |
| + // Maximum accepted drawing survives URL encoding and the real request limit. |
| + maxRow := makeRow(issued()) |
| + points := make([]string, 2048) |
| + for i := range points { |
| + if i%2 == 0 { |
| + points[i] = `{"x":600,"y":200}` |
| + } else { |
| + points[i] = `{"x":590,"y":190}` |
| + } |
| + } |
| + maxForm := url.Values{"digest": {"immutable-digest"}, "consent": {"yes"}, "signature_method": {"drawn"}, "signature_name": {"Paul"}, "signature_strokes": {"[[" + strings.Join(points, ",") + "]]"}} |
| + if w := call(maxRow, "sign", maxForm); w.Code != 303 { |
| + t.Fatalf("max drawing rejected: %d", w.Code) |
| } |
| // Actual conflicting HTTP terminal transitions: only one can succeed. |
| row := makeRow(issued()) |
| @@ -90,7 +126,7 @@ func TestContractChallengeRacesExpiryAndRestart(t *testing.T) { |
| go func(action string) { |
| defer wg.Done() |
| <-start |
| - out <- call(row, action, url.Values{"digest": {"immutable-digest"}, "consent": {"yes"}}).Code |
| + out <- call(row, action, url.Values{"digest": {"immutable-digest"}, "consent": {"yes"}, "signature_name": {"Test Signer"}, "signature_method": {"typed"}}).Code |
| }(action) |
| } |
| close(start) |
| @@ -117,7 +153,11 @@ func TestContractChallengeRacesExpiryAndRestart(t *testing.T) { |
| wg = sync.WaitGroup{} |
| for _, action := range []string{"verify", "resend"} { |
| wg.Add(1) |
| - go func(action string) { defer wg.Done(); <-start; call(row, action, url.Values{"code": {"test-code"}}) }(action) |
| + go func(action string) { |
| + defer wg.Done() |
| + <-start |
| + call(row, action, url.Values{"code": {"0123456789abcdef0123456789abcdef"}}) |
| + }(action) |
| } |
| close(start) |
| wg.Wait() |
| @@ -134,13 +174,13 @@ func TestContractChallengeRacesExpiryAndRestart(t *testing.T) { |
| t.Fatalf("guess %d: %d", i, w.Code) |
| } |
| } |
| - if w := call(row, "verify", url.Values{"code": {"test-code"}}); w.Code < 400 { |
| + if w := call(row, "verify", url.Values{"code": {"0123456789abcdef0123456789abcdef"}}); w.Code < 400 { |
| t.Fatal("exhausted challenge accepted") |
| } |
| s = issued() |
| s.ChallengeExpires = now - 1 |
| row = makeRow(s) |
| - if w := call(row, "sign", url.Values{"digest": {"immutable-digest"}, "consent": {"yes"}}); w.Code < 400 { |
| + if w := call(row, "sign", url.Values{"digest": {"immutable-digest"}, "consent": {"yes"}, "signature_name": {"Test Signer"}, "signature_method": {"typed"}}); w.Code < 400 { |
| t.Fatal("stale verified challenge accepted") |
| } |
| s = issued() |
diff --git a/internal/docs/contract_demo.go b/internal/docs/contract_demo.go| index 06c6885..e878627 100644 |
| --- a/internal/docs/contract_demo.go |
| +++ b/internal/docs/contract_demo.go |
| @@ -61,7 +61,9 @@ type contractRow struct { |
| func (contractRow) TableName() string { return "contract_demo_records" } |
| type contractState struct { |
| - Renderer string |
| + Renderer string |
| + CodeLength, TotalAttempts int |
| + Signature *contractSignature |
| ReleaseBytes, ReleaseSignature []byte |
| @@ -171,6 +173,10 @@ func (d *contractDemo) mount(r chi.Router) { |
| w.Header().Set("Content-Type", "text/css; charset=utf-8") |
| w.Write(contractCSS) |
| }) |
| + r.Get("/contracts/signature.js", func(w http.ResponseWriter, r *http.Request) { |
| + w.Header().Set("Content-Type", "text/javascript; charset=utf-8") |
| + w.Write(contractSignatureJS) |
| + }) |
| r.Get("/contracts", d.home) |
| r.Post("/contracts/start", d.start) |
| r.Get("/contracts/{id}", d.show) |
diff --git a/internal/docs/contract_demo_test.go b/internal/docs/contract_demo_test.go| index 85ece46..9f5b425 100644 |
| --- a/internal/docs/contract_demo_test.go |
| +++ b/internal/docs/contract_demo_test.go |
| @@ -122,7 +122,7 @@ func TestContractLiveFlowAndBoundaries(t *testing.T) { |
| app.mailRecorder.mu.Lock() |
| body, to := app.mailRecorder.lastBody, app.mailRecorder.lastTo |
| app.mailRecorder.mu.Unlock() |
| - code := regexp.MustCompile(`Verification code: ([a-f0-9]{32})`).FindStringSubmatch(body) |
| + code := regexp.MustCompile(`Verification code: ([0-9]{6})`).FindStringSubmatch(body) |
| if len(code) != 2 || to != cfg.Importer { |
| t.Fatal("real mail adapter not called for fixed issuer") |
| } |
| @@ -143,7 +143,7 @@ func TestContractLiveFlowAndBoundaries(t *testing.T) { |
| if len(digest) != 2 { |
| t.Fatal("consent not available after verification") |
| } |
| - if rec := own("POST", path+"/sign", url.Values{"digest": {digest[1]}, "consent": {"yes"}}); rec.Code != 303 { |
| + if rec := own("POST", path+"/sign", url.Values{"digest": {digest[1]}, "consent": {"yes"}, "signature_name": {"Test Signer"}, "signature_method": {"typed"}}); rec.Code != 303 { |
| t.Fatalf("sign %d %s", rec.Code, rec.Body) |
| } |
| if rec := own("POST", path+"/void", nil); rec.Code < 400 { |
| @@ -152,7 +152,7 @@ func TestContractLiveFlowAndBoundaries(t *testing.T) { |
| if got := own("GET", path+"/document.pdf", nil); !bytes.Equal(got.Body.Bytes(), original.Body.Bytes()) { |
| t.Fatal("issued PDF mutated") |
| } |
| - if got := own("GET", path+"/evidence.json", nil); got.Code != 200 || !strings.Contains(got.Body.String(), digest[1]) { |
| + if got := own("GET", path+"/evidence.json", nil); got.Code != 200 || (!strings.Contains(got.Body.String(), digest[1]) || !strings.Contains(got.Body.String(), "Test Signer")) { |
| t.Fatal("evidence not bound") |
| } |
| stranger := app.TestRequest(t, "stranger@example.com", "GET", path+"/document.pdf", nil).Cookies() |
diff --git a/internal/docs/contract_signature.go b/internal/docs/contract_signature.go| new file mode 100644 |
| index 0000000..ae84471 |
| --- /dev/null |
| +++ b/internal/docs/contract_signature.go |
| @@ -0,0 +1,105 @@ |
| +package docs |
| + |
| +import ( |
| + "crypto/rand" |
| + "fmt" |
| + json "github.com/go-json-experiment/json" |
| + "math/big" |
| + "net/url" |
| + "strings" |
| + "unicode" |
| + "unicode/utf8" |
| +) |
| + |
| +type signaturePoint struct { |
| + X int `json:"x"` |
| + Y int `json:"y"` |
| +} |
| +type contractSignature struct { |
| + Method string `json:"method"` |
| + Name string `json:"name"` |
| + Strokes [][]signaturePoint `json:"strokes,omitempty"` |
| +} |
| + |
| +func contractCode() string { |
| + n, err := rand.Int(rand.Reader, big.NewInt(1000000)) |
| + if err != nil { |
| + panic(err) |
| + } |
| + return fmt.Sprintf("%06d", n.Int64()) |
| +} |
| +func challengeLength(s contractState) int { |
| + if s.CodeLength == 6 { |
| + return 6 |
| + } |
| + return 32 |
| +} |
| +func validContractCode(code string, s contractState) bool { |
| + if s.CodeLength != 0 && s.CodeLength != 6 && s.CodeLength != 32 { |
| + return false |
| + } |
| + if len(code) != challengeLength(s) { |
| + return false |
| + } |
| + for _, c := range code { |
| + if c < '0' || c > '9' { |
| + if s.CodeLength == 6 || c < 'a' || c > 'f' { |
| + return false |
| + } |
| + } |
| + } |
| + return true |
| +} |
| +func parseContractSignature(f url.Values) (*contractSignature, error) { |
| + name := strings.TrimSpace(f.Get("signature_name")) |
| + if name == "" || !utf8.ValidString(name) || utf8.RuneCountInString(name) > 120 { |
| + return nil, errContract |
| + } |
| + for _, c := range name { |
| + if unicode.IsControl(c) { |
| + return nil, errContract |
| + } |
| + } |
| + s := &contractSignature{Method: f.Get("signature_method"), Name: name} |
| + switch s.Method { |
| + case "typed": |
| + case "drawn": |
| + raw := f.Get("signature_strokes") |
| + if len(raw) > 48<<10 || json.Unmarshal([]byte(raw), &s.Strokes, json.RejectUnknownMembers(true)) != nil || len(s.Strokes) == 0 || len(s.Strokes) > 64 { |
| + return nil, errContract |
| + } |
| + total := 0 |
| + minX, minY, maxX, maxY := 600, 200, 0, 0 |
| + for _, stroke := range s.Strokes { |
| + if len(stroke) < 2 { |
| + return nil, errContract |
| + } |
| + total += len(stroke) |
| + if total > 2048 { |
| + return nil, errContract |
| + } |
| + for _, p := range stroke { |
| + if p.X < 0 || p.X > 600 || p.Y < 0 || p.Y > 200 { |
| + return nil, errContract |
| + } |
| + minX = min(minX, p.X) |
| + minY = min(minY, p.Y) |
| + maxX = max(maxX, p.X) |
| + maxY = max(maxY, p.Y) |
| + } |
| + } |
| + if max(maxX-minX, maxY-minY) < 5 { |
| + return nil, errContract |
| + } |
| + default: |
| + return nil, errContract |
| + } |
| + return s, nil |
| +} |
| +func signaturePoints(points []signaturePoint) string { |
| + var b strings.Builder |
| + for _, p := range points { |
| + fmt.Fprintf(&b, "%d,%d ", p.X, p.Y) |
| + } |
| + return b.String() |
| +} |
diff --git a/internal/docs/contract_signature_test.go b/internal/docs/contract_signature_test.go| new file mode 100644 |
| index 0000000..ccde62c |
| --- /dev/null |
| +++ b/internal/docs/contract_signature_test.go |
| @@ -0,0 +1,47 @@ |
| +package docs |
| + |
| +import ( |
| + "net/url" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func TestContractSignatureValidation(t *testing.T) { |
| + valid := `[[{"x":10,"y":20},{"x":100,"y":70}]]` |
| + for _, tc := range []struct { |
| + name, method, strokes string |
| + ok bool |
| + }{ |
| + {"Paul Campbell", "typed", "", true}, {"Paul Campbell", "drawn", valid, true}, |
| + {"", "typed", "", false}, {"Paul\nCampbell", "typed", "", false}, |
| + {strings.Repeat("x", 121), "typed", "", false}, {"Paul", "svg", valid, false}, |
| + {"Paul", "drawn", "[]", false}, {"Paul", "drawn", `[[{"x":1,"y":1},{"x":1,"y":1}]]`, false}, |
| + {"Paul", "drawn", `[[{"x":1,"y":1},{"x":601,"y":1}]]`, false}, |
| + {"Paul", "drawn", `[[{"x":1,"y":1},{"x":50.5,"y":1}]]`, false}, |
| + {"Paul", "drawn", `[[{"x":1,"y":1},{"x":50,"y":1,"html":"x"}]]`, false}, |
| + {"Paul", "drawn", valid + "[]", false}, |
| + } { |
| + _, err := parseContractSignature(url.Values{"signature_name": {tc.name}, "signature_method": {tc.method}, "signature_strokes": {tc.strokes}}) |
| + if (err == nil) != tc.ok { |
| + t.Errorf("method=%s strokes=%s accepted=%v", tc.method, tc.strokes, err == nil) |
| + } |
| + } |
| +} |
| +func TestContractCodeCompatibility(t *testing.T) { |
| + for i := 0; i < 100; i++ { |
| + if !validContractCode(contractCode(), contractState{CodeLength: 6}) { |
| + t.Fatal("invalid generated code") |
| + } |
| + } |
| + if !validContractCode("000001", contractState{CodeLength: 6}) || validContractCode("00001", contractState{CodeLength: 6}) || validContractCode("abcdef", contractState{CodeLength: 6}) { |
| + t.Fatal("six digit validation") |
| + } |
| + if !validContractCode(strings.Repeat("a", 32), contractState{}) || validContractCode(strings.Repeat("a", 32), contractState{CodeLength: 6}) { |
| + t.Fatal("legacy compatibility") |
| + } |
| + s := contractState{TotalAttempts: 20} |
| + setDemoChallenge(&s, "123456", 1) |
| + if s.TotalAttempts != 20 { |
| + t.Fatal("resend resets lifetime limit") |
| + } |
| +} |
diff --git a/internal/docs/contract_view.go b/internal/docs/contract_view.go| index 09e0f45..5004ff6 100644 |
| --- a/internal/docs/contract_view.go |
| +++ b/internal/docs/contract_view.go |
| @@ -13,7 +13,10 @@ var contractPage string |
| //go:embed views/contract.css |
| var contractCSS []byte |
| -var contractView = template.Must(template.New("contract").Funcs(template.FuncMap{"when": func(v int64) string { return time.Unix(v, 0).UTC().Format("2 January 2006 at 15:04 UTC") }, "token": func(k string) string { return "{{" + k + "}}" }}).Parse(contractPage)) |
| + |
| +//go:embed views/contract-signature.js |
| +var contractSignatureJS []byte |
| +var contractView = template.Must(template.New("contract").Funcs(template.FuncMap{"points": signaturePoints, "codeLength": challengeLength, "when": func(v int64) string { return time.Unix(v, 0).UTC().Format("2 January 2006 at 15:04 UTC") }, "token": func(k string) string { return "{{" + k + "}}" }}).Parse(contractPage)) |
| type contractViewData struct { |
| ID, Error, Source, Connect string |
diff --git a/internal/docs/views/contract-signature.js b/internal/docs/views/contract-signature.js| new file mode 100644 |
| index 0000000..060a314 |
| --- /dev/null |
| +++ b/internal/docs/views/contract-signature.js |
| @@ -0,0 +1,58 @@ |
| +const form = document.querySelector('[data-signature-form]'); |
| +if (form) { |
| + const canvas = form.querySelector('canvas'); |
| + const ctx = canvas.getContext('2d'); |
| + const method = form.querySelector('[name="signature_method"]'); |
| + const name = form.querySelector('[name="signature_name"]'); |
| + const preview = form.querySelector('[data-typed-signature]'); |
| + const drawing = form.querySelector('[data-drawing]'); |
| + const output = form.querySelector('[name="signature_strokes"]'); |
| + const status = form.querySelector('[data-signature-status]'); |
| + let strokes = [], active = null, pointer = null; |
| + const repaint = () => { |
| + ctx.clearRect(0, 0, 600, 200); |
| + ctx.strokeStyle = '#25352f'; ctx.lineWidth = 2; |
| + ctx.lineCap = 'round'; ctx.lineJoin = 'round'; |
| + for (const stroke of strokes) { |
| + ctx.beginPath(); stroke.forEach((p, i) => i ? ctx.lineTo(p.x, p.y) : ctx.moveTo(p.x, p.y)); ctx.stroke(); |
| + } |
| + output.value = JSON.stringify(strokes.filter(s => s.length > 1)); |
| + }; |
| + if (ctx) { |
| + form.querySelector('[data-method-picker]').hidden = false; |
| + method.addEventListener('change', () => { |
| + drawing.hidden = method.value !== 'drawn'; preview.hidden = method.value !== 'typed'; |
| + }); |
| + name.addEventListener('input', () => { preview.textContent = name.value || 'Your signature'; }); |
| + canvas.addEventListener('pointerdown', e => { |
| + if (pointer !== null || e.button !== 0 || strokes.length >= 64) return; |
| + e.preventDefault(); pointer = e.pointerId; active = []; strokes.push(active); |
| + canvas.setPointerCapture(pointer); add(e); |
| + }); |
| + function add(e) { |
| + if (e.pointerId !== pointer || !active) return; |
| + if (strokes.reduce((n,s) => n+s.length,0) >= 2048) { |
| + status.textContent = 'Drawing limit reached. Clear to start again, or use this signature.'; return; |
| + } |
| + const r = canvas.getBoundingClientRect(); |
| + const p = {x: Math.round(Math.max(0,Math.min(600,(e.clientX-r.left)*600/r.width))), |
| + y: Math.round(Math.max(0,Math.min(200,(e.clientY-r.top)*200/r.height)))}; |
| + const last = active.at(-1); |
| + if (!last || p.x !== last.x || p.y !== last.y) active.push(p); |
| + repaint(); |
| + } |
| + canvas.addEventListener('pointermove', add); |
| + const finish = e => { if(e.pointerId !== pointer) return; active=null; pointer=null; strokes=strokes.filter(s=>s.length>1); repaint(); }; |
| + canvas.addEventListener('pointerup', finish); |
| + canvas.addEventListener('pointercancel', finish); |
| + canvas.addEventListener('lostpointercapture', finish); |
| + form.querySelector('[data-clear-signature]').addEventListener('click', () => { |
| + strokes=[]; active=null; pointer=null; status.textContent='Signature cleared.'; repaint(); |
| + }); |
| + form.addEventListener('submit', e => { |
| + if (method.value === 'drawn' && !strokes.some(s=>s.length>1)) { |
| + e.preventDefault(); status.textContent='Draw your signature, or choose Type.'; |
| + } |
| + }); |
| + } |
| +} |
diff --git a/internal/docs/views/contract.css b/internal/docs/views/contract.css| index 0a16290..1dddca7 100644 |
| --- a/internal/docs/views/contract.css |
| +++ b/internal/docs/views/contract.css |
| @@ -1 +1,3 @@ |
| :root{font:16px/1.55 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;color:#25352f;background:#f6f5ef;color-scheme:light}*{box-sizing:border-box}body{margin:0}a{color:#245b46;text-underline-offset:4px}button,input,textarea{font:inherit}.bar{display:flex;align-items:center;gap:32px;padding:22px 5vw;border-bottom:1px solid #dddcd3}.bar a{text-decoration:none}.brand{font-size:21px}.bar>span{margin-left:auto;font-size:13px;color:#68716a}main{max-width:1120px;margin:64px auto;padding:0 36px 60px}h1{font-size:clamp(32px,4.7vw,58px);font-weight:550;line-height:1.12;letter-spacing:-1.8px;margin:18px 0 25px}h2{font-size:20px;font-weight:600}.eyebrow{font-size:11px;letter-spacing:2px;font-weight:650;color:#65756b}.intro{font-size:19px;color:#5c6a61;line-height:1.65;margin:0 0 30px}.button,button{display:inline-block;border:1px solid transparent;border-radius:7px;padding:12px 21px;text-decoration:none;cursor:pointer;font-weight:550}.primary{background:#245b46;color:#fff}.primary:hover{background:#194532}.secondary{background:transparent;border-color:#c6cec7;color:#315443}form{margin:22px 0}.note{margin-top:45px;padding:22px;background:#eceee5;border-radius:9px;max-width:750px;font-size:14px}.note p{margin-bottom:0}.hint,small{color:#5e6c63;font-size:13px;display:block}.recent{margin-top:60px;max-width:750px}.record{display:flex;justify-content:space-between;padding:18px 0;border-bottom:1px solid #d9ded5;text-decoration:none}.record span{font-size:13px}.steps{display:flex;gap:32px;border-bottom:1px solid #d9ded5;padding-bottom:18px;margin-bottom:42px;color:#607165;font-size:13px}.workspace{display:grid;grid-template-columns:minmax(0,2.3fr) minmax(220px,1fr);gap:42px}.workspace form{margin-top:0}label{display:block;font-size:14px;font-weight:600;margin:20px 0 7px}input,textarea{display:block;width:100%;border:1px solid #c6cfc5;border-radius:5px;padding:11px 12px;background:#fffefa;color:#243c2d}textarea{font-family:Georgia,serif;font-size:17px;line-height:1.65;resize:vertical}.twocol{display:grid;grid-template-columns:1fr 1fr;gap:20px}form>.primary{margin-top:18px}.fields{padding:18px 24px;background:#eceee5;border-radius:9px;align-self:start}.fields h2{font-size:15px}.field{border-bottom:1px solid #d2d8cc;padding:13px 0}.field strong{font-size:13px;display:block}.field p{font-size:14px;margin:5px 0}.fields code{display:block;font-size:12px;margin:7px 0;color:#315745}.preview{margin-top:45px;padding-top:25px;border-top:1px solid #d4d9cf}.preview-head{display:flex;align-items:center;justify-content:space-between;gap:15px}.paper{background:#fffefa;box-shadow:0 3px 18px #273d2310;border:1px solid #e0e0d6;padding:55px 65px;max-width:800px;font:18px/1.7 Georgia,serif;margin:20px 0 32px;white-space:pre-wrap;overflow-wrap:anywhere}.paper p{min-height:1em;margin:0 0 1em}.facts{display:grid;grid-template-columns:160px 1fr;gap:14px 22px;max-width:750px;padding:22px 0}.facts dt{font-size:13px;color:#617064}.facts dd{margin:0;white-space:pre-wrap}.check{display:flex;gap:12px;align-items:flex-start;max-width:650px}.check input{width:auto;margin-top:6px;accent-color:#245b46}.error{padding:16px;background:#f8e6dd;color:#773c22;border:1px solid #d9bcb0}details{margin:22px 0;font-size:14px}summary{cursor:pointer}.downloads{display:flex;gap:12px;flex-wrap:wrap}footer{margin-top:60px;border-top:1px solid #d9ded5;padding-top:15px;font-size:12px;color:#617064}.digest{overflow-wrap:anywhere}.skip{position:absolute;left:-9999px}.skip:focus{left:12px;top:8px;background:white;padding:8px}a:focus-visible,button:focus-visible,input:focus-visible,textarea:focus-visible{outline:3px solid #b38e36;outline-offset:3px}@media(max-width:700px){main{margin-top:35px;padding:0 20px 45px}.bar{padding:16px 20px;gap:18px}.bar>span{display:none}.workspace{grid-template-columns:1fr}.fields{order:-1}.steps{gap:15px;font-size:11px}.paper{padding:25px}.facts{grid-template-columns:1fr;gap:4px}.facts dd{margin-bottom:14px}.preview-head{display:block}} |
| + |
| +[hidden]{display:none!important}.signature-box{max-width:650px;padding:24px;border:1px solid #c6cfc5;border-radius:9px;background:#fffefa;margin:24px 0}.signature-box canvas,.saved-signature{display:block;width:100%;height:auto;aspect-ratio:3;border:1px solid #c6cfc5;border-radius:5px;background:#fffefa;touch-action:none;margin:15px 0}.typed-signature{font:italic 36px/1.5 Georgia,serif;overflow-wrap:anywhere;min-height:70px;margin:20px 0}.signature-box select{font:inherit;padding:10px;border:1px solid #c6cfc5;border-radius:5px;background:#fffefa;color:#25352f}.signature-box select:focus-visible{outline:3px solid #b38e36;outline-offset:3px} |
diff --git a/internal/docs/views/contract.html b/internal/docs/views/contract.html| index 2895e7c..f9dd3f3 100644 |
| --- a/internal/docs/views/contract.html |
| +++ b/internal/docs/views/contract.html |
| @@ -1,4 +1,4 @@ |
| -<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>{{if .Home}}Contracts{{else}}{{.State.Title}}{{end}} · Oficina Docs</title><link rel="stylesheet" href="/contracts/style.css"></head><body> |
| +<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>{{if .Home}}Contracts{{else}}{{.State.Title}}{{end}} · Oficina Docs</title><link rel="stylesheet" href="/contracts/style.css"><script src="/contracts/signature.js" defer></script></head><body> |
| <a class="skip" href="#main">Skip to content</a> |
| <header class="bar"><a href="/" class="brand">Oficina <strong>Docs</strong></a><a href="/contracts">Contracts</a><span>{{.State.Address}}</span></header> |
| <main id="main"> |
| @@ -30,13 +30,23 @@ |
| {{if .Expired}}<p>The 24-hour signing window has ended. Prepare a new agreement to continue.</p>{{else}} |
| <p>The document is frozen. <a href="/contracts/{{.ID}}/document.pdf" target="_blank" rel="noopener">Review the PDF ↗</a></p> |
| <article class="paper" aria-label="Agreement reading view">{{range .State.Paragraphs}}<p>{{.}}</p>{{end}}</article> |
| -{{if .State.Verified}}<form method="post" action="/contracts/{{.ID}}/sign"><input type="hidden" name="digest" value="{{.State.Digest}}"><label class="check"><input type="checkbox" name="consent" value="yes" required><span>I have reviewed this agreement and agree to sign it electronically in my own name.</span></label><p>Signing as <strong>{{.State.Address}}</strong>.</p><button class="primary">Sign agreement</button></form>{{else}} |
| +{{if and (ge .State.TotalAttempts 20) (not .State.Verified)}}<p>The verification attempt limit has been reached. Prepare a new agreement to continue.</p>{{else}} |
| +{{if .State.Verified}}<form method="post" action="/contracts/{{.ID}}/sign" data-signature-form> |
| +<section class="signature-box"><h2>Your signature</h2> |
| +<label for="signature-name">Full name</label><input id="signature-name" name="signature_name" maxlength="120" autocomplete="name" required> |
| +<div data-method-picker hidden><label for="signature-method">How would you like to sign?</label><select id="signature-method" name="signature_method"><option value="typed">Type</option><option value="drawn">Draw</option></select></div> |
| +<div class="typed-signature" data-typed-signature aria-live="polite">Your signature</div> |
| +<div data-drawing hidden><p id="drawing-help">Draw with your mouse, finger or pen. You can also choose Type.</p><canvas width="600" height="200" aria-label="Draw your signature" aria-describedby="drawing-help"></canvas><button type="button" class="secondary" data-clear-signature>Clear signature</button></div> |
| +<input type="hidden" name="signature_strokes"><p data-signature-status role="status"></p> |
| +<p class="hint">Your signature is saved with the signing evidence for this exact agreement.</p></section><input type="hidden" name="digest" value="{{.State.Digest}}"><label class="check"><input type="checkbox" name="consent" value="yes" required><span>I have reviewed this agreement and agree to sign it electronically in my own name.</span></label><p>Signing as <strong>{{.State.Address}}</strong>.</p><button class="primary">Sign agreement</button></form>{{else}} |
| <p>{{if eq .State.Delivery "sent"}}We sent a verification code to{{else if eq .State.Delivery "failed"}}Email delivery failed. Retry sending the code to{{else}}Email delivery is pending or uncertain. Check the inbox for{{end}} <strong>{{.State.Address}}</strong>.</p> |
| -<form method="post" action="/contracts/{{.ID}}/verify"><label for="code">Verification code</label><input id="code" name="code" autocomplete="one-time-code" spellcheck="false" maxlength="32" minlength="32" required><small>Paste the 32-character code from the email. It expires after 15 minutes. {{.State.Attempts}} of 5 attempts used.</small><button class="primary">Verify my email →</button></form>{{end}} |
| +<form method="post" action="/contracts/{{.ID}}/verify"><label for="code">Verification code</label><input id="code" name="code" autocomplete="one-time-code" spellcheck="false" maxlength="{{codeLength .State}}" minlength="{{codeLength .State}}" {{if eq .State.CodeLength 6}}inputmode="numeric" pattern="[0-9]{6}"{{end}} required><small>{{if eq .State.CodeLength 6}}Enter the six-digit code from the email.{{else}}Enter your existing 32-character code, or send a new six-digit code.{{end}} It expires after 15 minutes. {{.State.Attempts}} of 5 attempts used.</small><button class="primary">Verify my email →</button></form>{{end}} |
| <form method="post" action="/contracts/{{.ID}}/resend"><button class="secondary">Send a new code</button><small>Available once per minute. This replaces any previous code or verification.</small></form> |
| +{{end}} |
| <details><summary>Withdraw this signing request</summary><p>This permanently closes the unsigned request.</p><form method="post" action="/contracts/{{.ID}}/void"><button class="secondary">Withdraw request</button></form></details>{{end}} |
| {{else if eq .State.Stage "completed"}} |
| <div class="eyebrow">AGREEMENT COMPLETED</div><h1>Signed. And all together.</h1><p class="intro">{{.State.Title}}<br>Accepted by {{.State.Address}}</p><dl class="facts"><dt>Signed</dt><dd>{{when .State.Signed}}</dd><dt>Verification</dt><dd>Email address verified before signing</dd></dl> |
| +{{with .State.Signature}}<section class="signature-box"><h2>Signed by {{.Name}}</h2>{{if eq .Method "drawn"}}<svg class="saved-signature" viewBox="0 0 600 200" role="img" aria-label="Signature drawn by {{.Name}}">{{range .Strokes}}<polyline points="{{points .}}" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>{{end}}</svg>{{else}}<p class="typed-signature">{{.Name}}</p>{{end}}</section>{{end}} |
| <div class="downloads"><a class="button primary" href="/contracts/{{.ID}}/document.pdf">Download original agreement</a><a class="button secondary" href="/contracts/{{.ID}}/evidence.json">Download signing evidence</a></div><p class="hint">The original PDF is preserved unchanged. The evidence records your affirmative acceptance and its exact document digest; this is not a PDF certificate signature.</p> |
| {{else if eq .State.Stage "voided"}}<div class="eyebrow">REQUEST WITHDRAWN</div><h1>This agreement is closed.</h1><p>It can no longer be signed. Prepare a new agreement if you need to continue.</p>{{end}} |
| {{if .State.Issued}}<footer><p>Demo records available until {{when .Retention}}. Download completed records to keep them. Existing encrypted backups may retain data under the app’s backup policy.</p><details><summary>Document identity</summary><code class="digest">{{.State.Digest}}</code></details></footer>{{end}} |