#!/bin/bash # Make a one-time link that adds BioLock to an existing account, such as the site owner's Sprout account. # Usage: scripts/biolock-link.sh [minutes, default 30] # Open the printed link on the device to add and use its fingerprint or face. It works once. set -euo pipefail handle="${1:?usage: scripts/biolock-link.sh [minutes]}" minutes="${2:-30}" [[ "$handle" =~ ^[a-z0-9-]+$ ]] || { echo "bad handle" >&2; exit 1; } [[ "$minutes" =~ ^[0-9]+$ ]] || { echo "bad minutes" >&2; exit 1; } cd "$(dirname "$0")/.." token="$(openssl rand 24 | base64 | tr '+/' '-_' | tr -d '=')" hash="$(printf '%s' "$token" | shasum -a 256 | cut -d' ' -f1)" now="$(date +%s)" npx wrangler d1 execute skillfarm --remote --command \ "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 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"])')" [ "$count" = "1" ] || { echo "No active account with handle $handle." >&2; exit 1; } echo "https://skillfarm.ai/biolock/add#$token" echo "Works once, until $(date -r $((now + minutes * 60)))."