From 0f059368a78d86c54fc853866b970151ed82eeea Mon Sep 17 00:00:00 2001 From: Tao Lin Date: Wed, 3 Nov 2021 11:47:55 +0800 Subject: [PATCH 01/14] Add gitlab login support. Signed-off-by: Tao Lin --- bin/hedgedoc | 48 ++++++++-- bin/login-hedgedoc-via-gitlab.sh | 157 +++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+), 8 deletions(-) create mode 100755 bin/login-hedgedoc-via-gitlab.sh diff --git a/bin/hedgedoc b/bin/hedgedoc index f6fa78f..7d473a5 100755 --- a/bin/hedgedoc +++ b/bin/hedgedoc @@ -14,6 +14,10 @@ IFS=$'\n' ### Load config SCRIPTNAME="$(basename "$0")" + +# the extra helpers will be located in the same directory +HELPER_SCRIPT_DIR=$(dirname $(readlink -e $0 2>/dev/null) 2>/dev/null) + XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" HEDGEDOC_SERVER="${HEDGEDOC_SERVER:-http://127.0.0.1:3000}" @@ -56,7 +60,7 @@ Commands: Delete the given note from the server. - login --email|--ldap [username] [password] + login --email|--ldap|--gitlab [username] [password] Authenticate the CLI with the server. (If not passed as args, user & passsword will be asked for via stdin) Stores session key in \$HEDGEDOC_COOKIES_FILE=$HEDGEDOC_COOKIES_FILE/ @@ -259,11 +263,36 @@ function export_note() { esac } +function user_login_helper_common() { + local login_url=$1; shift + local username_arg=$1; shift + local username=$1; shift + local password=$1; shift + curl \ + --request POST \ + --silent \ + --cookie-jar "$HEDGEDOC_COOKIES_FILE" \ + --data-urlencode "$username_arg=$username" \ + --data-urlencode "password=$password" \ + "${HEDGEDOC_SERVER}/${login_url}" > /dev/null + +} + +function user_login_helper_gitlab() { + local login_url=$1; shift + local username_arg=$1; shift + local username=$1; shift + local password=$1; shift + + bash ${HELPER_SCRIPT_DIR}/login-hedgedoc-via-gitlab.sh $username $password >& /dev/null +} + function user_login() { local method="${1#--}" username="${2:-}" password="${3:-}" username_arg="" case "$method" in email) local username_arg="email" login_url="login";; ldap) local username_arg="username" login_url="auth/ldap";; + gitlab) local username_arg="username" login_url="auth/gitlab";; *) echo "Error: Unrecognized login method '--$method'." echo "Usage: $SCRIPTNAME login --email|--ldap [username] [password]" @@ -276,13 +305,16 @@ function user_login() { [[ ! "$password" ]] && password="$(read_password "Please enter your password: ")" echo "" >&2 - curl \ - --request POST \ - --silent \ - --cookie-jar "$HEDGEDOC_COOKIES_FILE" \ - --data-urlencode "$username_arg=$username" \ - --data-urlencode "password=$password" \ - "${HEDGEDOC_SERVER}/${login_url}" > /dev/null + case "$method" in + email) + user_login_helper_common ${login_url} ${username_arg} ${username} ${password};; + ldap) + user_login_helper_common ${login_url} ${username_arg} ${username} ${password};; + gitlab) + user_login_helper_gitlab ${login_url} ${username_arg} ${username} ${password};; + *) + user_login_helper_common ${login_url} ${username_arg} ${username} ${password};; + esac if is_authenticated; then echo "Logged in to $HEDGEDOC_SERVER as $username using $method auth." diff --git a/bin/login-hedgedoc-via-gitlab.sh b/bin/login-hedgedoc-via-gitlab.sh new file mode 100755 index 0000000..87df95b --- /dev/null +++ b/bin/login-hedgedoc-via-gitlab.sh @@ -0,0 +1,157 @@ +#!/bin/bash +username=${1}; shift +password=${1}; shift + +if [ -z "$username" ]; then + echo "Please pass the username and password. " + exit -1 +fi +if [ -z "$password." ]; then + echo "Please pass the password. " + exit -1 +fi + +export HEDGEDOC_SERVER=${HEDGEDOC_SERVER} +if [ -z "$HEDGEDOC_SERVER" ]; then + echo "Please setup the HEDGEDOC_SERVER. " 1>&2 + exit -1 +fi +cookie=${HEDGEDOC_COOKIES_FILE} +if [ -z "$HEDGEDOC_COOKIES_FILE" ]; then + echo "Please setup the HEDGEDOC_COOKIES_FILE. " 1>&2 + exit -1 +fi + +hedgedochost=$(echo $HEDGEDOC_SERVER | perl -ne 'print "$1" if /(https?:\/\/[^:^\/\\s]+)\//') + +function step1() { + echo + echo "Step 1:" + local headers + headers=$(curl "${HEDGEDOC_SERVER}/auth/gitlab" \ + -H "authority: ${hedgedochost}" \ + -H 'pragma: no-cache' \ + -H 'cache-control: no-cache' \ + -H 'upgrade-insecure-requests: 1' \ + -H "referer: ${HEDGEDOC_SERVER}/" \ + -b $cookie -c $cookie --insecure \ + -s -o /dev/null \ + -D - | grep location | cut -c11-) + export GITLAB_CALLBACK_LOCATION=${headers//[$'\t\r\n']} # need to remove CRLF + echo "GITLAB_CALLBACK_LOCATION is $GITLAB_CALLBACK_LOCATION" + # get the host + export GITLAB_HOST=$(echo $GITLAB_CALLBACK_LOCATION | perl -ne 'print "$1" if /(https?:\/\/[^:^\/\\s]+)\//') + echo "GITLAB_HOST is $GITLAB_HOST" # with http/https prefix +} + +function step2() { + echo + echo "Step 2:" + + local body + body=$(curl $GITLAB_CALLBACK_LOCATION \ + -H 'Connection: keep-alive' \ + -H 'Pragma: no-cache' \ + -H 'Cache-Control: no-cache' \ + -H 'Upgrade-Insecure-Requests: 1' \ + -b $cookie -c $cookie \ + --insecure) + export GITLAB_LOGIN_LOCATION=$(echo "$body" | perl -ne 'print "$1" if /.*?a href="(.+?)"/') + echo "GITLAB_LOGIN_LOCATION is $GITLAB_LOGIN_LOCATION" +} + +function step3() { + echo + echo "Step 3:" + local body + local gitlab_token + local token + + # https://stackoverflow.com/questions/47948887/login-to-gitlab-with-username-and-password-using-curl + body=$(curl $GITLAB_LOGIN_LOCATION \ + -H 'Connection: keep-alive' \ + -H 'Pragma: no-cache' \ + -H 'Cache-Control: no-cache' \ + -H 'Upgrade-Insecure-Requests: 1' \ + -b $cookie -c $cookie --insecure) + gitlab_token=$( echo "$body" | grep 'authenticity_token' | perl -ne 'print "$1\n" if /.*?authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p ) + echo + echo "GET TOKEN" + echo $gitlab_token + token= + if [ -n "$gitlab_token" ]; then + token="--data-urlencode authenticity_token=${gitlab_token}" + fi + local gitlab_login_real=$( echo "$body" | grep 'form' | perl -ne 'print "$1\n" if /.*?action="(.+?)"[[:blank:]]/' | sed -n 1p ) + + export GITLAB_LOGIN_REAL_LOCATION="${GITLAB_HOST}${gitlab_login_real}" + echo "GITLAB_LOGIN_REAL_LOCATION is $GITLAB_LOGIN_REAL_LOCATION" # with http/https prefix + + + local header + header=$(curl $GITLAB_LOGIN_REAL_LOCATION \ + -H 'Connection: keep-alive' \ + -H 'Pragma: no-cache' \ + -H 'Cache-Control: no-cache' \ + -H 'Upgrade-Insecure-Requests: 1' \ + -H "Origin: ${GITLAB_HOST}" \ + -H 'Content-Type: application/x-www-form-urlencoded' \ + -H "Referer: ${GITLAB_LOGIN_LOCATION}" \ + -H 'Accept-Language: en' \ + --data "grant_type=password&username=${username}&password=${password}" ${token} \ + -b $cookie -c $cookie \ + --insecure -D - | grep Location | cut -c11-) + header=${header//[$'\t\r\n']} + if [ "$header" = "$GITLAB_CALLBACK_LOCATION" ]; then + echo "same callback $header" + else + echo "Mismatch callback: " 1>&2 + echo "header: $header " 1>&2 + echo "callback: $GITLAB_CALLBACK_LOCATION" 1>&2 + exit -1 + fi + +} + +function step4-5() { + echo + echo "Step 4:" + local body + local link + body=$(curl $GITLAB_CALLBACK_LOCATION \ + -H 'Connection: keep-alive' \ + -H 'Pragma: no-cache' \ + -H 'Cache-Control: no-cache' \ + -H 'Upgrade-Insecure-Requests: 1' \ + -b $cookie -c $cookie \ + --insecure) + + link=$(echo $body | grep window.location | perl -ne 'print "$1\n" if /.*?window.location= "(.+?)";/') + echo "WILL REDIRECT TO $link " + + + + echo + echo "Step 5:" + curl $link \ + -H "authority: ${hedgedochost}" \ + -H 'pragma: no-cache' \ + -H 'cache-control: no-cache' \ + -b $cookie -c $cookie \ + --compressed --insecure + + echo + echo "DONE" + +} + +# gitlab +# check + +if [ ! -f "$cookie" ]; then + step1 + step2 + step3 + step4-5 +fi + From b8d5b3c7e244bf2af04d6cfd2f8699aef7494079 Mon Sep 17 00:00:00 2001 From: Tao Lin Date: Wed, 3 Nov 2021 12:02:07 +0800 Subject: [PATCH 02/14] In order to access several envvars in the sub-shell, need to 'export' them. Signed-off-by: Tao Lin --- bin/hedgedoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/hedgedoc b/bin/hedgedoc index 7d473a5..44fa28c 100755 --- a/bin/hedgedoc +++ b/bin/hedgedoc @@ -20,13 +20,13 @@ HELPER_SCRIPT_DIR=$(dirname $(readlink -e $0 2>/dev/null) 2>/dev/null) XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" -HEDGEDOC_SERVER="${HEDGEDOC_SERVER:-http://127.0.0.1:3000}" +export HEDGEDOC_SERVER="${HEDGEDOC_SERVER:-http://127.0.0.1:3000}" OLD_CODIMD_CONFIG_DIR="${HEDGEDOC_CONFIG_DIR:-$XDG_CONFIG_HOME/codimd}" HEDGEDOC_CONFIG_DIR="${HEDGEDOC_CONFIG_DIR:-$XDG_CONFIG_HOME/$SCRIPTNAME}" [ -e "$OLD_CODIMD_CONFIG_DIR" ] && [ ! -e "$HEDGEDOC_CONFIG_DIR" ] && mv "$OLD_CODIMD_CONFIG_DIR" "$HEDGEDOC_CONFIG_DIR" -HEDGEDOC_COOKIES_FILE="${HEDGEDOC_COOKIES_FILE:-$HEDGEDOC_CONFIG_DIR/key.conf}" +export HEDGEDOC_COOKIES_FILE="${HEDGEDOC_COOKIES_FILE:-$HEDGEDOC_CONFIG_DIR/key.conf}" # Auto-create XDG compliant config dir (https://www.ctrl.blog/entry/xdg-basedir-scripting) mkdir -p "$HEDGEDOC_CONFIG_DIR" From 9cd3f8bafba258dbc061728f670860e299882d8e Mon Sep 17 00:00:00 2001 From: Hugo Peixoto Date: Wed, 3 Nov 2021 15:18:49 +0000 Subject: [PATCH 03/14] Clarify import note_id parameter requirements Signed-off-by: Hugo Peixoto Signed-off-by: Tao Lin --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 7f995b3..1d1a37b 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,14 @@ hedgedoc import test.md $ hedgedoc import [note_id] # takes a local path to a text file, and an optional note id for the new note qhmNmwmxSmK1H2oJmkKBQQ # returns on success ``` + You can open the new note on the server by going to `$HEDGEDOC_SERVER/`. +The optional `note_id` is only available on servers with `allowFreeURL` +enabled. +Check the [documentation](https://docs.hedgedoc.org/configuration/#users-and-privileges) +for more information. + ### Publish an existing note ```bash From 9ed7e4398822adaddb274b6971331c17874ed36e Mon Sep 17 00:00:00 2001 From: Tao Lin Date: Thu, 4 Nov 2021 17:32:37 +0800 Subject: [PATCH 04/14] the location could be lower case in header. Signed-off-by: Tao Lin Signed-off-by: Tao Lin --- bin/login-hedgedoc-via-gitlab.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/login-hedgedoc-via-gitlab.sh b/bin/login-hedgedoc-via-gitlab.sh index 87df95b..7de6856 100755 --- a/bin/login-hedgedoc-via-gitlab.sh +++ b/bin/login-hedgedoc-via-gitlab.sh @@ -100,7 +100,7 @@ function step3() { -H 'Accept-Language: en' \ --data "grant_type=password&username=${username}&password=${password}" ${token} \ -b $cookie -c $cookie \ - --insecure -D - | grep Location | cut -c11-) + --insecure -D - | grep -i location | cut -c11-) header=${header//[$'\t\r\n']} if [ "$header" = "$GITLAB_CALLBACK_LOCATION" ]; then echo "same callback $header" From b8f91585aea323d9fee47604fa2dfbfcc36cd6b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=BCrkop?= Date: Fri, 4 Feb 2022 15:55:34 +0100 Subject: [PATCH 05/14] Improve docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Axel Dürkop Signed-off-by: Tao Lin --- README.md | 97 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 1d1a37b..0eba75f 100644 --- a/README.md +++ b/README.md @@ -10,64 +10,55 @@ For more background, see the initial [discussion](https://github.com/hackmdio/co ## Install Dependencies: - - A HedgeDoc server running somewhere - - `curl` (install via `apt install curl` or `brew install curl` on Mac) - - `wget` (install via `apt install wget` or `brew install wget` on Mac) - - `jq` (install via `apt install jq` or `brew install jq` on Mac) + +- A HedgeDoc server running somewhere +- `curl` (install via `apt install curl` or `brew install curl` on Mac) +- `wget` (install via `apt install wget` or `brew install wget` on Mac) +- `jq` (install via `apt install jq` or `brew install jq` on Mac) ```bash git clone https://github.com/hedgedoc/cli cd cli/bin # optionally symlink the hedgedoc script somewhere into your $PATH +# might need admin rights (sudo) ln -s $PWD/hedgedoc /usr/local/bin/hedgedoc +# check if the new command exists. You should see the documentation +hedgedoc + # set HEDGEDOC_SERVER environment variable to your server's URL # it defaults to http://127.0.0.1:3000 +# do this in .profile and/or .bashrc export HEDGEDOC_SERVER='https://hedgedoc.example.com' +# log out and in again to the terminal to read the new variable -# Test by creating a new note -hedgedoc login --email +# Test by creating a new note with FREELY access, no login required +# You will receive the generated hash for the document +# Caution: You won't have the right to delete the new document! +echo "# HedgeDoc!" > test.md hedgedoc import test.md -``` - -## Documentation -### Create/import a new note -```bash -$ hedgedoc import [note_id] # takes a local path to a text file, and an optional note id for the new note -qhmNmwmxSmK1H2oJmkKBQQ # returns on success +# check for the document in the browser by concatenating the +# address of your server and the hash ``` -You can open the new note on the server by going to `$HEDGEDOC_SERVER/`. +## Documentation -The optional `note_id` is only available on servers with `allowFreeURL` -enabled. -Check the [documentation](https://docs.hedgedoc.org/configuration/#users-and-privileges) -for more information. +It's not necessary to authenticate against the server in order to use `hedgedoc-cli`. But without authentication you won't have access to the non-FREELY documents and everything that's accessible behind the login. -### Publish an existing note +### Variants of authentication -```bash -$ hedgedoc publish qhmNmwmxSmK1H2oJmkKBQQ # takes a -S1ok9no3f # returns public note id -``` -You can open the published note on the server by going to `$HEDGEDOC_SERVER/s/`. +#### Authenticate with cookie -### Export an existing note +Authentication with a cookie is so far the only way if you login with GitLab and the like. Use browser extensions like [Get cookies.txt](https://chrome.google.com/webstore/detail/get-cookiestxt/bgaddhkoddajcdgocldbbfleckgcbcid) to store the cookie in `key.conf`. -```bash -$ hedgedoc export --pdf qhmNmwmxSmK1H2oJmkKBQQ # takes a , outputs to .pdf by default -$ hedgedoc export --md qhmNmwmxSmK1H2oJmkKBQQ my_note.md # or you can specify an output path explicitly -$ hedgedoc export --html qhmNmwmxSmK1H2oJmkKBQQ -$ hedgedoc export --slides qhmNmwmxSmK1H2oJmkKBQQ my_slides.zip -``` +Possible you have many lines in `key.conf`. You only need the line with `connect.sid` followed by a long hash! -### Authenticate and get notes history +Optionally add the HEDGEDOC_COOKIES_FILE environment variable to specify where cookies will be stored. It defaults to `~/.config/hedgedoc/key.conf` ```bash -# optionally add the HEDGEDOC_COOKIES_FILE environment variable to specify -# where cookies will be stored. It defaults to ~/.config/hedgedoc-cli/key.conf -$ export HEDGEDOC_COOKIES_FILE=~/.config/hedgedoc-cli/key.conf +# You can put this in .profile and/or .bashrc, too. +$ export HEDGEDOC_COOKIES_FILE=~/.config/hedgedoc/key.conf ``` #### Authenticate with email @@ -83,7 +74,11 @@ $ hedgedoc login --ldap username p4sW0rD # takes a username and a pas $ hedgedoc login --ldap # or pass them via stdin prompt instead ``` -#### Get auth status, history, and logout +#### Check if authentication works + +If your authentication method is set up correctly the following commands will work. + +### Get auth status, history, and logout ```bash $ hedgedoc profile @@ -110,6 +105,36 @@ You may need to log in again if: - your session expired - the hedgedoc server was restarted (which force-expires all sessions as a side-effect) - the is`$HEDGEDOC_COOKIES_FILE` deleted, moved, or becomes unreadable by `hedgedoc-cli` +### Create/import a new note +```bash +$ hedgedoc import [note_id] # takes a local path to a text file, and an optional note id for the new note +qhmNmwmxSmK1H2oJmkKBQQ # returns on success +``` + +You can open the new note on the server by going to `$HEDGEDOC_SERVER/`. + +The optional `note_id` is only available on servers with `allowFreeURL` +enabled. +Check the [documentation](https://docs.hedgedoc.org/configuration/#users-and-privileges) +for more information. + +### Publish an existing note + +```bash +$ hedgedoc publish qhmNmwmxSmK1H2oJmkKBQQ # takes a +S1ok9no3f # returns public note id +``` +You can open the published note on the server by going to `$HEDGEDOC_SERVER/s/`. + +### Export an existing note + +```bash +$ hedgedoc export --pdf qhmNmwmxSmK1H2oJmkKBQQ # takes a , outputs to .pdf by default +$ hedgedoc export --md qhmNmwmxSmK1H2oJmkKBQQ my_note.md # or you can specify an output path explicitly +$ hedgedoc export --html qhmNmwmxSmK1H2oJmkKBQQ +$ hedgedoc export --slides qhmNmwmxSmK1H2oJmkKBQQ my_slides.zip +``` + ## API Endpoints From 3154487c9ee33c5a24a8a2e3ae803497020edd41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=BCrkop?= Date: Fri, 4 Feb 2022 16:12:32 +0100 Subject: [PATCH 06/14] Update docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Axel Dürkop Signed-off-by: Tao Lin --- README.md | 62 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 0eba75f..5956ffd 100644 --- a/README.md +++ b/README.md @@ -7,44 +7,50 @@ For more background, see the initial [discussion](https://github.com/hackmdio/co *There is an alternative, TypeScript-based CodiMD CLI for `hackmdio/codimd` maintained by the HackMD team here: https://github.com/hackmdio/codimd-cli.* *(it may or may not be compatible with the `hedgedoc/hedgedoc` server)* -## Install +## Installation -Dependencies: +### Dependencies - A HedgeDoc server running somewhere - `curl` (install via `apt install curl` or `brew install curl` on Mac) - `wget` (install via `apt install wget` or `brew install wget` on Mac) - `jq` (install via `apt install jq` or `brew install jq` on Mac) -```bash -git clone https://github.com/hedgedoc/cli -cd cli/bin -# optionally symlink the hedgedoc script somewhere into your $PATH -# might need admin rights (sudo) -ln -s $PWD/hedgedoc /usr/local/bin/hedgedoc - -# check if the new command exists. You should see the documentation -hedgedoc - -# set HEDGEDOC_SERVER environment variable to your server's URL -# it defaults to http://127.0.0.1:3000 -# do this in .profile and/or .bashrc -export HEDGEDOC_SERVER='https://hedgedoc.example.com' -# log out and in again to the terminal to read the new variable - -# Test by creating a new note with FREELY access, no login required -# You will receive the generated hash for the document -# Caution: You won't have the right to delete the new document! -echo "# HedgeDoc!" > test.md -hedgedoc import test.md - -# check for the document in the browser by concatenating the -# address of your server and the hash -``` +Clone the repository. + + $ git clone https://github.com/hedgedoc/cli + +Enter the folder with the script. + + $ cd cli/bin + +Optionally symlink the hedgedoc script somewhere into your $PATH to make it globally accessible. Otherwise you will have to provide the path to the script manually. This command might need admin rights (sudo)! + + $ ln -s $PWD/hedgedoc /usr/local/bin/hedgedoc + +Check if the new command exists. You should see the documentation + + $ hedgedoc + +Set `HEDGEDOC_SERVER` environment variable to your server's URL. It defaults to `http://127.0.0.1:3000` Do this once on the command line or persist it in `.profile` and/or `.bashrc`. + + $ export HEDGEDOC_SERVER='https://hedgedoc.example.com' + +Log out and in again to the terminal to read the new variable. + +Test your configuration by creating a new note with FREELY access and no login required. You will receive the generated hash for the document like `3jXcabSfSNesbH6KT72ieg`. + +**Caution: You won't have the right to delete the new document if not authenticated!** + + $ echo "# HedgeDoc!" > test.md + $ hedgedoc import test.md + +Check for the document in the browser by concatenating the + address of your server and the hash. ## Documentation -It's not necessary to authenticate against the server in order to use `hedgedoc-cli`. But without authentication you won't have access to the non-FREELY documents and everything that's accessible behind the login. +It's not necessary to authenticate against the server in order to make use of `hedgedoc-cli`. But without authentication you won't have access to the non-FREELY documents and everything that's accessible behind the login. ### Variants of authentication From 3c87abb7f6aa2e2dd54ef193c39287215bcd916c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=BCrkop?= Date: Fri, 4 Feb 2022 16:26:38 +0100 Subject: [PATCH 07/14] Fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Axel Dürkop Signed-off-by: Tao Lin --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5956ffd..83e0130 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Set `HEDGEDOC_SERVER` environment variable to your server's URL. It defaults to Log out and in again to the terminal to read the new variable. -Test your configuration by creating a new note with FREELY access and no login required. You will receive the generated hash for the document like `3jXcabSfSNesbH6KT72ieg`. +Test your configuration by creating a new note with FREELY access and no login required. You will receive the generated `` for the document like `3jXcabSfSNesbH6KT72ieg`. **Caution: You won't have the right to delete the new document if not authenticated!** @@ -46,7 +46,7 @@ Test your configuration by creating a new note with FREELY access and no login r $ hedgedoc import test.md Check for the document in the browser by concatenating the - address of your server and the hash. + address of your server and the ``. ## Documentation From 929d5f8f634c82893a2aa1e0b4063587ba4cdf2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=BCrkop?= Date: Thu, 10 Feb 2022 12:01:10 +0100 Subject: [PATCH 08/14] Fix re-reading of variable again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Axel Dürkop Signed-off-by: Tao Lin --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 83e0130..180f351 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Set `HEDGEDOC_SERVER` environment variable to your server's URL. It defaults to $ export HEDGEDOC_SERVER='https://hedgedoc.example.com' -Log out and in again to the terminal to read the new variable. +If you added the variable to `.profile` or `.bashrc`, re-open the terminal to read the new variable. Test your configuration by creating a new note with FREELY access and no login required. You will receive the generated `` for the document like `3jXcabSfSNesbH6KT72ieg`. @@ -111,7 +111,9 @@ You may need to log in again if: - your session expired - the hedgedoc server was restarted (which force-expires all sessions as a side-effect) - the is`$HEDGEDOC_COOKIES_FILE` deleted, moved, or becomes unreadable by `hedgedoc-cli` + ### Create/import a new note + ```bash $ hedgedoc import [note_id] # takes a local path to a text file, and an optional note id for the new note qhmNmwmxSmK1H2oJmkKBQQ # returns on success From 06eaf9d03ca6ae8015fb1a48377b1e5c6f73c128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=BCrkop?= Date: Thu, 10 Feb 2022 12:59:00 +0100 Subject: [PATCH 09/14] improve headlines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Axel Dürkop Signed-off-by: Tao Lin --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 180f351..9a09b00 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ For more background, see the initial [discussion](https://github.com/hackmdio/co - `wget` (install via `apt install wget` or `brew install wget` on Mac) - `jq` (install via `apt install jq` or `brew install jq` on Mac) +### Instructions + Clone the repository. $ git clone https://github.com/hedgedoc/cli @@ -48,12 +50,12 @@ Test your configuration by creating a new note with FREELY access and no login r Check for the document in the browser by concatenating the address of your server and the ``. -## Documentation - -It's not necessary to authenticate against the server in order to make use of `hedgedoc-cli`. But without authentication you won't have access to the non-FREELY documents and everything that's accessible behind the login. +## Configuration and usage ### Variants of authentication +It's not necessary to authenticate against the server in order to make use of `hedgedoc-cli`. But without authentication you won't have access to the non-FREELY documents and everything that's accessible behind the login. + #### Authenticate with cookie Authentication with a cookie is so far the only way if you login with GitLab and the like. Use browser extensions like [Get cookies.txt](https://chrome.google.com/webstore/detail/get-cookiestxt/bgaddhkoddajcdgocldbbfleckgcbcid) to store the cookie in `key.conf`. From 8d7429250a11ee71e1831b4b2b42e2e4860195c0 Mon Sep 17 00:00:00 2001 From: Mathias Petermann Date: Wed, 1 Jun 2022 10:27:55 +0200 Subject: [PATCH 10/14] Add backup cli command, exporting all notes for current user Signed-off-by: Mathias Petermann Signed-off-by: Tao Lin --- bin/hedgedoc | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/bin/hedgedoc b/bin/hedgedoc index 44fa28c..7f8e76e 100755 --- a/bin/hedgedoc +++ b/bin/hedgedoc @@ -81,7 +81,7 @@ Config: \$HEDGEDOC_CONFIG_DIR (defaults to $XDG_CONFIG_HOME/$SCRIPTNAME) \$HEDGEDOC_COOKIES_FILE (defaults to \$HEDGEDOC_CONFIG_DIR/key.conf) -Usage examples: +Usage examples: \$ export HEDGEDOC_SERVER='$HEDGEDOC_SERVER' \$ export HEDGEDOC_COOKIES_FILE=$HEDGEDOC_COOKIES_FILE @@ -178,7 +178,7 @@ function import_note() { echo "Hint: You may need to run '$SCRIPTNAME login' to authenticate first." >&2 return 2 fi - echo "$note_id" + echo "$note_id" } function export_note() { @@ -234,7 +234,7 @@ function export_note() { output_path="${output_path:-$note_id.zip}" # hack: there is no way to get slide output as zipfile directly. # we dump it to folder of html files first then zip it. - + # check for any existing conflicting file/dir that wget downloads local temp_folder_name="$( echo "$HEDGEDOC_SERVER" \ @@ -285,6 +285,18 @@ function user_login_helper_gitlab() { local password=$1; shift bash ${HELPER_SCRIPT_DIR}/login-hedgedoc-via-gitlab.sh $username $password >& /dev/null + +} + +function backup_all_notes() { + output_path="${1:-archive.zip}" + + curl \ + --silent \ + --cookie "$HEDGEDOC_COOKIES_FILE" \ + --output "$output_path" \ + "${HEDGEDOC_SERVER}/me/export" \ + && echo "$output_path" } function user_login() { @@ -381,7 +393,7 @@ function user_profile() { function user_history() { assert_is_authenticated - + curl \ --silent \ --cookie "$HEDGEDOC_COOKIES_FILE" \ @@ -422,6 +434,9 @@ function main() { export) export_note "${1:-}" "${2:-}" "${3:-}" ;; + backup) + backup_all_notes "${1:-}" + ;; delete) delete_note "${1:-}" ;; @@ -448,7 +463,7 @@ function main() { # Do the usage function of no Command line arguments are given. if [[ $# -eq 0 ]]; then set -- "help" -fi +fi # Allow importing funcs without running main by using `source ./bin/hedgedoc --import` if [[ "${1:-}" != "--import" ]]; then From 71685d5e15b4576fb8f0df766dfeb44cb9735215 Mon Sep 17 00:00:00 2001 From: Tao Lin <831611+mirguest@users.noreply.github.com> Date: Sat, 23 Jul 2022 21:31:06 +0800 Subject: [PATCH 11/14] Update bin/login-hedgedoc-via-gitlab.sh Co-authored-by: Erik Michelson Signed-off-by: Tao Lin --- bin/login-hedgedoc-via-gitlab.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/login-hedgedoc-via-gitlab.sh b/bin/login-hedgedoc-via-gitlab.sh index 7de6856..169f168 100755 --- a/bin/login-hedgedoc-via-gitlab.sh +++ b/bin/login-hedgedoc-via-gitlab.sh @@ -6,7 +6,7 @@ if [ -z "$username" ]; then echo "Please pass the username and password. " exit -1 fi -if [ -z "$password." ]; then +if [ -z "$password" ]; then echo "Please pass the password. " exit -1 fi From f4900fc327a640393385d6245af0ed290e31ec23 Mon Sep 17 00:00:00 2001 From: Tao Lin Date: Sat, 23 Jul 2022 21:37:34 +0800 Subject: [PATCH 12/14] use the same shabang. Signed-off-by: Tao Lin --- bin/login-hedgedoc-via-gitlab.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/login-hedgedoc-via-gitlab.sh b/bin/login-hedgedoc-via-gitlab.sh index 169f168..f411c89 100755 --- a/bin/login-hedgedoc-via-gitlab.sh +++ b/bin/login-hedgedoc-via-gitlab.sh @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env bash + username=${1}; shift password=${1}; shift From b346d417f90ac26b12e68b62813c6a8ec1b88e84 Mon Sep 17 00:00:00 2001 From: Tao Lin Date: Sat, 23 Jul 2022 21:45:15 +0800 Subject: [PATCH 13/14] handle the redirectUri. Signed-off-by: Tao Lin --- bin/login-hedgedoc-via-gitlab.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bin/login-hedgedoc-via-gitlab.sh b/bin/login-hedgedoc-via-gitlab.sh index f411c89..4218cad 100755 --- a/bin/login-hedgedoc-via-gitlab.sh +++ b/bin/login-hedgedoc-via-gitlab.sh @@ -126,8 +126,12 @@ function step4-5() { -H 'Upgrade-Insecure-Requests: 1' \ -b $cookie -c $cookie \ --insecure) - + echo $body link=$(echo $body | grep window.location | perl -ne 'print "$1\n" if /.*?window.location= "(.+?)";/') + # if not found, try to parse again + if [ -z "$link" ]; then + link=$(echo $body | grep window.location | perl -ne 'print "$1\n" if /.*?let redirectUri = "(.+?)";/') + fi echo "WILL REDIRECT TO $link " From b60e5f057742492ffa30f0a8824d74bf61847558 Mon Sep 17 00:00:00 2001 From: Tao Lin Date: Sat, 23 Jul 2022 21:46:55 +0800 Subject: [PATCH 14/14] use the same set options Signed-off-by: Tao Lin --- bin/login-hedgedoc-via-gitlab.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bin/login-hedgedoc-via-gitlab.sh b/bin/login-hedgedoc-via-gitlab.sh index 4218cad..d4b8214 100755 --- a/bin/login-hedgedoc-via-gitlab.sh +++ b/bin/login-hedgedoc-via-gitlab.sh @@ -1,4 +1,15 @@ #!/usr/bin/env bash +# Documentation & Code: https://github.com/hedgedoc/cli + +### Bash environment setup +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html +set -o errexit +set -o errtrace +set -o nounset +set -o pipefail + +############################################################################## username=${1}; shift password=${1}; shift