diff --git a/src/commands/auth.sh b/src/commands/auth.sh index 6d7b278..fdfb773 100644 --- a/src/commands/auth.sh +++ b/src/commands/auth.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +# TODO: Refresh session on old token + # shellcheck disable=SC2120 function atfile.auth() { override_username="$1" diff --git a/src/shared/cache.sh b/src/shared/cache.sh index 25e4d12..0e6d86b 100644 --- a/src/shared/cache.sh +++ b/src/shared/cache.sh @@ -11,28 +11,34 @@ function atfile.cache.debug() { } function atfile.cache.del() { - key="$(atfile.util.get_cache_path "$1")" + key="$1" + + key_path="$(atfile.util.get_cache_path "$key")" - atfile.cache.debug "$1" "Deleting" - [[ -f "$key" ]] && rm "$key" + atfile.cache.debug "$key" "Deleting" + [[ -f "$key_path" ]] && rm "$key_path" } function atfile.cache.get() { - key="$(atfile.util.get_cache_path "$1")" + key="$1" unset value + key_path="$(atfile.util.get_cache_path "$key")" + + [[ -f "$key_path" ]] && value="$(cat "$key_path")" atfile.cache.debug "$1" "Getting" "$value" - [[ -f "$key" ]] && value="$(cat $key)" echo "$value" } function atfile.cache.set() { - key="$(atfile.util.get_cache_path "$1")" + key="$1" value="$2" - atfile.cache.debug "$1" "Setting" "$value" # shellcheck disable=SC2154 mkdir -p "$_path_cache" - echo "$value" > "$key" + key_path="$(atfile.util.get_cache_path "$key")" + + atfile.cache.debug "$key" "Setting" "$value" + echo "$value" > "$key_path" echo "$value" } diff --git a/src/shared/util.sh b/src/shared/util.sh index 8a82f25..07c781d 100644 --- a/src/shared/util.sh +++ b/src/shared/util.sh @@ -877,6 +877,9 @@ function atfile.util.override_actor() { _fmt_blob_url="$_fmt_blob_url_default" atfile.say.debug "Overridden identity\n↳ DID: $_username\n↳ PDS: $_server\n↳ Blob URL: $_fmt_blob_url" + + atfile.say.debug "Deleting cached JWT for '$_username_original'..." + atfile.cache.del "token" } # NOTE: This is to help during sourcing if atfile.uitl.override_actor() has diff --git a/src/shared/xrpc.sh b/src/shared/xrpc.sh index 789ff6c..89fc0d8 100644 --- a/src/shared/xrpc.sh +++ b/src/shared/xrpc.sh @@ -32,9 +32,20 @@ function atfile.xrpc.pds.get() { } function atfile.xrpc.pds.jwt() { - atfile.http.post \ - "$_server/xrpc/com.atproto.server.createSession" \ - '{"identifier": "'"$_username"'", "password": "'"$_password"'"}' | jq -r ".accessJwt" + token="$(atfile.cache.get "token")" + + if [[ -z "$token" ]]; then + atfile.say.debug "Generating JWT for '$_username'..." + new_token="$(atfile.http.post \ + "$_server/xrpc/com.atproto.server.createSession" \ + '{"identifier": "'"$_username"'", "password": "'"$_password"'"}' | jq -r ".accessJwt")" + + token="$(atfile.cache.set "token" "$new_token")" + else + atfile.say.debug "Reusing cached JWT for '$_username'..." + fi + + echo "$token" } function atfile.xrpc.pds.post() {