From 1f73faa7e4987f1820d7c2b70b2301592a3e76f5 Mon Sep 17 00:00:00 2001 From: bruno Date: Wed, 10 Aug 2022 12:02:02 -0400 Subject: [PATCH] add comments to code --- code/database_utils.nim | 13 +++++-- code/joplin_json_utils.nim | 5 ++- code/joplin_utils.nim | 72 +++++++++++++++++++++++++++++--------- code/password_utils.nim | 30 +++++++++++----- config/config.cfg | 13 +++++++ 5 files changed, 105 insertions(+), 28 deletions(-) diff --git a/code/database_utils.nim b/code/database_utils.nim index b3d5b80..5eb94fb 100644 --- a/code/database_utils.nim +++ b/code/database_utils.nim @@ -9,11 +9,17 @@ # Copyright 2019 - Thomas T. Jarløv # --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- # -# import db_sqlite, os, parsecfg, strutils, logging -import db_sqlite, os, parsecfg, logging +import os +import logging +import parsecfg +import db_sqlite import ../code/password_utils + +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : Generate Database +# --==--==--==--==--==--==--==--==--==--==-- # proc generateDB*() = echo "Generating database" @@ -69,6 +75,9 @@ proc generateDB*() = echo " - Database: session table already exists" +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : Create Administrator User +# --==--==--==--==--==--==--==--==--==--==-- # proc createAdminUser*(db: DbConn, args: seq[string]) = ## Create new admin user diff --git a/code/joplin_json_utils.nim b/code/joplin_json_utils.nim index e43c008..b850a26 100644 --- a/code/joplin_json_utils.nim +++ b/code/joplin_json_utils.nim @@ -8,7 +8,9 @@ # [TODO] joplin_json_utils : need to create many procedures # --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- # -# Data structure for TAGS +# --==--==--==--==--==--==--==--==--==--==-- # +# TYPE : Data structure for TAGS +# --==--==--==--==--==--==--==--==--==--==-- # type Tag = object id : string parent_id : string @@ -19,3 +21,4 @@ type Tags = object has_more: string + diff --git a/code/joplin_utils.nim b/code/joplin_utils.nim index a5da159..26deea1 100644 --- a/code/joplin_utils.nim +++ b/code/joplin_utils.nim @@ -6,7 +6,6 @@ # joplin_utils : procedure related joplin application # # --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- # - import net import times import osproc @@ -19,29 +18,37 @@ import std/options from os import sleep import std/[asyncdispatch, httpclient] - -# Setup joplin_ping data +# --==--==--==--==--==--==--==--==--==--==-- # +# TYPE : Setup joplin_ping data +# --==--==--==--==--==--==--==--==--==--==-- # type joplin_ping* = object ping_result*: seq[string] ping_status*: bool req*: Request -# Setup joplin_tags data +# --==--==--==--==--==--==--==--==--==--==-- # +# TYPE : Setup joplin_tags data +# --==--==--==--==--==--==--==--==--==--==-- # type joplin_tags* = object id*, parent_id*, title*: seq[string] created_time*, updated_time*, user_created_time*, user_updated_time*, is_shared*: seq[int] req*: Request -# Setup joplin_notebooks data +# --==--==--==--==--==--==--==--==--==--==-- # +# TYPE : Setup joplin_notebooks data +# --==--==--==--==--==--==--==--==--==--==-- # type joplin_notebooks* = object id*, parent_id*, title*, share_id*, icon*: seq[string] created_time*, updated_time*, user_created_time*, user_updated_time*, is_shared*: seq[int] req*: Request -# Setup joplin_notes data + +# --==--==--==--==--==--==--==--==--==--==-- # +# TYPE : Setup joplin_notes data +# --==--==--==--==--==--==--==--==--==--==-- # type joplin_notes* = object id*, parent_id*, title*, body*, author*, source_url*, source*, source_application*, application_data*, share_id*, conflict_original_id*, body_html*, base_url*: seq[string] @@ -49,15 +56,25 @@ type latitude*, longitude*, altitude*, order*:seq[float] req*: Request +# --==--==--==--==--==--==--==--==--==--==-- # +# DURATION : duration variables +# --==--==--==--==--==--==--==--==--==--==-- # let resetDuration = initDuration(seconds=2) deciSecondDuration* = initDuration(milliseconds = 100) qtrsecondDuration* = initDuration(milliseconds = 250) +# --==--==--==--==--==--==--==--==--==--==-- # +# VAR : connexion http variables +# --==--==--==--==--==--==--==--==--==--==-- # var client = newHttpClient() lastConnection = getTime().utc + +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : reset HTTP client +# --==--==--==--==--==--==--==--==--==--==-- # proc resetHttpClient() = if (getTime().utc - lastConnection) > resetDuration: # Probably a new timeout. We have not yet experienced a long outage. @@ -71,8 +88,9 @@ proc resetHttpClient() = echo("Current exception: ", getCurrentExceptionMsg()) client = newHttpClient(timeout=500) - -# [TODO] procédure Ping à finir qui ne fonctionne pas. +# --==--==--==--==--==--==--==--==--==--==--==--==--==-==--==--==- # +# PROC : use the joplin API ping to validate service availibility +# --==--==--==--==--==--==--==--==--==--==--==--==--==-==--==--==- # proc ping_joplin*(token:string): Future[joplin_ping] {.async.} = var j_p: joplin_ping @@ -108,7 +126,9 @@ proc ping_joplin*(token:string): Future[joplin_ping] {.async.} = #let joplin_notes_Json = parseJson(json) - +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : get all joplin NOTEBOOKS +# --==--==--==--==--==--==--==--==--==--==-- # proc get_joplin_notebooks*(token:string): Future[joplin_notebooks] {.async.} = # Variables @@ -152,6 +172,10 @@ proc get_joplin_notebooks*(token:string): Future[joplin_notebooks] {.async.} = return j_nb + +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : get all joplin NOTES +# --==--==--==--==--==--==--==--==--==--==-- # proc get_joplin_notes*(token:string): Future[joplin_notes] {.async.} = # Variables @@ -195,6 +219,9 @@ proc get_joplin_notes*(token:string): Future[joplin_notes] {.async.} = return j_notes +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : get all joplin TAGS +# --==--==--==--==--==--==--==--==--==--==-- # proc get_joplin_tags*(token:string): Future[joplin_tags] {.async.} = # Variables @@ -238,7 +265,9 @@ proc get_joplin_tags*(token:string): Future[joplin_tags] {.async.} = return j_tags -# get the token from Joplin Terminal +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : get the token from Joplin Terminal +# --==--==--==--==--==--==--==--==--==--==-- # proc get_joplin_cli_token*(): string = var flagName: string = "" var flagValue: string = "" @@ -253,44 +282,55 @@ proc get_joplin_cli_token*(): string = return flagValue -# start Joplin Terminal +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : start Joplin Terminal +# --==--==--==--==--==--==--==--==--==--==-- # proc joplin_cli_start*(): bool = var rc = false var result = execCmdEx("joplin server start &") if result.exitCode == 0: - echo result.output + # echo result.output rc = true else: rc = false return rc -# check Joplin Terminal staus +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : check Joplin Terminal staus +# --==--==--==--==--==--==--==--==--==--==-- # proc joplin_cli_status*(): bool = var rc = false var result = execCmdEx("joplin server status") if result.exitCode == 0: - echo result.output + # echo result.output rc = true else: rc = false return rc -# stop Joplin Terminal +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : stop Joplin Terminal +# --==--==--==--==--==--==--==--==--==--==-- # proc joplin_cli_stop*(): bool = var rc = false var result = execCmdEx("joplin server stop") if result.exitCode == 0: - echo result.output + # echo result.output rc = true else: rc = false return rc + +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : +# --==--==--==--==--==--==--==--==--==--==-- # + # proc get_joplin_tags_json*(token:string): Future[] {.async.} = # # url = fmt"http://localhost:41184/notes/{id}?fields=id,parent_id,title,body,created_time,updated_time,is_conflict,latitude,longitude,altitude,author,source_url,is_todo,todo_due,todo_completed,source,source_application,application_data,order,user_created_time,user_updated_time,encryption_cipher_text,encryption_applied,markup_language,is_shared,share_id,conflict_original_id,master_key_id&token={token}" diff --git a/code/password_utils.nim b/code/password_utils.nim index 8a1e776..548dce3 100644 --- a/code/password_utils.nim +++ b/code/password_utils.nim @@ -8,19 +8,24 @@ # Inspiration of : https://ttj.dk/blog/2019/01/20/setup-a-website-with-nim # Copyright 2019 - Thomas T. Jarløv # --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- # - -# import md5, bcrypt, math, random, os import md5 import bcrypt import random randomize() +# --==--==--==--==--==--==--==--==--==--==-- # +# Let and VAR : for random +# --==--==--==--==--==--==--==--==--==--==-- # var urandom: File let useUrandom = urandom.open("/dev/urandom") -proc makeSalt*(): string = - ## Generate random salt. Uses cryptographically secure /dev/urandom - ## on platforms where it is available, and Nim's random module in other cases. + +# --==--==--==--==--==--==--==--==--==--==--=--==--==--=--==--==--=--==--==-- # +# PROC : Make Salt +## : Generate random salt. Uses cryptographically secure /dev/urandom +## : on platforms where it is available, and Nim's random module in other cases. +# --==--==--==--==--==--==--==--==--==--==--=--==--==--=--==--==--=--==--==-- # +proc makeSalt*(): string = result = "" if useUrandom: var randomBytes: array[0..127, char] @@ -32,13 +37,20 @@ proc makeSalt*(): string = for i in 0..127: result.add(chr(rand(94) + 32)) # Generate numbers from 32 to 94 + 32 = 126 -proc makeSessionKey*(): string = - ## Creates a random key to be used to authorize a session. + +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : Make session key +## Creates a random key to be used to authorize a session. +# --==--==--==--==--==--==--==--==--==--==-- # +proc makeSessionKey*(): string = let random = makeSalt() return bcrypt.hash(random, genSalt(8)) -proc makePassword*(password, salt: string, comparingTo = ""): string = - ## Creates an MD5 hash by combining password and salt +# --==--==--==--==--==--==--==--==--==--==-- # +# PROC : make password +## Creates an MD5 hash by combining password and salt +# --==--==--==--==--==--==--==--==--==--==-- # +proc makePassword*(password, salt: string, comparingTo = ""): string = let bcryptSalt = if comparingTo != "": comparingTo else: genSalt(8) result = hash(getMD5(salt & getMD5(password)), bcryptSalt) \ No newline at end of file diff --git a/config/config.cfg b/config/config.cfg index ac0e537..ecee9e7 100644 --- a/config/config.cfg +++ b/config/config.cfg @@ -1,3 +1,6 @@ +# --==--==--==--==--==--==--==--==--==--==-- # +# Database : information to setup DB +# --==--==--==--==--==--==--==--==--==--==-- # [Database] folder = "data" host = "data/website.db" @@ -5,14 +8,24 @@ name = "website" user = "user" pass = "" + +# --==--==--==--==--==--==--==--==--==--==-- # +# Server : informations related to server side +# --==--==--==--==--==--==--==--==--==--==-- # [Server] website = "https://127.0.0.1:7000" title = "Joplin The New Web" url = "127.0.0.1" port = "7000" + +# --==--==--==--==--==--==--==--==--==--==-- # +# Joplin : Informations related to Joplin app +# --==--==--==--==--==--==--==--==--==--==-- # [Joplin] +#Joplin DESKTOP token #token = "e5f6644fbf6a97ddc55648dae72b11caecda6c6642d8ce0d3b20129b89b196385737eb908923542c3343649ebbf865b55bda031ab4c3a16edc7723ef2ad77d8f" +# Joplin CLI token token = "5b05f489016ce8a001ec83a7968419368eb9206340a18f73119c79e2154ab267ddec424658920bb6f88961c170a2680cd07fbd83f38e1c0c8df907a9aed22427" joplin_server = "https://joplinlab.bcmaison.cf" joplin_server_user = "joplinlab@zohomail.com"