Start implemeting owner model, update client app model, get first stage of Oauth2 working (issuing authorization code)
parent
5cd2a9ea16
commit
2fd65bd84c
@ -1 +1 @@
|
||||
Subproject commit 897cc85f52f00ce959a26d3a787aa887a37e0aad
|
||||
Subproject commit e45ab8bf31bf5cd69c8d1c05baa90b5672a666cd
|
@ -1,4 +1,5 @@
|
||||
{
|
||||
"secret": "%s{app->client.secret}",
|
||||
"name": "%s{app->client.name}"
|
||||
"name": "%s{app->client.name}",
|
||||
"auth_code": "%s{ app->auth_code ? app->auth_code : "" }"
|
||||
}
|
||||
|
@ -0,0 +1,80 @@
|
||||
#include "owner.h"
|
||||
#include "json/json.h"
|
||||
#include "sha256/sha256.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct owner* owner_new()
|
||||
{
|
||||
struct owner* o = malloc(sizeof(struct owner));
|
||||
|
||||
FILE* f = fopen("data/owner.json","r");
|
||||
if( f ) {
|
||||
struct json_pull_parser* jpp = json_pull_parser_new(f);
|
||||
if( !jpp ) {
|
||||
goto reinit;
|
||||
}
|
||||
|
||||
// Load file from
|
||||
int save;
|
||||
if( !json_pull_parser_begin_object( jpp, &save ) ) { goto reinit; }
|
||||
|
||||
char* key;
|
||||
while( key = json_pull_parser_read_object_key(jpp) ) {
|
||||
if( 0 == strcmp(key,"salt") ) {
|
||||
o->password_salt = json_pull_parser_read_string(jpp);
|
||||
} else if( 0 == strcmp(key,"hash") ) {
|
||||
o->password_hash = json_pull_parser_read_string(jpp);
|
||||
}
|
||||
}
|
||||
|
||||
if( !json_pull_parser_end_object( jpp, &save ) ) { goto reinit; }
|
||||
|
||||
o->initialized = true;
|
||||
return o;
|
||||
}
|
||||
|
||||
reinit:
|
||||
o->initialized = false;
|
||||
o->password_salt = strdup("");
|
||||
o->password_hash = strdup("");
|
||||
|
||||
return o;
|
||||
}
|
||||
void owner_free( struct owner* o )
|
||||
{
|
||||
free(o->password_salt);
|
||||
free(o->password_hash);
|
||||
free(o);
|
||||
}
|
||||
|
||||
bool owner_check_password( struct owner* o, const char* passwd )
|
||||
{
|
||||
char buffer[512];
|
||||
snprintf( buffer, 512, "%s:%s", o->password_salt, passwd );
|
||||
|
||||
char hash[65] = "";
|
||||
sha256_easy_hash_hex( buffer, strlen(buffer), hash );
|
||||
|
||||
return 0 == strcmp(hash,o->password_hash);
|
||||
}
|
||||
|
||||
void owner_set_password( struct owner* o, const char* passwd )
|
||||
{
|
||||
free(o->password_salt);
|
||||
free(o->password_hash);
|
||||
|
||||
char* new_salt = o->password_salt = malloc(65);
|
||||
for( int i = 0; i < 64; ++i ) {
|
||||
new_salt[i] = 'a' + (rand()%26);
|
||||
}
|
||||
new_salt[64] = '\0';
|
||||
|
||||
char buffer[512];
|
||||
snprintf( buffer, 512, "%s:%s", new_salt, passwd );
|
||||
|
||||
char* new_hash = o->password_hash = malloc(65);
|
||||
sha256_easy_hash_hex( buffer, strlen(buffer), new_hash );
|
||||
}
|
||||
|
@ -1,7 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct owner
|
||||
{
|
||||
bool initialized;
|
||||
char* password_salt;
|
||||
char* password_hash;
|
||||
};
|
||||
|
||||
struct owner* owner_new();
|
||||
void owner_save( struct owner* );
|
||||
void owner_free( struct owner* );
|
||||
|
||||
bool owner_check_password( struct owner* o, const char* passwd );
|
||||
void owner_set_password( struct owner* o, const char* passwd );
|
||||
|
||||
|
Loading…
Reference in new issue