Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,46 @@ codimd export --html qhmNmwmxSmK1H2oJmkKBQQ my_note.html
codimd export --slides qhmNmwmxSmK1H2oJmkKBQQ my_slides.zip
```

### Authenticate and get notes history

```bash
# optionally add the CODIMD_COOKIES_FILE environment variable to specify
# where cookies will be stored. It defaults to ~/.config/codimd-cli/key.conf
export CODIMD_COOKIES_FILE=~/.codimd-key.conf
```
#### Authenticate with email

```bash
codimd login --email email@example.net p4sW0rD # takes an email and a password
```

#### Authenticate with ldap

```bash
codimd login --ldap username p4sW0rD # takes a username and a password
```

#### Get auth status, history, and logout

```bash
codimd profile
You are logged in $CODIMD_SERVER as email with id xxxx-xx[...]xxx.

codimd history
ID Name
0nAp3YRyTlyQ-N3N7lCk-w Note_1
qhmNmwmxSmK1H2oJmkKBQQ Note_2

codimd logout
```

## API Endpoints

These server endpoints are used by this project and can be unstable and undocumented, but may be of use if you're developing your own projects that need API access to CodiMD.

- `https://<codimd_server>/login`
- `https://<codimd_server>/logout`
- `https://<codimd_server>/me`
- `https://<codimd_server>/history` (requires auth)
- `https://<codimd_server>/new`
- `https://<codimd_server>/<note_id>/publish`
Expand Down
134 changes: 134 additions & 0 deletions bin/codimd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ Options:
$ $codi_cli export --html qhmNmwmxSmK1H2oJmkKBQQ output.html
$ $codi_cli export --slides qhmNmwmxSmK1H2oJmkKBQQ output.zip

$ $codi_cli login --email email@example.net p4sW0rD
$ $codi_cli profile
You are logged in as email with id x[...]x.
$ $codi_cli history
ID Name
0nAp3YRyTlyQ-N3N7lCk-w Note_1
qhmNmwmxSmK1H2oJmkKBQQ Note_2
$ $codi_cli delete 0nAp3YRyTlyQ-N3N7lCk-w
$ $codi_cli logout

Example:
$ env CODIMD_SERVER='https://codimd.example.com' $codi_cli import /path/to/your/content.md
"
Expand Down Expand Up @@ -53,12 +63,136 @@ function export_note() {
fi
}

CODIMD_COOKIES_FILE=${CODIMD_COOKIES_FILE:="$HOME/.config/codimd-cli/key.conf"}

mkdir -p ${CODIMD_COOKIES_FILE%$BASE}

function authenticate_email() {
if [[ -z $1 ]] || [[ -z $2 ]]; then
echo "ERROR: You must provide an email and password."
echo ""
echo "Usage: codimd login --email <email> <password>"
echo "For usage examples, see: $codi_cli help"
exit 1
fi

curl -c $CODIMD_COOKIES_FILE \
-XPOST \
-d "email=$1&password=$2" \
$CODIMD_SERVER/login &>/dev/null
}

function authenticate_ldap() {
if [[ -z $1 ]] || [[ -z $2 ]]; then
echo "ERROR: You must provide an email and password."
echo ""
echo "Usage: codimd login --email <username> <password>"
echo "For usage examples, see: $codi_cli help"
exit 1
fi

curl -c $CODIMD_COOKIES_FILE \
-XPOST \
-d "username=$1&password=$2" \
$CODIMD_SERVER/auth/ldap &>/dev/null
}

function authenticate() {
if [[ -z $1 ]]; then
echo "Usage: codimd login [--email|--ldap]"
echo "For usage examples, see: $codi_cli help"
exit 0
fi

if [[ $1 == "--email" ]]; then
shift
authenticate_email "$@"
elif [[ $1 == "--ldap" ]]; then
shift
authenticate_ldap "$@"
else
echo "ERROR: You must provide a protocol."
echo ""
echo "Usage: codimd login [--email|--ldap] <login> <password>"
echo "For usage examples, see: $codi_cli help"
exit 1
fi

if [[ $(is_authenticated) == "ok" ]]; then
echo "Logged in $CODIMD_SERVER as $1 successfully."
else
echo "Failed to login at $CODIMD_SERVER."
exit 1
fi

}

function is_authenticated() {
STATUS=$(curl -b $CODIMD_COOKIES_FILE $CODIMD_SERVER/me 2>/dev/null)

echo $(echo $STATUS | jq -r '.status')
}

function deauthenticate() {
curl -b $CODIMD_COOKIES_FILE -c $CODIMD_COOKIES_FILE $CODIMD_SERVER/logout &>/dev/null
}

function get_profile() {
STATUS=$(curl -b $CODIMD_COOKIES_FILE $CODIMD_SERVER/me 2>/dev/null)

if [[ "$(echo $STATUS | jq -r '.status')" == "forbidden" ]]; then
echo "Error: Need to authenticate."
exit 1
else
NAME=$(echo $STATUS | jq -r '.name')
ID=$(echo $STATUS | jq -r '.id')
echo "You are logged in $CODIMD_SERVER as $NAME with id $ID."
fi
}

function get_history() {
if [[ "$(is_authenticated)" ]]; then
HISTORY=$(curl -b $CODIMD_COOKIES_FILE $CODIMD_SERVER/history 2>/dev/null)
echo $HISTORY | jq -r '["ID", "", "", "Name"], (.history[] | [.id, .text]) | @tsv'
# If the tags are needed, they can be found in .tags
# Same for the date in .time
else
echo "Error: Need to authenticate."
exit 1
fi
}

function delete_history() {
if [[ -z "$1" ]]; then
echo "ERROR: You must specify a note id to delete."
echo ""
echo "Usage: codimd delete <note_id>"
exit 1
elif [[ "$(is_authenticated)" ]]; then
curl -b $CODIMD_COOKIES_FILE -X DELETE $CODIMD_SERVER/history/$1
else
echo "Error: Need to authenticate."
exit 1
fi
}

if [[ "$1" == "import" ]]; then
import_note "$2"
elif [[ "$1" == "publish" ]]; then
publish_note "$2"
elif [[ "$1" == "export" ]]; then
export_note "$2" "$3" "$4"
elif [[ "$1" == "login" ]]; then
shift
authenticate "$@"
elif [[ "$1" == "logout" ]]; then
deauthenticate
elif [[ "$1" == "profile" ]]; then
get_profile
elif [[ "$1" == "history" ]]; then
get_history
elif [[ "$1" == "delete" ]]; then
delete_history "$2"
else
echo "$help_str"
fi