📦➔🦋 Store and retrieve files on the Atmosphere
1#!/usr/bin/env bash
2
3# shellcheck disable=SC2120
4function atfile.auth() {
5 override_username="$1"
6 override_password="$2"
7
8 [[ -n "$override_password" ]] && _password="$override_password"
9 [[ -n "$override_username" ]] && _username="$override_username"
10
11 if [[ -z "$_server" ]]; then
12 skip_resolving=0
13
14 # shellcheck disable=SC2154
15 if [[ -z $override_username ]] && [[ $_is_sourced == 0 ]]; then
16 # NOTE: Speeds things up a little if the user is overriding actor
17 # Keep this in-sync with the main case in `../entry.sh`!
18 if [[ $_command == "bsky" && -n "${_command_args[1]}" ]] ||\
19 [[ $_command == "cat" && -n "${_command_args[1]}" ]] ||\
20 [[ $_command == "fetch" && -n "${_command_args[1]}" ]] ||\
21 [[ $_command == "fetch-crypt" && -n "${_command_args[1]}" ]] ||\
22 [[ $_command == "info" && -n "${_command_args[1]}" ]] ||\
23 [[ $_command == "list" && "${_command_args[0]}" == *.* ]] ||\
24 [[ $_command == "list" && "${_command_args[0]}" == did:* ]] ||\
25 [[ $_command == "list" && -n "${_command_args[1]}" ]] ||\
26 [[ $_command == "url" && -n "${_command_args[1]}" ]]; then
27 atfile.say.debug "Skipping identity resolving\n↳ Actor is overridden by command ('$_command')"
28 skip_resolving=1
29 fi
30
31 # NOTE: Speeds things up a little if the command doesn't need actor resolving
32 if [[ -z $_command ]] ||\
33 [[ $_command == "ai" ]] ||\
34 [[ $_command == "build" ]] ||\
35 [[ $_command == "handle" ]] ||\
36 [[ $_command == "help" ]] ||\
37 [[ $_command == "now" ]] ||\
38 [[ $_command == "resolve" ]] ||\
39 [[ $_command == "scrape" ]] ||\
40 [[ $_command == "something-broke" ]] ||\
41 [[ $_command == "stream" ]] ||\
42 [[ $_command == "update" ]] ||\
43 [[ $_command == "version" ]]; then
44 atfile.say.debug "Skipping identity resolving\n↳ Not required for command '$_command'"
45 skip_resolving=1
46 fi
47 fi
48
49 if [[ $skip_resolving == 0 ]]; then
50 [[ -z "$_username" || "$_username" == "<your-username>" ]] && atfile.die "\$${_envvar_prefix}_USERNAME not set"
51 [[ -z "$_password" || "$_password" == "<your-password>" ]] && atfile.die "\$${_envvar_prefix}_PASSWORD not set"
52
53 atfile.say.debug "Authenticating as '$_username'..."
54
55 resolved_did="$(atfile.util.resolve_identity "$_username")"
56 error="$(atfile.util.get_xrpc_error $? "$resolved_did")"
57 [[ -n "$error" ]] && atfile.die.xrpc_error "Unable to resolve '$_username'" "$resolved_did"
58
59 _username="$(echo "$resolved_did" | cut -d "|" -f 1)"
60 _server="$(echo "$resolved_did" | cut -d "|" -f 2)"
61
62 atfile.say.debug "Resolved identity\n↳ DID: $_username\n↳ PDS: $_server"
63 fi
64 else
65 atfile.say.debug "Skipping identity resolving\n↳ ${_envvar_prefix}_ENDPOINT_PDS is set ($_server)"
66 [[ $_server != "http://"* ]] && [[ $_server != "https://"* ]] && _server="https://$_server"
67 fi
68
69 if [[ -n $_server ]]; then
70 # shellcheck disable=SC2154
71 if [[ $_disable_auth_check == 0 ]]; then
72 atfile.say.debug "Checking authentication is valid..."
73
74 session="$(com.atproto.server.getSession)"
75 error="$(atfile.util.get_xrpc_error $? "$session")"
76
77 if [[ -n "$error" ]]; then
78 atfile.die.xrpc_error "Unable to authenticate" "$error"
79 else
80 _username="$(echo "$session" | jq -r ".did")"
81 fi
82 else
83 atfile.say.debug "Skipping checking authentication validity\n↳ ${_envvar_prefix}_DISABLE_AUTH_CHECK is set ($_disable_auth_check)"
84 if [[ "$_username" != "did:"* ]]; then
85 atfile.die "Cannot skip authentication validation without a DID\n↳ \$${_envvar_prefix}_USERNAME currently set to '$_username' (need \"did:<type>:<key>\")"
86 fi
87 fi
88 fi
89}