Created
December 24, 2022 09:56
-
-
Save lrvick/b23b63f289790e2264a7c5a07b056d12 to your computer and use it in GitHub Desktop.
postgrest testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
base64_url_encode(){ | |
data=${1?} | |
echo -n "${data}" \ | |
| openssl base64 -e -A \ | |
| sed 's/\+/-/g' \ | |
| sed 's/\//_/g' \ | |
| sed -E 's/=+$//' | |
} | |
jwt_sig(){ | |
data=${1?} | |
secret=${2?} | |
signature=$( \ | |
echo -n "${data}" \ | |
| openssl dgst -sha256 -hmac "${secret}" -binary \ | |
| openssl base64 -e -A \ | |
| sed 's/\+/-/g' \ | |
| sed 's/\//_/g' \ | |
| sed -E 's/=+$//' | |
) | |
echo -n "${data}"."${signature}" | |
} | |
jwt_token(){ | |
role=${1:-role} | |
secret=${2:-a_test_only_postgrest_jwt_secret} | |
header="$(base64_url_encode '{"alg":"HS256"}')" | |
payload="$(base64_url_encode '{"role":"'"${role}"'"}')" | |
echo -n "$(jwt_sig "${header}.${payload}" "${secret}")" | |
} | |
psql -c "insert into hosts (name,maxusers) values ('test.hashbang.sh','500');"; | |
psql -c "insert into hosts (name,maxusers) values ('test2.hashbang.sh','500');"; | |
curl http://userdb-postgrest:3000/signup \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $(jwt_token 'api-user-create')" \ | |
-X POST \ | |
--data-binary @- <<-EOF | |
{ | |
"name": "testuser43", | |
"host": "test.hashbang.sh", | |
"shell": "/bin/zsh", | |
"keys": ["$(cat bats/keys/id_ed25519.pub)"] | |
} | |
EOF | |
curl http://userdb-postgrest:3000/passwd?name=eq.testuser43 \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $(jwt_token 'api-user-manage')" \ | |
-X PATCH \ | |
--data-binary @- <<-EOF | |
{ | |
"host": "test2.hashbang.sh" | |
} | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment