Récupération du nom du compte après le login

This commit is contained in:
kitoy 2023-04-11 21:17:47 +02:00
parent b722d7668d
commit 811ebb8ca0
6 changed files with 81 additions and 149 deletions

View File

@ -27,7 +27,7 @@
<li><a href="#"> Mes services </a> </li>
<li><a href="#"> Documentation </a></li>
<li><a href="#"> Changer mon mot de passe </a></li>
<li><a href="#"> Se déconnecter </a></li>
<li><a href="/logout"> Se déconnecter </a></li>
</ul>
</details>
</li>
@ -40,6 +40,7 @@
<hgroup>
<h1> Bienvenue sur kitoy.me </h1>
<h2> Gérez votre compte sur kitoy.me </h2>
<h3> Connecté en tant que : $login$ </h3>
</hgroup>
<!-- Nav -->

View File

@ -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: '<i>Turn on dark mode</i>',
dark: '<i>Turn off dark mode</i>',
},
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;

View File

@ -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
}
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);