Skillfarm / website · skillfarm.ai / scripts / biolock-link.sh
| 1 | #!/bin/bash |
| 2 | # Make a one-time link that adds BioLock to an existing account, such as the site owner's Sprout account. |
| 3 | # Usage: scripts/biolock-link.sh <handle> [minutes, default 30] |
| 4 | # Open the printed link on the device to add and use its fingerprint or face. It works once. |
| 5 | set -euo pipefail |
| 6 | handle="${1:?usage: scripts/biolock-link.sh <handle> [minutes]}" |
| 7 | minutes="${2:-30}" |
| 8 | [[ "$handle" =~ ^[a-z0-9-]+$ ]] || { echo "bad handle" >&2; exit 1; } |
| 9 | [[ "$minutes" =~ ^[0-9]+$ ]] || { echo "bad minutes" >&2; exit 1; } |
| 10 | cd "$(dirname "$0")/.." |
| 11 | token="$(openssl rand 24 | base64 | tr '+/' '-_' | tr -d '=')" |
| 12 | hash="$(printf '%s' "$token" | shasum -a 256 | cut -d' ' -f1)" |
| 13 | now="$(date +%s)" |
| 14 | npx wrangler d1 execute skillfarm --remote --command \ |
| 15 | "INSERT INTO biolock_links (token_hash, user_id, expires_at, created_at) SELECT '$hash', id, $((now + minutes * 60)), $now FROM users WHERE handle = '$handle' AND status = 'active'" >/dev/null |
| 16 | count="$(npx wrangler d1 execute skillfarm --remote --json --command "SELECT COUNT(*) AS n FROM biolock_links WHERE token_hash = '$hash'" | python3 -c 'import json,sys; print(json.load(sys.stdin)[0]["results"][0]["n"])')" |
| 17 | [ "$count" = "1" ] || { echo "No active account with handle $handle." >&2; exit 1; } |
| 18 | echo "https://skillfarm.ai/biolock/add#$token" |
| 19 | echo "Works once, until $(date -r $((now + minutes * 60)))." |