|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
#include "client_app.h"
|
|
|
|
|
#include "json/json.h"
|
|
|
|
|
#include "json/layout.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
|
|
static char* nullify_empty( char* str )
|
|
|
|
|
{
|
|
|
|
@ -20,16 +22,27 @@ static char* safe( char* str )
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct json_object_field client_app_layout[] = {
|
|
|
|
|
{ "secret", offsetof( struct client_app, client.secret ), true, &json_field_string },
|
|
|
|
|
{ "name", offsetof( struct client_app, client.name ), false, &json_field_string },
|
|
|
|
|
{ "auth_code", offsetof( struct client_app, auth_code ), false, &json_field_string },
|
|
|
|
|
{ "access_token", offsetof( struct client_app, access_token ), false, &json_field_string },
|
|
|
|
|
{ "redirect_uri", offsetof( struct client_app, redirect_uri ), false, &json_field_string },
|
|
|
|
|
{ NULL },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct client_app* client_app_from_id( const char* client_id )
|
|
|
|
|
{
|
|
|
|
|
FILE* f = NULL;
|
|
|
|
|
struct client_app* app = NULL;
|
|
|
|
|
struct json_pull_parser* jpp = NULL;
|
|
|
|
|
|
|
|
|
|
char filename[512];
|
|
|
|
|
snprintf( filename, 512, "data/client_apps/%s.json", client_id );
|
|
|
|
|
FILE* f = fopen(filename,"r");
|
|
|
|
|
if( !f ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
f = fopen(filename,"r");
|
|
|
|
|
if( !f ) { goto failed; }
|
|
|
|
|
|
|
|
|
|
struct client_app* app = malloc(sizeof(struct client_app));
|
|
|
|
|
app = malloc(sizeof(struct client_app));
|
|
|
|
|
app->client.name = NULL;
|
|
|
|
|
app->client.id = strdup(client_id);
|
|
|
|
|
app->client.secret = NULL;
|
|
|
|
@ -37,41 +50,19 @@ struct client_app* client_app_from_id( const char* client_id )
|
|
|
|
|
app->redirect_uri = NULL;
|
|
|
|
|
app->access_token = NULL;
|
|
|
|
|
|
|
|
|
|
struct json_pull_parser* jpp = json_pull_parser_new( f );
|
|
|
|
|
if( !jpp ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
int s[5];
|
|
|
|
|
if( !json_pull_parser_begin_object( jpp, &s[0] ) ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
jpp = json_pull_parser_new( f );
|
|
|
|
|
if( !jpp ) { goto failed; }
|
|
|
|
|
|
|
|
|
|
char* key;
|
|
|
|
|
while( key = json_pull_parser_read_object_key(jpp) ) {
|
|
|
|
|
if( 0 == strcmp(key,"secret") ) {
|
|
|
|
|
app->client.secret = json_pull_parser_read_string(jpp);
|
|
|
|
|
|
|
|
|
|
} else if( 0 == strcmp(key,"name") ) {
|
|
|
|
|
app->client.name = json_pull_parser_read_string(jpp);
|
|
|
|
|
|
|
|
|
|
} else if( 0 == strcmp(key,"auth_code") ) {
|
|
|
|
|
app->auth_code = nullify_empty( json_pull_parser_read_string(jpp) );
|
|
|
|
|
|
|
|
|
|
} else if( 0 == strcmp(key,"access_token") ) {
|
|
|
|
|
app->access_token = nullify_empty( json_pull_parser_read_string(jpp) );
|
|
|
|
|
|
|
|
|
|
} else if( 0 == strcmp(key,"redirect_uri") ) {
|
|
|
|
|
app->redirect_uri = nullify_empty( json_pull_parser_read_string(jpp) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(key);
|
|
|
|
|
|
|
|
|
|
if( !json_pull_parser_end_object(jpp, &s[0] ) ) {
|
|
|
|
|
client_app_free(app);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if( !json_read_object_layout( jpp, client_app_layout, app ) ) { goto failed; }
|
|
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
json_pull_parser_release(jpp);
|
|
|
|
|
if( f ) { fclose(f); }
|
|
|
|
|
return app;
|
|
|
|
|
failed:
|
|
|
|
|
client_app_free(app);
|
|
|
|
|
app = NULL;
|
|
|
|
|
goto cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct client_app* client_app_new( const char* client_name )
|
|
|
|
@ -119,6 +110,8 @@ void client_app_save( struct client_app* app )
|
|
|
|
|
|
|
|
|
|
void client_app_free( struct client_app* app )
|
|
|
|
|
{
|
|
|
|
|
if( !app ) { return; }
|
|
|
|
|
|
|
|
|
|
free(app->client.name);
|
|
|
|
|
free(app->client.secret);
|
|
|
|
|
free(app->client.id);
|
|
|
|
|