You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.6 KiB
C

#include "client_apps.h"
#include "form.h"
#include "json/json.h"
#include "json/layout.h"
#include "http/server/request.h"
#include "model/client_app.h"
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
bool handle_mastodon_api_apps( struct http_request* req )
{
bool result = false;
FILE* post_data = http_request_get_request_data( req );
struct client_app_data
{
char* client_name;
char* redirect_uris;
} data;
memset(&data,0,sizeof(data));
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[] = {
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;
}
} else {
struct form_parser* fp = form_pull_parser_new( post_data );
if( !fp ) { return false; }
char* key;
while(( key = form_pull_parser_read_key( fp ) )) {
if( 0 == strcmp( "client_name", key ) ) {
data.client_name = strdup( form_pull_parser_read_value(fp) );
} else if( 0 == strcmp( "redirect_uris", key ) ) {
data.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);
}
if( !data.client_name ) { goto failed; }
if( !data.redirect_uris ) { goto failed; }
struct client_app* app = client_app_new( data.client_name );
app->redirect_uri = strdup(data.redirect_uris);
client_app_save(app);
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
#include "controller/mastodon_api/apps.json.inc"
client_app_free(app);
result = true;
cleanup:
free(data.client_name);
free(data.redirect_uris);
return result;
failed:
result = false;
goto cleanup;
}
bool check_bearer_token( const char* auth_token )
{
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 ) ) {
client_app_free(app);
return false;
}
client_app_free(app);
return true;
}
bool check_authentication_header( struct http_request* req )
{
// Check bearer token
const char* auth_token = http_request_get_header( req, "Authorization" );
if( !auth_token ) { return false; }
return check_bearer_token( auth_token );
}