#!/bin/bash # Data API for the Climbing Tracker when hosted in an n0m.org files container. # Each person has their own file: data/users/.json # GET api.sh -> everyone's data as one JSON object, keyed by id # GET api.sh?check= -> whether that person has a password, and whether # the one sent with this request is correct # PUT api.sh?user= -> replaces that person's data with the request body # (needs their password; the first save sets it) # PUT api.sh?user=&photo= # -> saves a photo for one of that person's climbs as # data/photos/-.jpg. The body is # {"photo":""}, or {"photo":""} to # delete it. Photos are shrunk in the browser first # because n0m only accepts JSON bodies up to 100 KB. # (Query strings are used instead of paths like api.sh/users/ because n0m # never responds to a path that continues past a script's file name) # # Passwords: the browser never sends the password itself. It sends a token (a # SHA-256 of the profile id and password) in the X-Auth header. What is kept in # data/.auth/ is a SHA-256 of that token, hashed again together with a # random secret (data/.auth/_pepper) that is created on first use. # # n0m will happily serve any ordinary file in the container, including ones in # dot folders. It runs executable files instead of sending their contents # though, so the secret files are made executable with a "#!/bin/true" first # line: requesting one over the web runs "true" and returns nothing. source exprash.sh redirect_stdout data_dir='data' users_dir="$data_dir/users" backups_dir="$data_dir/backups" auth_dir="$data_dir/.auth" photos_dir="$data_dir/photos" max_photo_bytes=72000 legacy_file="$data_dir/climbing-data.json" pepper_file="$auth_dir/_pepper" token="${HTTP_X_AUTH:-}" mkdir -p "$users_dir" "$backups_dir" "$auth_dir" "$photos_dir" sha256() { sha256sum | cut -d ' ' -f 1 } # $1: file path # $2: secret value write_secret() { local secret_tmp secret_tmp="$(mktemp "$data_dir/tmp.XXXXXX")" printf '#!/bin/true\n%s\n' "$2" > "$secret_tmp" chmod 700 "$secret_tmp" mv "$secret_tmp" "$1" } # $1: file path read_secret() { tail -n 1 "$1" } if [ ! -s "$pepper_file" ]; then write_secret "$pepper_file" "$(head -c 32 /dev/urandom | sha256)" fi pepper="$(read_secret "$pepper_file")" # $1: SHA-256 of a token pepper_hash() { printf '%s:%s' "$pepper" "$1" | sha256 } # One-off upgrade of password files saved before the secret existed for auth_file in "$auth_dir"/*; do [ -f "$auth_file" ] || continue if [ "$(head -c 2 "$auth_file")" != '#!' ]; then write_secret "$auth_file" "$(pepper_hash "$(head -n 1 "$auth_file")")" fi done # $1: user id valid_id() { [[ "$1" =~ ^[a-z0-9]{4,24}$ ]] } valid_token() { [[ "$token" =~ ^[a-f0-9]{64}$ ]] } token_hash() { pepper_hash "$(printf '%s' "$token" | sha256)" } # $1: user id has_password() { [ -s "$auth_dir/$1" ] } # $1: user id password_correct() { valid_token && has_password "$1" && [ "$(read_secret "$auth_dir/$1")" == "$(token_hash)" ] } # One-off move of the original single-person data into Alysia's profile if [ -s "$legacy_file" ] && [ ! -e "$users_dir/alysia.json" ] && [ "$(head -c 1 "$legacy_file")" == '{' ] && [ "$(cat "$legacy_file")" != '{}' ] then migrate_tmp="$(mktemp "$data_dir/tmp.XXXXXX")" { printf '{"name":"Alysia",'; tail -c +2 "$legacy_file"; } > "$migrate_tmp" mv "$migrate_tmp" "$users_dir/alysia.json" mv "$legacy_file" "$backups_dir/original-climbing-data.json" fi get '/' && { set_header 'Cache-Control' 'no-store' if has_query 'check'; then check_id="$(query 'check')" ok='false' has='false' if valid_id "$check_id"; then has_password "$check_id" && has='true' password_correct "$check_id" && ok='true' fi printf '{"ok":%s,"hasPassword":%s}' "$ok" "$has" | send_json else { printf '{' separator='' for user_file in "$users_dir"/*.json; do [ -s "$user_file" ] || continue user_id="$(basename "$user_file" .json)" [[ "$user_id" =~ ^[a-z0-9]+$ ]] || continue printf '%s"%s":' "$separator" "$user_id" cat "$user_file" separator=',' done printf '}' } | send_json fi } put '/' && { user_id="$(query 'user')" tmp_file="$(mktemp "$data_dir/tmp.XXXXXX")" cat > "$tmp_file" # n0m only passes the body through for valid JSON requests, so this is just # a sanity check that we aren't about to save something empty or truncated first_char="$(head -c 1 "$tmp_file")" last_char="$(tail -c 1 "$tmp_file")" if ! valid_id "$user_id" || [ "$first_char" != '{' ] || [ "$last_char" != '}' ] then rm -f "$tmp_file" status '400' printf '{"ok":false}' | send_json elif has_query 'photo'; then photo_id="$(query 'photo')" photo_file="$photos_dir/$user_id-$photo_id.jpg" photo_tmp="$(mktemp "$data_dir/tmp.XXXXXX")" sed -n 's/^{"photo":"\([A-Za-z0-9+\/=]*\)"}$/\1/p' "$tmp_file" | base64 -d > "$photo_tmp" 2>/dev/null photo_start="$(head -c 3 "$photo_tmp" | od -An -tx1 | tr -d ' \n')" photo_bytes="$(wc -c < "$photo_tmp")" if ! valid_id "$photo_id"; then status '400' printf '{"ok":false}' | send_json elif ! password_correct "$user_id"; then status '403' printf '{"ok":false,"auth":false}' | send_json elif [ "$(cat "$tmp_file")" == '{"photo":""}' ]; then rm -f "$photo_file" printf '{"ok":true}' | send_json # Only real JPEG files (they all start with the bytes FF D8 FF) are kept elif [ "$photo_start" == 'ffd8ff' ] && [ "$photo_bytes" -le "$max_photo_bytes" ] then chmod 644 "$photo_tmp" mv "$photo_tmp" "$photo_file" printf '{"ok":true}' | send_json else status '400' printf '{"ok":false}' | send_json fi rm -f "$tmp_file" "$photo_tmp" elif ! valid_token || { has_password "$user_id" && ! password_correct "$user_id"; } then rm -f "$tmp_file" status '403' printf '{"ok":false,"auth":false}' | send_json else # A profile without a password yet is claimed by the first save if ! has_password "$user_id"; then write_secret "$auth_dir/$user_id" "$(token_hash)" fi user_file="$users_dir/$user_id.json" # Keep one backup per person per day: their data before that day's first save backup_file="$backups_dir/$user_id-$(date +'%Y%m%d').json" if [ -s "$user_file" ] && [ ! -e "$backup_file" ]; then cp "$user_file" "$backup_file" fi mv "$tmp_file" "$user_file" printf '{"ok":true}' | send_json fi } (use || use_error) && { status '404' printf '{"ok":false}' | send_json }