Re-enable authentication, switch client app to json/layout

master
teknomunk 1 year ago
parent bc36480af3
commit 99cf9a4abb

@ -63,7 +63,7 @@ bool route_mastodon_api( struct http_request* req )
return true;
}
/*
//*
if( !check_bearer_token(req) ) { return false; }
printf( "authorization still valid\n" );
//*/

@ -1 +1 @@
Subproject commit 962ab0069d67747d29b1a2bbfd3efb07d2a9b890
Subproject commit ec58393b08f85ae9b73f76e8d0aa73d309e2a629

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

@ -84,9 +84,14 @@ void status_save( struct status* s )
snprintf( filename, 512, "data/statuses/%d.json", s->id );
snprintf( tmp_filename, 512+32, "%s.tmp", filename );
FILE* f = fopen( tmp_filename, "w" );
#include "src/model/status.json.inc"
fclose(f);
struct json_writer jw = {
.f = fopen( tmp_filename, "w" ),
.indent = 0,
.indentation = "\t",
};
printf( "content:\n%s\n", s->content );
json_write_pretty_object_layout( &jw, status_layout, s );
fclose(jw.f);
rename( tmp_filename, filename );
}

@ -1,5 +0,0 @@
{
"account_id": %d{s->account_id},
"content": "%( write_json_escaped(f,s->content); )",
"sensitive": %s{ b(s->sensitive) }
}
Loading…
Cancel
Save