| 1 | package fichas |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "unicode" |
| 7 | |
| 8 | "amadan.net/oficina/fichas/internal/fichas/seal" |
| 9 | ) |
| 10 | |
| 11 | // printFor returns the equality print for a value of fd, or "" when fd is |
| 12 | // not a printed field. |
| 13 | // |
| 14 | // Two jobs, both of which seal.Print deliberately does not do. |
| 15 | // |
| 16 | // **It refuses a field that is not TierPrint.** seal.Print takes a bare |
| 17 | // string and its doc comment was the only guard; a comment is not a guard. |
| 18 | // Printing a TierSealed field would publish equality classes for exactly |
| 19 | // the values §4.2 sealed to avoid publishing — `stage` has five, and |
| 20 | // frequency analysis puts five classes in order. |
| 21 | // |
| 22 | // **It canonicalises first.** An HMAC over raw bytes makes `Ana@Example.com` |
| 23 | // and `ana@example.com` different values, and `+353 1 555 0100` different |
| 24 | // from `+35315550100`. The print exists to answer "do we already have this |
| 25 | // contact"; without canonicalisation it misses on the commonest real |
| 26 | // variance and the feature quietly does not work. memoria normalises an |
| 27 | // address before fingerprinting for the same reason. |
| 28 | func printFor(s *seal.Sealer, fd FieldDef, value string) (string, error) { |
| 29 | if fd.Tier != TierPrint { |
| 30 | return "", fmt.Errorf( |
| 31 | "fichas: %s is tier %s and must not be printed; printing it would publish its equality classes", |
| 32 | fd.Key, fd.Tier) |
| 33 | } |
| 34 | return s.Print(fd.Key, []byte(canonical(fd.Kind, value))) |
| 35 | } |
| 36 | |
| 37 | // canonical folds away the variance that should not change identity. |
| 38 | // |
| 39 | // Conservative on purpose: it removes case, surrounding space and, for a |
| 40 | // phone number, formatting. It does NOT try to parse or validate — a |
| 41 | // canonicaliser that rejects is a validator, and a validator here would |
| 42 | // silently drop values out of the print space rather than refusing them |
| 43 | // where a person could see it. |
| 44 | func canonical(kind, v string) string { |
| 45 | v = strings.TrimSpace(v) |
| 46 | switch kind { |
| 47 | case KindNumber: |
| 48 | return v |
| 49 | case KindURL: |
| 50 | return canonicalURL(v) |
| 51 | case KindText, KindEnum, KindDate, KindRef, KindEmail, KindPhone: |
| 52 | // Phone numbers arrive as text. Keeping digits and a leading + |
| 53 | // makes "+353 1 555 0100" and "+35315550100" one value; anything |
| 54 | // with a letter in it is left alone, so this cannot mangle a name. |
| 55 | if looksLikeAPhoneNumber(v) { |
| 56 | return stripPhoneFormatting(v) |
| 57 | } |
| 58 | return strings.ToLower(strings.Join(strings.Fields(v), " ")) |
| 59 | default: |
| 60 | return strings.ToLower(v) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func looksLikeAPhoneNumber(v string) bool { |
| 65 | digits := 0 |
| 66 | for _, r := range v { |
| 67 | switch { |
| 68 | case unicode.IsDigit(r): |
| 69 | digits++ |
| 70 | case r == '+' || r == '-' || r == ' ' || r == '(' || r == ')' || r == '.': |
| 71 | default: |
| 72 | return false |
| 73 | } |
| 74 | } |
| 75 | return digits >= 6 |
| 76 | } |
| 77 | |
| 78 | func stripPhoneFormatting(v string) string { |
| 79 | var b strings.Builder |
| 80 | for i, r := range v { |
| 81 | if unicode.IsDigit(r) || (r == '+' && i == 0) { |
| 82 | b.WriteRune(r) |
| 83 | } |
| 84 | } |
| 85 | return b.String() |
| 86 | } |
| 87 | |