diff --git a/assets/private.html b/assets/private.html index d4bb736..2f7a0fc 100644 --- a/assets/private.html +++ b/assets/private.html @@ -27,7 +27,7 @@
  • Mes services
  • Documentation
  • Changer mon mot de passe
  • -
  • Se déconnecter
  • +
  • Se déconnecter
  • @@ -40,6 +40,7 @@

    Bienvenue sur kitoy.me

    Gérez votre compte sur kitoy.me

    +

    Connecté en tant que : $login$

    diff --git a/assets/theme-switcher.js~ b/assets/theme-switcher.js~ deleted file mode 100644 index d8495a0..0000000 --- a/assets/theme-switcher.js~ +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Theme switcher - * - * Pico.css - https://picocss.com - * Copyright 2019-2021 - Licensed under MIT - */ - -export const themeSwitcher = { - // Config - _scheme: 'auto', - change: { - light: 'Turn on dark mode', - dark: 'Turn off dark mode', - }, - buttonsTarget: '.theme-switcher', - - // Init - init() { - this.scheme = this._scheme; - this.initSwitchers(); - }, - - // Prefered color scheme - get preferedColorScheme() { - if (window.matchMedia('(prefers-color-scheme: dark)').matches) { - return 'dark'; - } else { - return 'light'; - } - }, - - // Init switchers - initSwitchers() { - const buttons = document.querySelectorAll(this.buttonsTarget); - buttons.forEach( - function (button) { - button.addEventListener( - 'click', - function (event) { - if (this.scheme == 'dark') { - this.scheme = 'light'; - } else { - this.scheme = 'dark'; - } - }.bind(this), - false - ); - }.bind(this) - ); - }, - - // Add new button - addButton(config) { - // Insert Switcher - let button = document.createElement(config.tag); - button.className = config.class; - document.querySelector(config.target).appendChild(button); - }, - - // Set scheme - set scheme(scheme) { - if (scheme == 'auto') { - if (this.preferedColorScheme == 'dark') { - this._scheme = 'dark'; - } else { - this._scheme = 'light'; - } - } - - // Set to Dark - else if (scheme == 'dark' || scheme == 'light') { - this._scheme = scheme; - } - - // Set to Apply theme - this.applyScheme(); - }, - - // Get scheme - get scheme() { - return this._scheme; - }, - - // Apply scheme - applyScheme() { - // Root attribute - document.querySelector('html').setAttribute('data-theme', this.scheme); - - // Buttons text - const buttons = document.querySelectorAll(this.buttonsTarget); - let text; - buttons.forEach( - function (button) { - if (this.scheme == 'dark') { - text = this.change.dark; - } else { - text = this.change.light; - } - button.innerHTML = text; - button.setAttribute('aria-label', text.replace(/<[^>]*>?/gm, '')); - }.bind(this) - ); - }, -}; - -export default themeSwitcher; diff --git a/conf/portal_user.conf b/conf/portal_user.conf index 410bf01..dd4991c 100644 --- a/conf/portal_user.conf +++ b/conf/portal_user.conf @@ -52,7 +52,7 @@ domain * { route /signup { handler create_user - methods post + methods get post validate post login v_login validate post password v_password } @@ -64,7 +64,9 @@ domain * { route /logout { handler v_session_remove + authenticate auth_example } } + diff --git a/src/portal_user.c b/src/portal_user.c index 77447b6..1fa3337 100644 --- a/src/portal_user.c +++ b/src/portal_user.c @@ -33,12 +33,14 @@ int portal_user_load(struct http_request *); int v_password_func(struct http_request *, char *); int create_user(struct http_request *); int v_session_validate(struct http_request *, char *); -int v_session_remove(struct http_request *, char *); +int v_session_remove(struct http_request *); int private_portal(struct http_request *); hashtable_t *hashtable = NULL; -int init(int state){ +int +init(int state) +{ hashtable = ht_create( 65536 ); int err=0; if( hashtable == NULL ) @@ -61,7 +63,8 @@ int init(int state){ return (KORE_RESULT_OK); } -int create_user(struct http_request *req) +int +create_user(struct http_request *req) { struct kore_buf *b = NULL; u_int8_t *d = NULL; @@ -103,9 +106,11 @@ int create_user(struct http_request *req) return (KORE_RESULT_ERROR); } - //salt = crypt_gensalt("$2b$", 15, NULL, 0); //-> linux - salt = bcrypt_gensalt(15); //-> openbsd - + #if defined(__linux__) + salt = crypt_gensalt("$2b$", 15, NULL, 0); //-> linux + #elif defined(__OpenBSD__) + salt = bcrypt_gensalt(15); + #endif if (salt == NULL) { kore_log(LOG_ERR, "crypt_gensalt"); exit(EXIT_FAILURE); @@ -156,7 +161,8 @@ int create_user(struct http_request *req) } -int portal_user_load(struct http_request *req) +int +portal_user_load(struct http_request *req) { struct kore_buf *b = NULL; u_int8_t *d = NULL; @@ -243,7 +249,6 @@ int portal_user_load(struct http_request *req) if ( strcmp( (const char *)sqlite3_column_text(res, 1), cryptpwd) ) { - printf("mauvais mot de passe\n"); kore_buf_replace_string(b, "$msg$", "Erreur de login ou mot de passe ", 17); } else @@ -279,7 +284,8 @@ int portal_user_load(struct http_request *req) } -int v_password_func(struct http_request *req, char *data) +int +v_password_func(struct http_request *req, char *data) { kore_log(LOG_NOTICE, "v_password_func called %s", data); @@ -290,12 +296,15 @@ int v_password_func(struct http_request *req, char *data) } -int v_session_remove (struct http_request *req, char *data) +int +v_session_remove (struct http_request *req) { - char buffer[SESSION_LEN]; - kore_log(LOG_NOTICE, "v_session_remove: %s", data); - (void)snprintf(buffer, SESSION_LEN, "%s", data); + char *buffer; + http_populate_cookies(req); + if (http_request_cookie(req, "session_id", &buffer)) + kore_log(LOG_DEBUG, "Got session_id: %s", buffer); + if (ht_get(hashtable, buffer) != NULL) ht_delete(hashtable, buffer); http_response_header(req, "location", "/"); @@ -306,36 +315,58 @@ int v_session_remove (struct http_request *req, char *data) return (KORE_RESULT_OK); } -int v_session_validate(struct http_request *req, char *data) +int +v_session_validate(struct http_request *req, char *data) { + + kore_log(LOG_NOTICE, "v_session_validate: %s", data); - + if ( ht_get(hashtable, data) != NULL ) return (KORE_RESULT_OK); - - return (KORE_RESULT_ERROR); + else + { + kore_log(LOG_NOTICE, "Session Inexistante"); + } + return (KORE_RESULT_ERROR); } -int private_portal(struct http_request *req) +int +private_portal(struct http_request *req) { struct kore_buf *b = NULL; u_int8_t *d = NULL; size_t len = 0; + char *buffer =NULL ; + session_t *account = NULL; if (req->method == HTTP_METHOD_GET) http_populate_get(req); + + http_populate_cookies(req); + + if (http_request_cookie(req, "session_id", &buffer)) + kore_log(LOG_DEBUG, "Got session_id: %s", buffer); + + b = kore_buf_alloc(asset_len_signup_html); kore_buf_append(b, asset_private_html, asset_len_private_html); - - + + account = ht_get(hashtable, buffer); if (req->method == HTTP_METHOD_GET) { - kore_buf_replace_string(b, "$msg$", "GO", 4); + + kore_buf_replace_string(b, "$msg$", "GO", 4); - http_response_header(req, "content-type", "text/html"); - d = kore_buf_release(b, &len); - http_response(req, 200, d, len); - kore_free(d); - return (KORE_RESULT_OK); + if (account != NULL) + { + kore_log(LOG_NOTICE, "account trouvé: %s", account->user); + kore_buf_replace_string(b, "$login$", account->user, sizeof(account->user)+1); + } + http_response_header(req, "content-type", "text/html"); + d = kore_buf_release(b, &len); + http_response(req, 200, d, len); + kore_free(d); } + return (KORE_RESULT_OK); } diff --git a/src/sessions.h b/src/sessions.h index 77d9d96..e81cda4 100644 --- a/src/sessions.h +++ b/src/sessions.h @@ -22,7 +22,7 @@ typedef struct hashtable_s hashtable_t; static hashtable_t *ht_create( int size ); static int ht_hash( hashtable_t *hashtable, char *session_id ); static session_t *ht_newpair( char *session_id, char *user ); -static char *ht_get( hashtable_t *hashtable, char *session_id ); +static session_t *ht_get( hashtable_t *hashtable, char *session_id ); static void ht_set( hashtable_t *hashtable, char *session_id, char *user ); static void ht_delete (hashtable_t *hashtable, char *key); static char *gen_session_id(int len); @@ -40,6 +40,7 @@ hashtable_t *ht_create( int size ) { /* Allocate the table itself. */ if( ( hashtable = malloc( sizeof( hashtable_t ) ) ) == NULL ) { return NULL; + } /* Allocate pointers to the head nodes. */ @@ -136,7 +137,7 @@ static void ht_set( hashtable_t *hashtable, char *session_id, char *user ) { } /* Retrieve a key-value pair from a hash table. */ -static char *ht_get( hashtable_t *hashtable, char *session_id ) { +static session_t *ht_get( hashtable_t *hashtable, char *session_id ) { int bin = 0; session_t *pair; @@ -149,12 +150,14 @@ static char *ht_get( hashtable_t *hashtable, char *session_id ) { } /* Did we actually find anything? */ - if( pair == NULL || pair->session_id == NULL || strcmp( session_id, pair->session_id ) != 0 ) { + if( pair == NULL || pair->session_id == NULL || strcmp( session_id, pair->session_id ) != 0 ) + { return NULL; - - } else { - return pair->user; - } + } + else + { + return pair; + } } @@ -184,7 +187,7 @@ char *gen_session_id(int len){ int index = 0; char session_id[len]; - char char1[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/,.-+=~`<>:"; + char char1[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/,.-+~`<>:"; int c_tmp = 0; char *res; srand((unsigned int)(time(NULL))); @@ -209,14 +212,15 @@ char *set_cookie_header( const char *champ, const char separator, lse = strlen(session_id); if (separator) ls = 1; - if ( (result = calloc(lch + ls + lse + 1, sizeof *result)) == NULL ) - return NULL; - memcpy (result, champ, lch); - if (separator) + result = calloc(lch + ls + lse + 1, sizeof *result); + + if (result != NULL) { + memcpy (result, champ, lch); + if (separator) result[lch] = separator; - memcpy (result + lch + ls, session_id, lse + 1 ); - + memcpy (result + lch + ls, session_id, lse + 1 ); + } return result; } diff --git a/src/sqlite_utils.h b/src/sqlite_utils.h index 30b0eec..06f5800 100644 --- a/src/sqlite_utils.h +++ b/src/sqlite_utils.h @@ -75,7 +75,7 @@ int check_db(const char *name){ } else { - kore_log(LOG_NOTICE, "Check database is 0K!"); + kore_log(LOG_NOTICE, "Check database 0K!"); } sqlite3_close(db);