Start implementing OAUTH2 application infrastructure so that Husky will connect

master
teknomunk 2 years ago
parent 4e351686f4
commit 75843983d6

1
.gitignore vendored

@ -5,3 +5,4 @@ src.a
src.bin
debug
release
data/

@ -1,5 +1,7 @@
#include "http_server/http_request.h"
#include "controller/mastodon_api.h"
#include "controller/oauth.h"
bool route_asset( struct http_request* req )
{
@ -29,6 +31,9 @@ bool route_request( struct http_request* req )
if( http_request_route( req, "/api/v1/" ) ) {
return route_mastodon_api( req );
}
if( http_request_route( req, "/oauth" ) ) {
return route_oauth( req );
}
if( http_request_route( req, "/.well-known" ) ) {
return false;
}

@ -3,8 +3,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "form.h"
#include "model/client_app.h"
bool route_mastodon_api( struct http_request* req )
{
@ -16,13 +18,36 @@ bool route_mastodon_api( struct http_request* 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 ) ) {
printf( "key: %s\n", key );
printf( "vakue: %s\n", form_pull_parser_read_value(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;
}
}
return false;

@ -0,0 +1,8 @@
{
"id": 1,
"name": "%s{app->client.name}",
"website": null,
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"client_id": "%s{app->client.id}",
"client_secret": "%s{app->client.secret}"
}

@ -0,0 +1,21 @@
#include "oauth.h"
#include "http_server/http_request.h"
bool route_oauth_authorize( struct http_request* req )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
#define RENDER
#include "view/login.html.inc"
#undef RENDER
return true;
}
bool route_oauth( struct http_request* req )
{
if( http_request_route( req, "/authorize" ) ) {
return route_oauth_authorize(req);
}
return false;
}

@ -0,0 +1,7 @@
#pragma once
#include <stdbool.h>
struct http_request;
bool route_oauth( struct http_request* );

@ -0,0 +1,45 @@
#include "client_app.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct client_app* client_app_new( const char* client_name )
{
char id[33];
for( int i = 0; i < 32; ++i ) {
id[i] = 'a' + (rand() % 26);
}
id[32] = '\0';
char secret[65];
for( int i = 0; i < 64; ++i ) {
secret[i] = 'a' + (rand() % 26);
}
secret[64] = '\0';
struct client_app* app = malloc(sizeof(struct client_app));
app->client.id = strdup(id);
app->client.name = strdup(client_name);
app->client.secret = strdup(secret);
client_app_save(app);
return app;
}
void client_app_save( struct client_app* app )
{
char filename[512];
snprintf( filename, 512, "data/client_apps/%s.json", app->client.id );
char tmp_filename[512];
snprintf( tmp_filename, 512, "%s.tmp-%d", filename, rand() );
FILE* f = fopen(tmp_filename, "w" );
#define RENDER
#include "model/client_app.json.inc"
#undef RENDER
fclose(f);
rename( tmp_filename, filename );
}

@ -0,0 +1,15 @@
#pragma once
struct client_app
{
struct {
char* name;
char* id;
char* secret;
} client;
};
struct client_app* load_client_app_from_id( const char* client_id );
struct client_app* client_app_new( const char* client_name );
void client_app_save( struct client_app* app );

@ -0,0 +1,7 @@
{
"client": {
"id": "%s{app->client.id}",
"secret": "%s{app->client.secret}",
"name": "%s{app->client.name}"
}
}

@ -0,0 +1 @@
TODO: show a login page
Loading…
Cancel
Save