Skip to content

Instantly share code, notes, and snippets.

@lrvick
Created December 24, 2022 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrvick/b23b63f289790e2264a7c5a07b056d12 to your computer and use it in GitHub Desktop.
Save lrvick/b23b63f289790e2264a7c5a07b056d12 to your computer and use it in GitHub Desktop.
postgrest testing
#!/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