Add authenticated methods section to api, start implementing statuses and timelines

master
teknomunk 1 year ago
parent 4a1edda659
commit d01e24d650

@ -7,6 +7,7 @@
#include "form.h"
#include "model/client_app.h"
#include "model/status.h"
bool handle_mastodon_api_apps( struct http_request* req )
{
@ -47,6 +48,43 @@ bool handle_mastodon_api_apps( struct http_request* req )
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" ) ) {
@ -55,7 +93,14 @@ bool route_mastodon_api( struct http_request* req )
}
}
// "GET /api/v1/timelines/home?with_muted=true&limit=31"
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;
}

@ -1 +1 @@
Subproject commit e45ab8bf31bf5cd69c8d1c05baa90b5672a666cd
Subproject commit 69c7584f8ea7861c726c2123145df4a03b11cd3d

@ -143,12 +143,15 @@ void client_app_gen_auth_code( struct client_app* app )
}
void client_app_generate_access_token( struct client_app* app )
{
char* access_token = app->access_token = malloc(65);
char code[33];
char* access_token = app->access_token = malloc(32+1+32+1);
for( int i = 0; i < 64; ++i ) {
access_token[i] = 'a'+(rand()%26);
for( int i = 0; i < 32; ++i ) {
code[i] = 'a'+(rand()%26);
}
access_token[64] = '\0';
code[32] = '\0';
snprintf( access_token, 32+1+32+1, "%s-%s", app->client.id, code );
client_app_save(app);
}

@ -0,0 +1,13 @@
#include "status.h"
static const char* host()
{
return "apogee.polaris-1.work";
}
void status_write_as_json( struct status* s, FILE* f )
{
#define RENDER
#include "src/model/status.json.inc"
#undef RENDER
}

@ -0,0 +1,11 @@
#pragma once
#include <stdio.h>
struct status
{
unsigned int id;
};
void status_write_as_json( struct status* s, FILE* f );

@ -0,0 +1,111 @@
{
"account": {
"acct": "PinochetsCommieCopter@poa.st",
"avatar": "https://i.poastcdn.org/8eed7003b59feae1b66cab96577cd18fedf5c13a55193bc78dadba50cae5c9aa.jpg",
"avatar_static": "https://i.poastcdn.org/8eed7003b59feae1b66cab96577cd18fedf5c13a55193bc78dadba50cae5c9aa.jpg",
"bot": false,
"created_at": "2022-03-18T19:33:06.000Z",
"display_name": "PinochetsCommieCopter",
"emojis": [],
"fields": [],
"followers_count": 0,
"following_count": 0,
"fqn": "PinochetsCommieCopter@poa.st",
"header": "https://pl.polaris-1.work/images/banner.png",
"header_static": "https://pl.polaris-1.work/images/banner.png",
"id": "AHXybo2KIxbkUzvewC",
"locked": false,
"note": "Humble twitter aggregator for all those banned from it, feel free so steal my stuff and post it anywhere almost all of my stuff is stolen from twitter anyways, also im not Kinochet<br/>",
"pleroma": {
"accepts_chat_messages": true,
"also_known_as": [],
"ap_id": "https://poa.st/users/PinochetsCommieCopter",
"background_image": null,
"favicon": null,
"hide_favorites": true,
"hide_followers": false,
"hide_followers_count": false,
"hide_follows": false,
"hide_follows_count": false,
"is_admin": false,
"is_confirmed": true,
"is_moderator": false,
"relationship": {},
"skip_thread_containment": false,
"tags": []
},
"source": {
"fields": [],
"note": "",
"pleroma": {
"actor_type": "Person",
"discoverable": true
},
"sensitive": false
},
"statuses_count": 5133,
"url": "https://poa.st/users/PinochetsCommieCopter",
"username": "PinochetsCommieCopter"
},
"application": null,
"bookmarked": false,
"card": null,
"content": "",
"created_at": "2022-12-12T02:52:24.000Z",
"emojis": [],
"favourited": false,
"favourites_count": 0,
"id": "AQW7nAyGMWDVvDO47E",
"in_reply_to_account_id": null,
"in_reply_to_id": null,
"language": null,
"media_attachments": [ {
"blurhash": "eRH.A}xs0Kxv00xYR,R+t5R+9Gt6xaNG%%2-;xaM{NGRjD%%Rjs:xaxu",
"description": null,
"id": "1248813041",
"pleroma": {
"mime_type": "image/png"
},
"preview_url": "https://i.poastcdn.org/0ce6abff5d4838b9af4033a2567d94033b9b62a15aae394e7e48b5f597db76de.png",
"remote_url": "https://i.poastcdn.org/0ce6abff5d4838b9af4033a2567d94033b9b62a15aae394e7e48b5f597db76de.png",
"text_url": "https://i.poastcdn.org/0ce6abff5d4838b9af4033a2567d94033b9b62a15aae394e7e48b5f597db76de.png",
"type": "image",
"url": "https://i.poastcdn.org/0ce6abff5d4838b9af4033a2567d94033b9b62a15aae394e7e48b5f597db76de.png"
} ],
"mentions": [],
"muted": false,
"pinned": false,
"pleroma": {
"content": {
"text/plain": ""
},
"conversation_id": 2935699,
"direct_conversation_id": null,
"emoji_reactions": [ {
"count": 1,
"me": false,
"name": "😆"
} ],
"expires_at": null,
"in_reply_to_account_acct": null,
"local": false,
"parent_visible": false,
"pinned_at": null,
"spoiler_text": {
"text/plain": ""
},
"thread_muted": false
},
"poll": null,
"reblog": null,
"reblogged": false,
"reblogs_count": 1,
"replies_count": 0,
"sensitive": false,
"spoiler_text": "",
"tags": [],
"text": null,
"uri": "https://poa.st/objects/0db3a913-5007-464d-9829-995d5090c976",
"url": "https://poa.st/objects/0db3a913-5007-464d-9829-995d5090c976",
"visibility": "public"
}
Loading…
Cancel
Save