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