tangled
alpha
login
or
join now
zio.sh
/
atfile
37
fork
atom
š¦āš¦ Store and retrieve files on the Atmosphere
37
fork
atom
overview
issues
3
pulls
pipelines
improve JWT caching
ducky.ws
1 month ago
530b8586
2913b6c1
1/1
build.yaml
success
8s
+66
-8
4 changed files
expand all
collapse all
unified
split
src
commands
auth.sh
lexi
com_atproto.sh
shared
util.sh
xrpc.sh
+12
-2
src/commands/auth.sh
reviewed
···
1
1
#!/usr/bin/env bash
2
2
3
3
-
# TODO: Refresh session on old token
4
4
-
5
3
# shellcheck disable=SC2120
6
4
function atfile.auth() {
7
5
override_username="$1"
···
76
74
session="$(com.atproto.server.getSession)"
77
75
error="$(atfile.util.get_xrpc_error $? "$session")"
78
76
77
77
+
# TODO: Handle accessJwt expiry
79
78
if [[ -n "$error" ]]; then
79
79
+
#if [[ $error == "[InvalidToken] Token could not be verified" ]]; then
80
80
+
# session="$(com.atproto.server.refreshSession)"
81
81
+
# error="$(atfile.util.get_xrpc_error $? "$session")"
82
82
+
#
83
83
+
# if [[ -n $error ]]; then
84
84
+
# atfile.cache.del "token"
85
85
+
# atfile.die.xrpc_error "Unable to refresh token" "$error"
86
86
+
# fi
87
87
+
#else
88
88
+
# atfile.die.xrpc_error "Unable to authenticate" "$error"
89
89
+
#fi
80
90
atfile.die.xrpc_error "Unable to authenticate" "$error"
81
91
else
82
92
_username="$(echo "$session" | jq -r ".did")"
+5
src/lexi/com_atproto.sh
reviewed
···
75
75
atfile.xrpc.pds.get "com.atproto.sync.listBlobs" "$query"
76
76
}
77
77
78
78
+
function com.atproto.server.refreshSession() {
79
79
+
refresh_token="$(atfile.util.get_token_cache "refresh")"
80
80
+
atfile.xrpc.pds.get "com.atproto.server.refreshSession" "" "" "Bearer $refresh_token"
81
81
+
}
82
82
+
78
83
function com.atproto.sync.uploadBlob() {
79
84
file="$1"
80
85
atfile.xrpc.pds.blob "$1" | jq -r ".blob"
+25
src/shared/util.sh
reviewed
···
1017
1017
fi
1018
1018
}
1019
1019
1020
1020
+
function atfile.util.set_token_cache() {
1021
1021
+
did="$1"
1022
1022
+
access="$2"
1023
1023
+
refresh="$3"
1024
1024
+
1025
1025
+
atfile.cache.set "token" "$did|$access|$refresh"
1026
1026
+
}
1027
1027
+
1028
1028
+
function atfile.util.get_token_cache() {
1029
1029
+
part="$1"
1030
1030
+
unset index
1031
1031
+
1032
1032
+
token_cache="$(atfile.cache.get "token")"
1033
1033
+
1034
1034
+
if [[ -n "$token_cache" ]]; then
1035
1035
+
case "$part" in
1036
1036
+
"did"|"username") index="1" ;;
1037
1037
+
"access") index="2" ;;
1038
1038
+
"refresh") index="3" ;;
1039
1039
+
esac
1040
1040
+
1041
1041
+
echo "$token_cache" | cut -d "|" -f $index
1042
1042
+
fi
1043
1043
+
}
1044
1044
+
1020
1045
function atfile.util.source_hook() {
1021
1046
file="$1"
1022
1047
+24
-6
src/shared/xrpc.sh
reviewed
···
31
31
"$type" | jq
32
32
}
33
33
34
34
+
# shellcheck disable=SC2120
34
35
function atfile.xrpc.pds.jwt() {
35
35
-
token="$(atfile.cache.get "token")"
36
36
+
token="$1"
37
37
+
38
38
+
[[ -z "$token" ]] && token="$(atfile.util.get_token_cache "access")"
36
39
37
40
if [[ -z "$token" ]]; then
38
41
atfile.say.debug "Generating JWT for '$_username'..."
39
39
-
new_token="$(atfile.http.post \
42
42
+
new_session="$(atfile.http.post \
40
43
"$_server/xrpc/com.atproto.server.createSession" \
41
41
-
'{"identifier": "'"$_username"'", "password": "'"$_password"'"}' | jq -r ".accessJwt")"
44
44
+
'{"identifier": "'"$_username"'", "password": "'"$_password"'"}')"
45
45
+
46
46
+
token_access="$(echo "$new_session" | jq -r ".accessJwt")"
47
47
+
token_refresh="$(echo "$new_session" | jq -r ".refreshJwt")"
48
48
+
49
49
+
atfile.say.debug "Generated JWT\nā³ DID: $_username\nā³ Access: $token_access\nā³ Refresh: $token_refresh"
42
50
43
43
-
token="$(atfile.cache.set "token" "$new_token")"
51
51
+
atfile.util.set_token_cache "$_username" "$token_access" "$token_refresh" > /dev/null
52
52
+
53
53
+
token="$token_access"
44
54
else
45
45
-
atfile.say.debug "Reusing cached JWT for '$_username'..."
55
55
+
if [[ $_username == "$(atfile.util.get_token_cache "did")" ]]; then
56
56
+
atfile.say.debug "Reusing cached JWT for '$_username'..."
57
57
+
else
58
58
+
atfile.say.debug "Deleting JWT cache (DID does not match)..."
59
59
+
atfile.cache.del "token"
60
60
+
atfile.xrpc.pds.jwt
61
61
+
fi
46
62
fi
47
63
48
64
echo "$token"
···
52
68
lexi="$1"
53
69
data="$2"
54
70
type="$3"
71
71
+
auth="$4"
55
72
56
73
[[ -z $type ]] && type="application/json"
74
74
+
[[ -z $auth ]] && auth="Bearer $(atfile.xrpc.pds.jwt)"
57
75
58
76
curl -s -X POST "$_server/xrpc/$lexi" \
59
59
-
-H "Authorization: Bearer $(atfile.xrpc.pds.jwt)" \
77
77
+
-H "Authorization: $auth" \
60
78
-H "Content-Type: $type" \
61
79
-H "User-Agent: $(atfile.util.get_uas)" \
62
80
-d "$data" | jq