#include "mastodon_api.h" #include "http_server/http_request.h" #include #include #include #include "form.h" #include "model/client_app.h" #include "model/status.h" bool handle_mastodon_api_apps( struct http_request* req ) { FILE* data = http_request_get_request_data( req ); struct form_parser* fp = form_pull_parser_new( data ); if( !fp ) { return false; } char* client_name = NULL; char* redirect_uris = NULL; char* key; while( key = form_pull_parser_read_key( fp ) ) { if( 0 == strcmp( "client_name", key ) ) { client_name = strdup( form_pull_parser_read_value(fp) ); } else if( 0 == strcmp( "redirect_uris", key ) ) { redirect_uris = strdup( form_pull_parser_read_value(fp) ); } else { printf( "key: %s\n", key ); printf( "value: %s\n", form_pull_parser_read_value(fp) ); } } form_pull_parser_release(fp); struct client_app* app = client_app_new( client_name ); http_request_send_headers( req, 200, "application/json", true ); FILE* f = http_request_get_response_body( req ); #define RENDER #include "controller/mastodon_api/apps.json.inc" #undef RENDER free(client_name); free(redirect_uris); return true; } bool check_bearer_token( struct http_request* req ) { // Check bearer token const char* auth_token = http_request_get_header( req, "Authorization" ); if( !auth_token ) { return false; } if( 0 != strncmp( auth_token, "Bearer ", 7 ) ) { return false; } char* client_id = strndup( &auth_token[7], 32 ); struct client_app* app = client_app_from_id( client_id ); free(client_id); if( !app ) { return false; } if( 0 != strcmp( &auth_token[7], app->access_token ) ) { return false; } client_app_free(app); return true; } bool handle_timeline( struct http_request* req, const char* which ) { // "GET /api/v1/timelines/home?with_muted=true&limit=31" bool with_muted = false; unsigned int limit = 100; http_request_send_headers( req, 200, "application/json", true ); FILE* f = http_request_get_response_body( req ); struct status s = { .id = 1, }; fprintf( f, "[" ); status_write_as_json(&s,f); fprintf( f, "]" ); return true; } bool route_mastodon_api( struct http_request* req ) { if( http_request_route( req, "apps" ) ) { if( http_request_route_method( req, "POST" ) ) { return handle_mastodon_api_apps(req); } } if( !check_bearer_token(req) ) { return false; } printf( "authorization still valid\n" ); if( http_request_route( req, "timelines/home" ) ) { return handle_timeline( req, "home" ); } else if( http_request_route( req, "accounts/verify_credentials" ) ) { return true; } return false; }