From d967936fde7f8acab81d69470affc0a21def6c47 Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Tue, 26 Oct 2021 00:10:52 +0100 Subject: [PATCH] Functions: adds httpie helpers for xsrf The functions are wrappers around http and https and set the x-xsrf-token header based on the value of the Xsrf-token cookie in the session file. Currently, the cookie name and header name are hard coded. I may look to specify those in an argument at some point although for now it's probably fine. --- shells/shared/functions | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/shells/shared/functions b/shells/shared/functions index 6749e596..bfda255a 100644 --- a/shells/shared/functions +++ b/shells/shared/functions @@ -325,3 +325,17 @@ function cat(){ /usr/bin/bat "$@" fi } + +# These functions extend httpie in order to add an X-XSRF-TOKEN header based on the value of a cookie. +http-xsrf(){ + sessionfile="$(echo "$*" | grep -Eo -- '--session[= ][^ ]*' | sed 's/^--session[= ]//')" + cookie="$(jq -r '.cookies."XSRF-TOKEN".value' "$sessionfile")" + http "$@" "X-XSRF-TOKEN:$cookie" +} + +https-xsrf(){ + sessionfile="$(echo "$*" | grep -Eo -- '--session[= ][^ ]*' | sed 's/^--session[= ]//')" + cookie="$(jq -r '.cookies."XSRF-TOKEN".value' "$sessionfile")" + https "$@" "X-XSRF-TOKEN:$cookie" +} +