Fix up JSON layout data, allow attributedTo instead of actor in post

master
teknomunk 1 year ago
parent 4344b9dac6
commit c90d6c71d8

@ -26,11 +26,13 @@ bool handle_mastodon_api_apps( struct http_request* req )
const char* content_type = http_request_get_header( req, "Content-Type" );
if( 0 == strcasecmp(content_type,"application/json") ) {
#define OBJ_TYPE struct client_app_data
static struct json_object_field layout[] = {
{ "redirect_uris", offsetof( struct client_app_data, redirect_uris ), true, &json_field_string },
{ "client_name", offsetof( struct client_app_data, client_name ), true, &json_field_string },
{ NULL },
JSON_FIELD_STRING( redirect_uris, true ),
JSON_FIELD_STRING( client_name, true ),
JSON_FIELD_END
};
#undef OBJ_TYPE
if( !json_read_object_layout_from_FILE( post_data, layout, &data ) ) {
goto failed;
}

@ -172,7 +172,7 @@ static bool handle_oauth_get_token( struct http_request* req )
struct client_app* app = NULL;
struct data_t
{
char* auth_code;
char* code;
char* redirect_uri;
char* client_secret;
char* client_id;
@ -185,16 +185,18 @@ static bool handle_oauth_get_token( struct http_request* req )
FILE* post_data = http_request_get_request_data(req);
const char* content_type = http_request_get_header( req, "Content-Type" );
if( 0 == strcasecmp(content_type,"application/json") ) {
#define OBJ_TYPE struct data_t
static struct json_object_field layout[] = {
{ "code", offsetof( struct data_t, auth_code ), false, &json_field_string },
{ "redirect_uri", offsetof( struct data_t, redirect_uri ), true, &json_field_string },
{ "grant_type", offsetof( struct data_t, grant_type ), false, &json_field_string },
{ "client_secret", offsetof( struct data_t, client_secret ), false, &json_field_string },
{ "client_id", offsetof( struct data_t, client_id ), false, &json_field_string },
{ "username", offsetof( struct data_t, username ), false, &json_field_string },
{ "password", offsetof( struct data_t, password ), false, &json_field_string },
{ NULL },
JSON_FIELD_STRING( code, false ),
JSON_FIELD_STRING( redirect_uri, false ),
JSON_FIELD_STRING( grant_type, false ),
JSON_FIELD_STRING( client_secret, false ),
JSON_FIELD_STRING( client_id, false ),
JSON_FIELD_STRING( username, false ),
JSON_FIELD_STRING( password, false ),
JSON_FIELD_END,
};
#undef OBJ_TYPE
if( !json_read_object_layout_from_FILE( post_data, layout, &data ) ) {
printf( "A" );
goto invalid_request;
@ -214,7 +216,7 @@ static bool handle_oauth_get_token( struct http_request* req )
data.grant_type = strdup(form_pull_parser_read_value(fp));
} else if( 0 == strcmp(key,"code") ) {
data.auth_code = strdup(form_pull_parser_read_value(fp));
data.code = strdup(form_pull_parser_read_value(fp));
} else if( 0 == strcmp(key,"client_id") ) {
data.client_id = strdup(form_pull_parser_read_value(fp));
@ -248,8 +250,8 @@ static bool handle_oauth_get_token( struct http_request* req )
printf( "6" );
if( 0 != strcmp( app->client.secret, data.client_secret ) ) { goto access_denied; }
if( 0 == strcmp("authorization_code",data.grant_type) ) {
if( !data.auth_code ) { goto invalid_request; }
if( 0 != strcmp( app->auth_code, data.auth_code ) ) { goto access_denied; }
if( !data.code ) { goto invalid_request; }
if( 0 != strcmp( app->auth_code, data.code ) ) { goto access_denied; }
} else if( 0 == strcmp("password", data.grant_type ) ) {
owner_account = account_from_id(0);
owner = owner_new();
@ -277,7 +279,7 @@ static bool handle_oauth_get_token( struct http_request* req )
cleanup:
client_app_free(app);
free(data.auth_code);
free(data.code);
free(data.redirect_uri);
free(data.client_secret);
free(data.client_id);

@ -1 +1 @@
Subproject commit 6f0f39a98f798d5f6c2299f633e86b939188e32b
Subproject commit 2e434a116e32def0a18bd4344d8a049943fcb0f9

@ -43,20 +43,41 @@ static struct json_enum account_types_enum[] = {
{ NULL },
};
#define OBJ_TYPE struct account
static struct json_object_field account_layout[] = {
{ "handle", offsetof( struct account, handle ), true, &json_field_string },
{ "server", offsetof( struct account, server ), true, &json_field_string },
{ "display_name", offsetof( struct account, display_name ), true, &json_field_string },
{ "avatar", offsetof( struct account, avatar.url ), false, &json_field_string },
{ "avatar_static", offsetof( struct account, avatar.static_url ), false, &json_field_string },
{ "account_type", offsetof( struct account, account_type ), true, &json_field_enum, account_types_enum },
{ "inbox", offsetof( struct account, inbox ), false, &json_field_string },
{ "note", offsetof( struct account, note ), false, &json_field_string },
{ "followers", offsetof( struct account, followers_count ), false, &json_field_integer },
{ "following", offsetof( struct account, following_count ), false, &json_field_integer },
{ "account_url", offsetof( struct account, account_url ), true, &json_field_string },
{ NULL },
JSON_FIELD_STRING( handle, true ),
JSON_FIELD_STRING( server, true ),
JSON_FIELD_STRING( display_name, true ),
JSON_FIELD_STRING( avatar, false ),
{
.key = "avatar",
.offset = offsetof( struct account, avatar.url ),
.required = false,
.type = &json_field_string
},
{
.key = "avatar_static",
.offset = offsetof( struct account, avatar.static_url ),
.required = false,
.type = &json_field_string
},
JSON_FIELD_ENUM( account_type, account_types_enum, true ),
JSON_FIELD_STRING( inbox, false ),
JSON_FIELD_STRING( note, false ),
{
.key = "followers",
.offset = offsetof( struct account, followers_count ),
.required = false,
.type = &json_field_integer
},
{
.key = "following",
.offset = offsetof( struct account, following_count ),
.required = false,
.type = &json_field_integer
},
JSON_FIELD_STRING( account_url, true ),
JSON_FIELD_END,
};
static struct account* new_system_account()

@ -12,41 +12,77 @@ static struct json_enum ap_account_type_enum[] = {
{ NULL, 0 },
};
#define OBJ_TYPE struct ap_crypto_key
static struct json_object_field public_key_layout[] = {
{ "id", offsetof(struct ap_crypto_key,id), true, &json_field_string },
{ "owner", offsetof(struct ap_crypto_key,owner), true, &json_field_string },
{ "publicKeyPem", offsetof(struct ap_crypto_key,pem), true, &json_field_string },
{ NULL, 0 },
JSON_FIELD_STRING( id, true ),
JSON_FIELD_STRING( owner, true ),
{
.key = "publicKeyPem",
.offset = offsetof(struct ap_crypto_key,pem),
.required = true,
.type = &json_field_string
},
JSON_FIELD_END,
};
#undef OBJ_TYPE
static struct json_object_field icon_layout[] = {
{ "url", offsetof(struct ap_account,avatar), true, &json_field_string },
{ NULL },
{
.key = "url",
.offset = offsetof(struct ap_account,avatar),
.required = true,
.type = &json_field_string
},
JSON_FIELD_END,
};
static struct json_object_field image_layout[] = {
{ "url", offsetof(struct ap_account,banner), true, &json_field_string },
{ NULL },
{
.key = "url",
.offset = offsetof(struct ap_account,banner),
.required = true,
.type = &json_field_string
},
JSON_FIELD_END,
};
#define OBJ_TYPE struct ap_account
static struct json_object_field ap_account_layout[] = {
{ "id", offsetof(struct ap_account,id), true, &json_field_string },
{ "type", offsetof(struct ap_account,type), true, &json_field_enum, ap_account_type_enum },
{ "url", offsetof(struct ap_account,url), true, &json_field_string },
{ "name", offsetof(struct ap_account,name), true, &json_field_string },
{ "preferredUsername", offsetof(struct ap_account,preferredUsername), true, &json_field_string },
{ "inbox", offsetof(struct ap_account,inbox), true, &json_field_string },
{ "summary", offsetof(struct ap_account,summary), true, &json_field_string },
{ "featured", offsetof(struct ap_account,featured), false, &json_field_string },
{ "followers", offsetof(struct ap_account,followers), false, &json_field_string },
{ "following", offsetof(struct ap_account,following), false, &json_field_string },
{ "publicKey", offsetof(struct ap_account,public_key), false, &json_field_object_composite, public_key_layout },
{ "discoverable", offsetof(struct ap_account,discoverable), false, &json_field_bool },
{ "icon", 0, false, &json_field_object_composite, icon_layout },
{ "image", 0, false, &json_field_object_composite, image_layout },
{ NULL },
JSON_FIELD_STRING( id, true ),
JSON_FIELD_ENUM( type, ap_account_type_enum, true ),
JSON_FIELD_STRING( url, true ),
JSON_FIELD_STRING( name, true ),
JSON_FIELD_STRING( preferredUsername, true ),
JSON_FIELD_STRING( inbox, true ),
JSON_FIELD_STRING( summary, true ),
JSON_FIELD_STRING( featured, false ),
JSON_FIELD_STRING( followers, false ),
JSON_FIELD_STRING( following, false ),
{
.key = "publicKey",
.offset = offsetof(struct ap_account,public_key),
.required = false,
.type = &json_field_object_composite,
.composite_layout = public_key_layout
},
JSON_FIELD_BOOL( discoverable, false ),
{
.key = "icon",
.offset = 0,
.required = false,
.type = &json_field_object_composite,
.composite_layout = icon_layout
},
{
.key = "image",
.offset = 0,
.required = false,
.type = &json_field_object_composite,
.composite_layout = image_layout
},
JSON_FIELD_END,
};
#undef OBJ_TYPE
struct ap_account* ap_account_from_file( const char* filename )
{

@ -11,6 +11,52 @@
#include <stdlib.h>
#include <string.h>
static bool context_eat( struct json_pull_parser* jpp, int* save )
{
json_pull_parser_read_value(jpp);
char* key;
while( key = json_pull_parser_read_object_key(jpp) ) {
free(key);
json_pull_parser_read_value(jpp);
}
if( !json_pull_parser_end_object(jpp,save) ) { return false; }
return true;
}
static bool context_read_language( struct json_pull_parser* jpp, struct ap_activity_context* ctx, int* save )
{
char* lang = json_pull_parser_read_string(jpp);
if( !lang ) {
return false;
}
static const char* langs[] = {
"und",
"en",
"de",
};
ctx->language = clang_unknown;
for( int i = 0; i < sizeof(langs)/sizeof(langs[0]); ++i ) {
//printf( "candidate: %s ==? %s\n", langs[i], lang );
if( 0 == strcmp(langs[i],lang) ) {
ctx->language = i+1;
}
}
if( ctx->language == clang_unknown ) {
printf( "unknown language: %s\n", lang );
return false;
}
free(lang);
if( !json_pull_parser_end_object(jpp,save) ) {
printf( "Object failed\n" );
return false;
}
return true;
}
static bool context_reader( struct json_pull_parser* jpp, void* field_data, struct json_object_field* layout_field_data )
{
//printf( "context_reader\n" );
@ -47,37 +93,12 @@ static bool context_reader( struct json_pull_parser* jpp, void* field_data, stru
return false;
}
char* key = json_pull_parser_read_object_key(jpp);
if( 0 != strcmp("@language",key) ) {
return false;
if( 0 == strcmp("@language",key) ) {
if( !context_read_language( jpp, ctx, &save2 ) ) { return false; }
} else {
if( !context_eat( jpp, &save2 ) ) { return false; }
}
free(key);
char* lang = json_pull_parser_read_string(jpp);
if( !lang ) {
return false;
}
static const char* langs[] = {
"und",
"en",
"de",
};
ctx->language = clang_unknown;
for( int i = 0; i < sizeof(langs)/sizeof(langs[0]); ++i ) {
//printf( "candidate: %s ==? %s\n", langs[i], lang );
if( 0 == strcmp(langs[i],lang) ) {
ctx->language = i+1;
}
}
if( ctx->language == clang_unknown ) {
printf( "unknown language: %s\n", lang );
return false;
}
free(lang);
if( !json_pull_parser_end_object(jpp,&save2) ) {
printf( "Object failed\n" );
return false;
}
}
}

@ -7,7 +7,7 @@
#include <string.h>
#include <stddef.h>
extern struct json_object_field ap_signature_layout[];
extern struct json_field_type ap_signature_composite_type;
extern struct json_field_type ap_activity_source_type;
// https://www.w3.org/TR/activitystreams-vocabulary/#h-activity-types
@ -44,14 +44,26 @@ struct json_enum ap_activity_type_enum[] = {
{ NULL, 0 },
};
struct json_object_field activity_ref_types[] = {
{ (char*)apaot_ref, offsetof( struct ap_activity, object.ref ), false, &json_field_string },
{ (char*)apaot_activity, offsetof( struct ap_activity, object.ptr ), false, &ap_activity_type },
JSON_FIELD_END,
struct json_tagged_union_typelist activity_ref_types[] = {
{
.tag_value = apaot_ref,
.offset = offsetof( struct ap_activity, object.ref ),
.type = &json_field_string
},
{
.tag_value = apaot_activity,
.offset = offsetof( struct ap_activity, object.ptr ),
.type = &ap_activity_type
},
JSON_TAGGED_UNION_TYPELIST_END,
};
struct json_object_field signature_types[] = {
{ (char*)1, offsetof( struct ap_activity, signature ), false, &json_field_object_composite, ap_signature_layout },
JSON_FIELD_END
struct json_tagged_union_typelist signature_types[] = {
{
.tag_value = 1,
.offset = offsetof( struct ap_activity, signature ),
.type = &ap_signature_composite_type,
},
JSON_TAGGED_UNION_TYPELIST_END,
};
#define OBJ_TYPE struct ap_activity
@ -64,7 +76,7 @@ struct json_object_field ap_activity_layout[] = {
},
JSON_FIELD_STRING(id,true),
JSON_FIELD_STRING(actor,true),
JSON_FIELD_STRING(actor,false),
JSON_FIELD_STRING(state,false),
{
@ -94,7 +106,7 @@ struct json_object_field ap_activity_layout[] = {
.offset = offsetof( struct ap_activity, tags ),
.required = false,
.type = &json_field_array_of,
.type_data = &ap_activity_tag_type
.array_item_type = &ap_activity_tag_type
},
JSON_FIELD_STRING(context,false),
@ -113,15 +125,33 @@ struct json_object_field ap_activity_layout[] = {
.type = &json_field_bool
},
{ "attachment", offsetof( struct ap_activity, attachments ), false, &json_field_array_of, &ap_attachement_type },
{
.key = "attachment",
.offset = offsetof( struct ap_activity, attachments ),
.required = false,
.type = &json_field_array_of,
.array_item_type = &ap_attachement_type
},
JSON_FIELD_ARRAY_OF_STRINGS(to,true),
JSON_FIELD_ARRAY_OF_STRINGS(cc,false),
JSON_FIELD_ARRAY_OF_STRINGS(bcc,false),
{ "object", offsetof( struct ap_activity, object.tag ), false, &json_field_tagged_union, &activity_ref_types },
{
.key = "object",
.offset = offsetof( struct ap_activity, object.tag ),
.required = false,
.type = &json_field_tagged_union,
.tagged_item_types = activity_ref_types
},
{ "signature", offsetof( struct ap_activity, has_signature ), false, &json_field_tagged_union, &signature_types },
{
.key = "signature",
.offset = offsetof( struct ap_activity, has_signature ),
.required = false,
.type = &json_field_tagged_union,
.tagged_item_types = signature_types
},
JSON_FIELD_ENUM( type, ap_activity_type_enum, true ),
@ -141,6 +171,14 @@ struct ap_activity* ap_activity_from_FILE( FILE* f )
return NULL;
}
if( !act->actor ) {
if( !act->attributed_to ) {
ap_activity_free(act);
return NULL;
}
act->actor = strdup(act->attributed_to);
}
return act;
}

@ -19,3 +19,6 @@ struct json_object_field ap_signature_layout[] = {
JSON_FIELD_END,
};
#undef OBJ_TYPE
JSON_FIELD_TYPE_COMPOSITE( ap_signature );

@ -11,12 +11,14 @@ static struct json_enum ap_activity_tag_enum[] = {
{ NULL, 0 },
};
#define OBJ_TYPE struct ap_activity_tag
static struct json_object_field ap_activity_tag_layout[] = {
{ "type", offsetof( struct ap_activity_tag, type ), true, &json_field_enum, ap_activity_tag_enum },
{ "href", offsetof( struct ap_activity_tag, href ), false, &json_field_string },
{ "name", offsetof( struct ap_activity_tag, name ), false, &json_field_string },
{ NULL },
JSON_FIELD_ENUM( type, ap_activity_tag_enum, true ),
JSON_FIELD_STRING( href, false ),
JSON_FIELD_STRING( name, false ),
JSON_FIELD_END,
};
#undef OBJ_TYPE
void ap_activity_tag_free( struct ap_activity_tag* tag )
{
@ -28,26 +30,5 @@ void ap_activity_tag_free( struct ap_activity_tag* tag )
free(tag);
}
static void* ap_activity_tag_alloc_shim()
{
struct ap_activity_tag* ptr = malloc(sizeof(struct ap_activity_tag));
memset(ptr,0,sizeof(*ptr));
return ptr;
}
static void ap_activity_tag_free_shim( void* ptr )
{
ap_activity_tag_free(ptr);
}
JSON_FIELD_TYPE_OBJECT_LAYOUT( ap_activity_tag );
/*
struct json_field_type ap_activity_tag_type = {
.reader = json_field_object_type_reader,
.writer = json_field_object_type_writer,
.size = sizeof( struct ap_activity_tag ),
.layout = ap_activity_tag_layout,
.alloc = alloc,
.free = free_shim,
};
*/
JSON_FIELD_TYPE_OBJECT_LAYOUT_WITH_DEFAULTS( ap_activity_tag );

@ -17,12 +17,16 @@
#include <stdint.h>
#include <time.h>
#define OBJ_TYPE struct http_header
static struct json_object_field http_header_layout[] = {
{ "key", offsetof(struct http_header, key), true, &json_field_string },
{ "value", offsetof(struct http_header, value), true, &json_field_string },
{ NULL },
JSON_FIELD_STRING( key, true ),
JSON_FIELD_STRING( value, true ),
JSON_FIELD_END,
};
#undef OBJ_TYPE
JSON_FIELD_TYPE_COMPOSITE( http_header )
/*
static struct json_field_type http_header_type = {
.reader = json_field_object_composite_reader,
.writer = json_field_object_composite_writer,
@ -30,15 +34,17 @@ static struct json_field_type http_header_type = {
.layout = http_header_layout,
.alloc = NULL,
.free = NULL,
};
};*/
#define OBJ_TYPE struct ap_envelope
static struct json_object_field envelope_layout[] = {
{ "when", offsetof(struct ap_envelope, when), true, &json_field_string },
{ "headers", offsetof(struct ap_envelope, headers), true, &json_field_array_of, &http_header_type },
{ "validated", offsetof(struct ap_envelope, validated), false, &json_field_bool },
{ "body", offsetof(struct ap_envelope, body), true, &json_field_string },
{ NULL },
JSON_FIELD_STRING( when, true ),
JSON_FIELD_ARRAY_OF_TYPE( headers, true, http_header_composite_type ),
JSON_FIELD_BOOL( validated, false ),
JSON_FIELD_STRING( body, true ),
JSON_FIELD_END,
};
#undef OBJ_TYPE
struct ap_envelope* ap_envelope_from_id( int id )
{

@ -8,11 +8,23 @@
#include <stddef.h>
#include <string.h>
#define OBJ_TYPE struct outbox_envelope
static struct json_object_field layout[] = {
{ "account", offsetof( struct outbox_envelope, account_id ), true, &json_field_integer },
{ "activity", offsetof( struct outbox_envelope, activity_id ), true, &json_field_integer },
{ NULL },
{
.key = "account",
.offset = offsetof( struct outbox_envelope, account_id ),
.required = true,
.type = &json_field_integer
},
{
.key = "activity",
.offset = offsetof( struct outbox_envelope, activity_id ),
.required = true,
.type = &json_field_integer
},
JSON_FIELD_END
};
#undef OBJ_TYPE
struct outbox_envelope* outbox_envelope_new()
{

@ -22,14 +22,25 @@ static char* safe( char* str )
return str;
}
#define OBJ_TYPE struct client_app
static struct json_object_field client_app_layout[] = {
{ "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 },
{ "secret", offsetof( struct client_app, client.secret ), true, &json_field_string },
{ NULL },
{
.key = "name",
.offset = offsetof( struct client_app, client.name ),
.required = false,
.type = &json_field_string
},
JSON_FIELD_STRING( auth_code, false ),
JSON_FIELD_STRING( access_token, false ),
JSON_FIELD_STRING( redirect_uri, false ),
{
.key = "secret",
.offset = offsetof( struct client_app, client.secret ),
.required = true,
.type = &json_field_string },
JSON_FIELD_END,
};
#undef OBJ_TYPE
struct client_app* client_app_from_id( const char* client_id )
{

@ -24,14 +24,16 @@ static struct json_enum notification_type_enum[] = {
{ NULL, -1 },
};
#define OBJ_TYPE struct notification
static struct json_object_field notification_layout[] = {
{ "account_id", offsetof( struct notification, account_id ), true, &json_field_integer },
{ "status_id", offsetof( struct notification, status_id ), false, &json_field_integer },
{ "ref_account_id", offsetof( struct notification, ref_account_id ), false, &json_field_integer },
{ "react", offsetof( struct notification, react ), false, &json_field_string },
{ "type", offsetof( struct notification, type ), true, &json_field_enum, notification_type_enum },
{ NULL }
JSON_FIELD_INTEGER( account_id, true ),
JSON_FIELD_INTEGER( status_id, false ),
JSON_FIELD_INTEGER( ref_account_id, false ),
JSON_FIELD_STRING( react, false ),
JSON_FIELD_ENUM( type, notification_type_enum, true ),
JSON_FIELD_END,
};
#undef OBJ_TYPE
struct notification* notification_from_id( int id )
{

@ -8,11 +8,23 @@
#include <stdio.h>
#include <stddef.h>
#define OBJ_TYPE struct owner
static struct json_object_field owner_layout[] = {
{ "salt", offsetof( struct owner, password_salt ), true, &json_field_string },
{ "hash", offsetof( struct owner, password_hash ), true, &json_field_string },
{ NULL },
{
.key = "salt",
.offset = offsetof( struct owner, password_salt ),
.required = true,
.type = &json_field_string
},
{
.key = "hash",
.offset = offsetof( struct owner, password_hash ),
.required = true,
.type = &json_field_string
},
JSON_FIELD_END,
};
#undef OBJ_TYPE
void* allocate( size_t s );

@ -13,9 +13,10 @@ enum {
STATUSES_PER_BLOCK = 256,
};
#define OBJ_TYPE struct timeline
static struct json_object_field timeline_block_layout[] = {
{ "status_ids", offsetof( struct timeline, status_ids ), true, &json_field_array_of, &json_field_integer },
{ NULL },
JSON_FIELD_ARRAY_OF_INTS( status_ids, true ),
JSON_FIELD_END,
};
struct timeline* timeline_block_load( int id, unsigned int head, unsigned int block )

Loading…
Cancel
Save