Branchwell

Skillfarm / website · skillfarm.ai / scripts / biolock-link.sh

Raw 1.3 KB on ca5271886e6deb70a55372acda5d273e29b90b31
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.
5set -euo pipefail
6handle="${1:?usage: scripts/biolock-link.sh <handle> [minutes]}"
7minutes="${2:-30}"
8[[ "$handle" =~ ^[a-z0-9-]+$ ]] || { echo "bad handle" >&2; exit 1; }
9[[ "$minutes" =~ ^[0-9]+$ ]] || { echo "bad minutes" >&2; exit 1; }
10cd "$(dirname "$0")/.."
11token="$(openssl rand 24 | base64 | tr '+/' '-_' | tr -d '=')"
12hash="$(printf '%s' "$token" | shasum -a 256 | cut -d' ' -f1)"
13now="$(date +%s)"
14npx 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
16count="$(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; }
18echo "https://skillfarm.ai/biolock/add#$token"
19echo "Works once, until $(date -r $((now + minutes * 60)))."