anastasis-authorization-post.sh (3224B)
1 #!/bin/bash 2 # This file is in the public domain. 3 set -eu 4 5 # You need to provide these: 6 # CLIENT_ID= 7 # CLIENT_SECRET= 8 # ORG_ID= 9 # Check shared secrets 10 if [ -x "$PINGEN_CLIENT_ID" ] 11 then 12 echo "PINGEN_CLIENT_ID not sent in environment" 13 exit 1 14 fi 15 if [ -x "$PINGEN_CLIENT_SECRET" ] 16 then 17 echo "PINGEN_CLIENT_SECRET not sent in environment" 18 exit 1 19 fi 20 if [ -x "$PINGEN_ORG_ID" ] 21 then 22 echo "PINGEN_ORG_ID not sent in environment" 23 exit 1 24 fi 25 26 ENDPOINT="https://api.pingen.com" 27 LOGS="$PWD/authorization-post.log" 28 29 MESSAGE=$(cat -) 30 ADDR="$1" 31 NAME=$(echo "$ADDR" | jq -r .full_name) 32 STREET=$(echo "$ADDR" | jq -r .street) 33 CITY=$(echo "$ADDR" | jq -r .city) 34 POSTCODE=$(echo "$ADDR" | jq -r .postcode) 35 COUNTRY=$(echo "$ADDR" | jq -r .country) 36 37 MYDIR=$(mktemp -d /tmp/authorization-post-XXXXXX) 38 cd "$MYDIR" 39 cat - | sed -e "s/%NAME%/$NAME/g" \ 40 -e "s/%STREET%/$STREET/g" \ 41 -e "s/%POSTCODE%/$POSTCODE/g" \ 42 -e "s/%CITY%/$CITY/g" \ 43 -e "s/%COUNTRY%/$COUNTRY/g" > input.tex <<EOF 44 \NeedsTeXFormat{LaTeX2e} 45 \documentclass[fontsize=11pt,a4paper]{scrlttr2} 46 \makeatletter 47 \KOMAoptions{foldmarks=off} 48 \makeatother 49 \setkomavar{subject}{Anastasis Recovery} 50 \address{Anastasis SARL \\\\ 7 rue de Mondorf \\\\ 5431 Erpeldange} 51 \setkomavar{signature}{Anastasis SARL} 52 \date{\today} 53 \begin{document} 54 \begin{letter}{\ \ %NAME% \\\\ \ \ %STREET% \\\\ \ \ %POSTCODE% %CITY% \\\\ \ \ %COUNTRY% } 55 \opening{To whom it may concern,} 56 EOF 57 echo "$MESSAGE" >> input.tex 58 cat - >> input.tex <<EOF 59 \closing{Best regards} 60 \end{letter} 61 \end{document} 62 EOF 63 pdflatex input.tex > "$MYDIR/pdflatex.out" 2> "$MYDIR/pdflatex.err" 64 65 REPLY=$(curl \ 66 -s \ 67 -X POST \ 68 -H "Content-Type: application/x-www-form-urlencoded" \ 69 --data-urlencode "grant_type=client_credentials" \ 70 --data-urlencode "client_id=$CLIENT_ID" \ 71 --data-urlencode "client_secret=$CLIENT_SECRET" \ 72 --data-urlencode "scope=letter" \ 73 https://identity.pingen.com/auth/access-tokens) 74 75 ACCESS_TOKEN=$(echo "$REPLY" | jq -r .access_token) 76 77 REPLY=$(curl -s \ 78 -X GET "$ENDPOINT/file-upload" \ 79 -H "Authorization: Bearer $ACCESS_TOKEN") 80 ATTRS=$(echo "$REPLY" | jq .data.attributes) 81 UPLOAD_URL=$(echo "$ATTRS" | jq -r .url) 82 URL_SIG=$(echo "$ATTRS" | jq -r .url_signature) 83 84 curl -s \ 85 -X PUT \ 86 -T input.pdf \ 87 "$UPLOAD_URL" 88 89 REQUEST="$(jq -n ' 90 { data: { 91 type: "letters", 92 attributes: { 93 file_original_name: "input.pdf", 94 file_url: $UPLOAD_URL, 95 file_url_signature: $URL_SIG, 96 address_position: "left", 97 delivery_product: "cheap", 98 print_mode: "duplex", 99 auto_send: true, 100 print_spectrum: "grayscale" 101 } } 102 }' \ 103 --arg UPLOAD_URL "$UPLOAD_URL" \ 104 --arg URL_SIG "$URL_SIG" \ 105 )" 106 107 STATUS=$(curl -s \ 108 --request POST \ 109 --url "$ENDPOINT/organisations/${ORG_ID}/letters" \ 110 --header "Content-Type: application/vnd.api+json" \ 111 --header "Authorization: Bearer $ACCESS_TOKEN" \ 112 -d "$REQUEST" \ 113 -o "$MYDIR/final-reply.txt" \ 114 -w "%{http_code}" -s) 115 cat "$MYDIR/final-reply.txt" >> "$LOGS" 116 case "$STATUS" in 117 201) 118 ;; 119 *) 120 echo "Failed to add letter: $STATUS" >> "$LOGS" 121 echo "$REPLY" 122 exit 1; 123 ;; 124 esac 125 126 rm -r "$MYDIR" 127 exit 0