OAuth token generation

FossilOrigin-Name: 805b627e15039644eb40858b1497747ddef1a6f76a59789304e0e7167f87fe50
This commit is contained in:
me@ow.nekobit.net 2022-02-15 20:32:50 +00:00
parent 828a7dfa8d
commit 9093a35aef
2 changed files with 127 additions and 7 deletions

View file

@ -45,13 +45,36 @@ struct mstdnt_app
char* vapid_key;
};
int mstdnt_load_app_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_app* app);
struct mstdnt_oauth_token
{
char* access_token;
char* token_type;
char* scope;
char* id;
char* me;
time_t time;
};
struct mstdnt_oauth_token_args
{
char* grant_type;
char* client_id;
char* client_secret;
char* redirect_uri;
char* scope;
char* code;
char* username;
char* password;
};
int mastodont_register_app(mastodont_t* data,
struct mstdnt_app_register_args* args,
struct mstdnt_storage* storage,
struct mstdnt_app* app);
int mastodont_obtain_oauth_token(mastodont_t* data,
struct mstdnt_oauth_token_args* args,
struct mstdnt_storage* storage,
struct mstdnt_oauth_token* app);
#endif /* MASTODONT_ACCOUNT */

View file

@ -18,9 +18,9 @@
#include <mastodont_application.h>
#include <mastodont_query.h>
int mstdnt_load_app_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_app* app)
static int mstdnt_read_app_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_app* app)
{
cJSON* root, *v;
if (_mstdnt_json_init(&root, results, storage))
@ -46,6 +46,36 @@ int mstdnt_load_app_result(struct mstdnt_storage* storage,
}
}
static int mstdnt_read_token_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_oauth_token* app)
{
cJSON* root, *v;
if (_mstdnt_json_init(&root, results, storage))
return 1;
struct _mstdnt_str_val strings[] = {
{ "grant_type", &(app->grant_type) },
{ "client_id", &(app->client_id) },
{ "client_secret", &(app->client_secret) },
{ "redirect_uri", &(app->redirect_uri) },
{ "scope", &(app->scope) },
{ "code", &(app->code) },
{ "username", &(app->username) },
{ "password", &(app->password) },
};
for (v = root->child; v; v = v->next)
{
if (_mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings),
NULL, 0) == 1)
{
return 1;
}
}
}
int mastodont_register_app(mastodont_t* data,
struct mstdnt_app_register_args* args,
@ -91,7 +121,7 @@ int mastodont_register_app(mastodont_t* data,
goto cleanup;
}
res = mstdnt_load_app_result(storage, &results, app);
res = mstdnt_read_app_result(storage, &results, app);
mastodont_fetch_results_cleanup(&results);
@ -100,3 +130,70 @@ cleanup:
return res;
}
int mastodont_obtain_oauth_token(mastodont_t* data,
struct mstdnt_oauth_token_args* args,
struct mstdnt_storage* storage,
struct mstdnt_oauth_token* token)
{
int res;
struct mstdnt_fetch_results results = { 0 };
/* Default args */
struct mstdnt_oauth_token_args _args;
if (args == NULL)
{
_args.grant_type = NULL;
_args.client_id = NULL;
_args.client_secret = NULL;
_args.redirect_uri = NULL;
_args.scope = NULL;
_args.code = NULL;
_args.username = NULL;
_args.password = NULL;
args = &_args;
}
storage->needs_cleanup = 0;
union param_value u_grant_type, u_client_id,
u_client_secret, u_redirect_uri,
u_scope, u_code, u_username, u_password;
u_grant_type.s = args->grant_type;
u_client_id.s = args->client_id;
u_client_secret.s = args->client_secret;
u_redirect_uri.s = args->redirect_uri;
u_scope.s = args->scope;
u_code.s = args->code;
u_username.s = args->username;
u_password.s = args->password;
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_STRING, "grant_type", u_grant_type },
{ _MSTDNT_QUERY_STRING, "client_id", u_client_id },
{ _MSTDNT_QUERY_STRING, "client_secret", u_client_secret },
{ _MSTDNT_QUERY_STRING, "redirect_uri", u_redirect_uri },
{ _MSTDNT_QUERY_STRING, "scope", u_scope },
{ _MSTDNT_QUERY_STRING, "code", u_code },
{ _MSTDNT_QUERY_STRING, "username", u_username },
{ _MSTDNT_QUERY_STRING, "password", u_password },
};
char* post = _mstdnt_query_string(NULL, params, _mstdnt_arr_len(params));
curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post);
if (mastodont_fetch_curl(data, "oauth/token", &results) != CURLE_OK)
{
res = 1;
goto cleanup;
}
res = mstdnt_read_token_result(storage, &results, token);
mastodont_fetch_results_cleanup(&results);
cleanup:
free(post);
return res;
}