| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | |
| 9 | "amadan.net/rastrillo/rastrillo/sessions" |
| 10 | ) |
| 11 | |
| 12 | var errHomeIdentity = errors.New("memoria: Home identity unavailable") |
| 13 | |
| 14 | type homeIdentityQueryer interface { |
| 15 | QueryRowContext(context.Context, string, ...any) *sql.Row |
| 16 | } |
| 17 | |
| 18 | const homeAddressFingerprintPurpose = "home.local-address.lookup.v1" |
| 19 | |
| 20 | func homeAddressPurpose(id int64) string { |
| 21 | return fmt.Sprintf("home.local-address.v1:%d", id) |
| 22 | } |
| 23 | |
| 24 | // ensureHomeLocalIdentity allocates only after independent local verification. The |
| 25 | // row is an identity binding, not a membership or content ownership record. |
| 26 | func (a *app) ensureHomeLocalIdentity(ctx context.Context, session sessions.Session) (int64, string, error) { |
| 27 | if session.Method != "magiclink" && session.Method != "keymail" { |
| 28 | return 0, "", errHomeIdentity |
| 29 | } |
| 30 | address := normaliseAddress(session.Subject) |
| 31 | if address == "" || !a.Admit(address) { |
| 32 | return 0, "", errHomeIdentity |
| 33 | } |
| 34 | fingerprint, err := a.store.sealer.Fingerprint(homeAddressFingerprintPurpose, []byte(address)) |
| 35 | if err != nil { |
| 36 | return 0, "", errHomeIdentity |
| 37 | } |
| 38 | tx, err := a.db.Writer().BeginTx(ctx, nil) |
| 39 | if err != nil { |
| 40 | return 0, "", errHomeIdentity |
| 41 | } |
| 42 | defer tx.Rollback() |
| 43 | var id int64 |
| 44 | err = tx.QueryRowContext(ctx, "SELECT id FROM memoria_home_local_identities WHERE address_lookup=?", fingerprint).Scan(&id) |
| 45 | if errors.Is(err, sql.ErrNoRows) { |
| 46 | result, insertErr := tx.ExecContext(ctx, "INSERT INTO memoria_home_local_identities(address_lookup,address_sealed) VALUES(?,?)", fingerprint, []byte{}) |
| 47 | if insertErr != nil { |
| 48 | return 0, "", errHomeIdentity |
| 49 | } |
| 50 | id, err = result.LastInsertId() |
| 51 | if err != nil || id <= 0 { |
| 52 | return 0, "", errHomeIdentity |
| 53 | } |
| 54 | sealed, sealErr := a.store.sealer.Seal(homeAddressPurpose(id), []byte(address)) |
| 55 | if sealErr != nil { |
| 56 | return 0, "", errHomeIdentity |
| 57 | } |
| 58 | if _, err = tx.ExecContext(ctx, "UPDATE memoria_home_local_identities SET address_sealed=? WHERE id=?", sealed, id); err != nil { |
| 59 | return 0, "", errHomeIdentity |
| 60 | } |
| 61 | } else if err != nil { |
| 62 | return 0, "", errHomeIdentity |
| 63 | } |
| 64 | label, err := a.lookupHomeLocalIdentity(ctx, tx, id) |
| 65 | if err != nil || label != address || tx.Commit() != nil { |
| 66 | return 0, "", errHomeIdentity |
| 67 | } |
| 68 | return id, label, nil |
| 69 | } |
| 70 | |
| 71 | func (a *app) lookupHomeLocalIdentity(ctx context.Context, q homeIdentityQueryer, id int64) (string, error) { |
| 72 | if id <= 0 { |
| 73 | return "", sql.ErrNoRows |
| 74 | } |
| 75 | var fingerprint string |
| 76 | var sealed []byte |
| 77 | if err := q.QueryRowContext(ctx, "SELECT address_lookup,address_sealed FROM memoria_home_local_identities WHERE id=?", id).Scan(&fingerprint, &sealed); err != nil { |
| 78 | return "", err |
| 79 | } |
| 80 | plain, err := a.store.sealer.Open(homeAddressPurpose(id), sealed) |
| 81 | if err != nil { |
| 82 | return "", errHomeIdentity |
| 83 | } |
| 84 | address := string(plain) |
| 85 | lookup, err := a.store.sealer.Fingerprint(homeAddressFingerprintPurpose, plain) |
| 86 | if err != nil || lookup != fingerprint || address != normaliseAddress(address) { |
| 87 | return "", errHomeIdentity |
| 88 | } |
| 89 | // In team mode the roster decides standing, and a link is identity: |
| 90 | // an address taken off MEMORIA_OWNERS loses its local sign-in, not |
| 91 | // the Home sign-in its link gives it (team_access.go). |
| 92 | if !a.teamMode() && !a.Admit(address) { |
| 93 | return "", sql.ErrNoRows |
| 94 | } |
| 95 | return address, nil |
| 96 | } |
| 97 | |
| 98 | // homeLocalID is the local identity row's ID for address, read-only. |
| 99 | // found is false when the person has no row, which every link needs: a |
| 100 | // link names the row's ID, and IDs are never reused. |
| 101 | func (a *app) homeLocalID(ctx context.Context, address string) (id int64, found bool, err error) { |
| 102 | fingerprint, err := a.store.sealer.Fingerprint(homeAddressFingerprintPurpose, []byte(normaliseAddress(address))) |
| 103 | if err != nil { |
| 104 | return 0, false, errHomeIdentity |
| 105 | } |
| 106 | err = a.db.Writer().QueryRowContext(ctx, "SELECT id FROM memoria_home_local_identities WHERE address_lookup=?", fingerprint).Scan(&id) |
| 107 | if errors.Is(err, sql.ErrNoRows) { |
| 108 | return 0, false, nil |
| 109 | } |
| 110 | if err != nil { |
| 111 | return 0, false, errHomeIdentity |
| 112 | } |
| 113 | return id, true, nil |
| 114 | } |
| 115 | |
| 116 | // localHomeIdentity is read-only: resolving a browser session during a Home |
| 117 | // exchange cannot allocate an identity or change the account being linked. |
| 118 | func (a *app) localHomeIdentity(ctx context.Context, session sessions.Session) (int64, string, error) { |
| 119 | if session.Method != "magiclink" && session.Method != "keymail" { |
| 120 | return 0, "", errHomeIdentity |
| 121 | } |
| 122 | address := normaliseAddress(session.Subject) |
| 123 | if address == "" || !a.Admit(address) { |
| 124 | return 0, "", errHomeIdentity |
| 125 | } |
| 126 | fingerprint, err := a.store.sealer.Fingerprint(homeAddressFingerprintPurpose, []byte(address)) |
| 127 | if err != nil { |
| 128 | return 0, "", errHomeIdentity |
| 129 | } |
| 130 | var id int64 |
| 131 | if err = a.db.Writer().QueryRowContext(ctx, "SELECT id FROM memoria_home_local_identities WHERE address_lookup=?", fingerprint).Scan(&id); err != nil { |
| 132 | return 0, "", errHomeIdentity |
| 133 | } |
| 134 | label, err := a.lookupHomeLocalIdentity(ctx, a.db.Writer(), id) |
| 135 | if err != nil || label != address { |
| 136 | return 0, "", errHomeIdentity |
| 137 | } |
| 138 | return id, label, nil |
| 139 | } |
| 140 | |