Download this file
| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/home/internal/operator" |
| 10 | ) |
| 11 | |
| 12 | // NewOperatorPersonResolver supplies identity resolution, never invocation |
| 13 | // authority. Only the signed operator transaction may call this adapter. |
| 14 | func NewOperatorPersonResolver(pepper []byte) (operator.PersonResolver, error) { |
| 15 | if len(pepper) != 32 { |
| 16 | return nil, operator.ErrRefused |
| 17 | } |
| 18 | key := append([]byte(nil), pepper...) |
| 19 | return func(ctx context.Context, conn *sql.Conn, address string, existingOnly bool, now time.Time) (string, bool, error) { |
| 20 | if conn == nil || now.Unix() <= 0 { |
| 21 | return "", false, operator.ErrRefused |
| 22 | } |
| 23 | if existingOnly { |
| 24 | var ref string |
| 25 | e := conn.QueryRowContext(ctx, "SELECT person_ref FROM people WHERE blind_index=?", blindIndex(key, address)).Scan(&ref) |
| 26 | if errors.Is(e, sql.ErrNoRows) { |
| 27 | return "", false, nil |
| 28 | } |
| 29 | if e != nil { |
| 30 | return "", false, operator.ErrUnavailable |
| 31 | } |
| 32 | return ref, true, nil |
| 33 | } |
| 34 | ref, e := findOrMintPersonOn(ctx, conn, key, address, now) |
| 35 | if e != nil { |
| 36 | return "", false, operator.ErrUnavailable |
| 37 | } |
| 38 | return ref, true, nil |
| 39 | }, nil |
| 40 | } |
| 41 | |