From 3ec7aef28c5523d15f7c677ab29dfc30b3b5fc2f Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 11 May 2013 12:21:59 +0200 Subject: Add API key support Signed-off-by: Florian Pritz --- fb-helper.c | 24 +++++++++++++++++++----- fb.1 | 4 ++++ fb.in | 14 +++++++++++++- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/fb-helper.c b/fb-helper.c index c8e4256..6e2a7fb 100644 --- a/fb-helper.c +++ b/fb-helper.c @@ -51,6 +51,7 @@ struct options { int debug; char *url; char *file; + char *apikeyfile; }; /* load the contents of file fn into data */ @@ -222,6 +223,7 @@ void display_help() printf(" -h This help\n"); printf(" -u URL of pastebin or URL to download\n"); printf(" -f File to upload to URL\n"); + printf(" -a Path to API key file\n"); } int main(int argc, char *argv[]) @@ -251,7 +253,8 @@ int main(int argc, char *argv[]) struct options options = { .debug = 0, .file = NULL, - .url = NULL + .url = NULL, + .apikeyfile = NULL }; if (argc == 1) { @@ -259,7 +262,7 @@ int main(int argc, char *argv[]) exit(0); } - while ((opt = getopt(argc, argv, "Du:f:m:h")) != -1) { + while ((opt = getopt(argc, argv, "Du:f:m:a:h")) != -1) { switch (opt) { case 'D': options.debug = 1; break; @@ -267,6 +270,8 @@ int main(int argc, char *argv[]) case 'f': options.file = optarg; break; + case 'a': options.apikeyfile = optarg; break; + case 'h': display_help(); exit(0); @@ -296,6 +301,18 @@ int main(int argc, char *argv[]) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); } + if (options.apikeyfile) { + curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "apikey", + CURLFORM_FILECONTENT, options.apikeyfile, + CURLFORM_END); + } else { + /* use .netrc settings for authentication if available */ + curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); + } + + /* if we have a file to upload, add it as a POST request */ if (options.file) { struct stat statbuf; @@ -358,9 +375,6 @@ int main(int argc, char *argv[]) curl_easy_setopt(curl, CURLOPT_URL, options.url); curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent); - /* use .netrc settings for authentication if available */ - curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL); - /* bail if the upload stalls for 30 seconds */ curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 30L); diff --git a/fb.1 b/fb.1 index 26077d5..e2ba33e 100644 --- a/fb.1 +++ b/fb.1 @@ -91,6 +91,8 @@ Display debugging information. .El .Sh CONFIGURATION FILES .Bl -tag +.It $XDG_CONFIG_HOME/fb-client/apikey +This file contains the API key to use for authentication. If this file exists, netrc authentication will be disabled. .It $XDG_CONFIG_HOME/fb-client/config This file allows to override certain variables by using the format 'option_name="value"'. Both, the option value and name, are case-sensitive string literals. @@ -101,6 +103,8 @@ The following option names are supported: The URL of the pastebin you want to use .It clipboard_cmd The command used to copy URLs of uploaded files to the clipboard. This defaults to xclip or pbcopy on Darwin. +.It apikey_file +The file that contains the API key. This defaults to "$XDG_CONFIG_HOME/fb-client/apikey" .El .It ~/.netrc This file will be used for authentication. diff --git a/fb.in b/fb.in index 3ab25db..cc79e21 100644 --- a/fb.in +++ b/fb.in @@ -32,6 +32,8 @@ if [ -z "$XDG_CONFIG_HOME" ]; then XDG_CONFIG_HOME="$HOME/.config" fi +apikey_file="$XDG_CONFIG_HOME/fb-client/apikey" + config_file="$XDG_CONFIG_HOME/fb-client/config" if [ -e "$config_file" ]; then . "$config_file" @@ -74,6 +76,10 @@ request_helper() { helperopts="$helperopts -D" fi + if [ -e "$apikey_file" ]; then + helperopts="$helperopts -a $apikey_file" + fi + if [ "$mode" = "d" ]; then $libdir/fb-helper $helperopts -u "$url" fi @@ -83,7 +89,13 @@ request_helper() { fi else useragent="fb-client/shell-$version" - curlopts="-# -n -L -A $useragent --speed-time 30 --speed-limit 1 --connect-timeout 10" + curlopts="-# -L -A $useragent --speed-time 30 --speed-limit 1 --connect-timeout 10" + + if [ -e "$apikey_file" ]; then + curlopts="$curlopts --data-urlencode 'apikey@$apikey_file'" + else + curlopts="$curlopts -n" + fi if [ "$debug" ]; then curlopts="$curlopts -v" -- cgit v1.2.3-24-g4f1b From 8786eeac017c2247daebbcc57b0cf5594f25986a Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 11 Aug 2013 18:27:01 +0200 Subject: make curlopts and useragent global variables Needed for apikey patch. Signed-off-by: Florian Pritz --- fb.in | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fb.in b/fb.in index cc79e21..762eb88 100644 --- a/fb.in +++ b/fb.in @@ -50,6 +50,8 @@ display_history= clipboard="" exitcode=0 debug= +useragent="fb-client/shell-$version" +default_curlopts="-# -L -A $useragent --speed-time 30 --speed-limit 1 --connect-timeout 10" base64_encode() { if type base64 2>&1 >/dev/null; then @@ -88,8 +90,9 @@ request_helper() { $libdir/fb-helper $helperopts -u "$url" -f "$file" fi else - useragent="fb-client/shell-$version" - curlopts="-# -L -A $useragent --speed-time 30 --speed-limit 1 --connect-timeout 10" + curlopts="$default_curlopts" + + require_executable curl if [ -e "$apikey_file" ]; then curlopts="$curlopts --data-urlencode 'apikey@$apikey_file'" -- cgit v1.2.3-24-g4f1b From 1c514095696ac48f9a127d278c29608a3c89c767 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 11 Aug 2013 18:27:46 +0200 Subject: Add -a option to generate a new api key Signed-off-by: Florian Pritz --- fb.in | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/fb.in b/fb.in index 762eb88..c271605 100644 --- a/fb.in +++ b/fb.in @@ -47,6 +47,7 @@ get= tar= compress=0 display_history= +create_apikey= clipboard="" exitcode=0 debug= @@ -219,6 +220,11 @@ read_stdin() { cat > "$1" } +read_string() { + read -r tmp + echo "$tmp" +} + id_from_arg() { if printf "%s" "$1" | grep -qE "^https?://"; then printf "%s" "$1" | sed -r 's/https?:\/\/[^\/]+\/([^\/]+).*/\1/' @@ -227,6 +233,44 @@ id_from_arg() { fi } +create_apikey() { + if [ -z "$HOST" ]; then + HOST=`hostname` + fi + + require_executable curl + + printf "%s" "Username: " + read_string | tr -d "\n" > "$tmpdir/username" + + printf "%s" "Password: " + stty -echo + read_string | tr -d "\n" > "$tmpdir/password" + stty echo + printf "\n" + + curlopts="$default_curlopts" + if [ "$debug" ]; then + curlopts="$curlopts -v" + fi + + curl $curlopts -w "%{http_code}\n" -s -F "username=<$tmpdir/username" -F "password=<$tmpdir/password" -F "comment=fb-client $USER@$HOST" "$pastebin/user/create_apikey" > "$tmpdir/api-result" + + rm "$tmpdir/username" "$tmpdir/password" + + status_code=`tail -n 1 "$tmpdir/api-result"` + + if [ "$status_code" == "200" ]; then + head -n 1 "$tmpdir/api-result" > "$apikey_file" + return 0 + fi + + echo "Failed to generate API key:" >&2 + sed '$d' "$tmpdir/api-result" >&2 + + return 1 +} + help() { cat <] -h this help -v show the client version -H display an upload history + -a create a new api key Options: -e extension for default highlighting (e.g. "diff") @@ -258,7 +303,7 @@ if ! type getopts >/dev/null 2>&1; then exit 1 fi -while getopts "e:n:gdhHtcvD" option; do +while getopts "e:n:gdhHatcvD" option; do case $option in e) extension="$OPTARG";; n) filename="$OPTARG";; @@ -267,6 +312,7 @@ while getopts "e:n:gdhHtcvD" option; do t) tar=1;; d) delete=1;; H) display_history=1;; + a) create_apikey=1;; v) printf "%s\n" "$version"; exit 0;; D) debug=1;; h|\?) help; exit 0;; @@ -306,7 +352,9 @@ if [ "$delete" ] || [ "$get" ]; then done elif [ "$display_history" ]; then request_helper d "$pastebin/file/upload_history" || exitcode=1 - +elif [ "$create_apikey" ]; then + create_apikey + exit $? elif [ $# -eq 0 ]; then if [ "$tar" ]; then printf "%s\n" "Error: -t is not supported when operating on stdin" >&2 -- cgit v1.2.3-24-g4f1b