annotate mod_rest/example/rest.sh @ 5636:b3484a112300

mod_rest/rest.sh: Update to use httpie-oauth2 plugin This bash implementation of OAuth2/OIDC was growing to the point where it needed a massive refactor, which made me look into alternatives where I finally settled on implementing oauth2 in a plugin for HTTPie.
author Kim Alvefur <zash@zash.se>
date Sat, 26 Aug 2023 14:37:04 +0200
parents 1c52efb6fd42
children acd2f397ce6b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5281
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 #!/bin/bash -eu
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 # Copyright (c) Kim Alvefur
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 # This file is MIT/X11 licensed.
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
5329
107d60c70c1a mod_rest/rest.sh: List dependencies in comment
Kim Alvefur <zash@zash.se>
parents: 5281
diff changeset
6 # Dependencies:
107d60c70c1a mod_rest/rest.sh: List dependencies in comment
Kim Alvefur <zash@zash.se>
parents: 5281
diff changeset
7 # - https://httpie.io/
5636
b3484a112300 mod_rest/rest.sh: Update to use httpie-oauth2 plugin
Kim Alvefur <zash@zash.se>
parents: 5432
diff changeset
8 # - https://hg.sr.ht/~zash/httpie-oauth2
5329
107d60c70c1a mod_rest/rest.sh: List dependencies in comment
Kim Alvefur <zash@zash.se>
parents: 5281
diff changeset
9
5281
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 # Settings
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 HOST=""
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 DOMAIN=""
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/restrc" ]; then
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 # Config file can contain the above settings
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 source "${XDG_CONFIG_HOME:-$HOME/.config}/restrc"
5636
b3484a112300 mod_rest/rest.sh: Update to use httpie-oauth2 plugin
Kim Alvefur <zash@zash.se>
parents: 5432
diff changeset
17
b3484a112300 mod_rest/rest.sh: Update to use httpie-oauth2 plugin
Kim Alvefur <zash@zash.se>
parents: 5432
diff changeset
18 if [ -z "${SCOPE:-}" ]; then
b3484a112300 mod_rest/rest.sh: Update to use httpie-oauth2 plugin
Kim Alvefur <zash@zash.se>
parents: 5432
diff changeset
19 SCOPE="openid xmpp"
b3484a112300 mod_rest/rest.sh: Update to use httpie-oauth2 plugin
Kim Alvefur <zash@zash.se>
parents: 5432
diff changeset
20 fi
5281
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 fi
5432
1c52efb6fd42 mod_rest/rest.sh: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 5431
diff changeset
22
5281
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 if [[ $# == 0 ]]; then
5636
b3484a112300 mod_rest/rest.sh: Update to use httpie-oauth2 plugin
Kim Alvefur <zash@zash.se>
parents: 5432
diff changeset
24 echo "${0##*/} [-h HOST] [/path] kind=(message|presence|iq) ...."
5281
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 # Last arguments are handed to HTTPie, so refer to its docs for further details
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 exit 0
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 fi
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 if [[ "$1" == "-h" ]]; then
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 HOST="$2"
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 shift 2
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 elif [ -z "${HOST:-}" ]; then
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 HOST="$(hostname)"
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 fi
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 if [[ "$HOST" != *.* ]]; then
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 # Assumes subdomain of your DOMAIN
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 if [ -z "${DOMAIN:-}" ]; then
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 DOMAIN="$(hostname -d)"
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 fi
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 if [[ "$HOST" == *:* ]]; then
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 HOST="${HOST%:*}.$DOMAIN:${HOST#*:}"
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 else
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 HOST="$HOST.$DOMAIN"
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 fi
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 fi
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 # For e.g /disco/example.com and such GET queries
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 GET_PATH=""
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 if [[ "$1" == /* ]]; then
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 GET_PATH="$1"
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 shift 1
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 fi
4ed65a6c2a6a mod_rest: Add an example bash script for using mod_rest
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55
5636
b3484a112300 mod_rest/rest.sh: Update to use httpie-oauth2 plugin
Kim Alvefur <zash@zash.se>
parents: 5432
diff changeset
56 https --check-status -p b --session rest -A oauth2 -a "$HOST" --oauth2-scope "$SCOPE" "$HOST/rest$GET_PATH" "$@"