treebird/src/login.c
me@ow.nekobit.net d9c56fe1c3 Login oauth form
FossilOrigin-Name: e7f0102304e89879e9f7c633ed8947b05fa66af849b46b89a54b2c83d22cba83
2022-05-07 22:35:49 +00:00

212 lines
6.7 KiB
C

/*
* Treebird - Lightweight frontend for Pleroma
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <curl/curl.h>
#include <fcgi_stdio.h>
#include <string.h>
#include <stdlib.h>
#include "query.h"
#include "base_page.h"
#include "login.h"
#include "error.h"
#include "easprintf.h"
#include "../config.h"
#include "http.h"
// Files
#include "../static/login.chtml"
void apply_access_token(char* token)
{
printf("Set-Cookie: access_token=%s; Path=/; Max-Age=31536000\r\n", token);
printf("Set-Cookie: logged_in=t; Path=/; Max-Age=31536000\r\n");
// if config_url_prefix is empty, make it root
redirect(REDIRECT_303, config_url_prefix &&
config_url_prefix[0] != '\0' ? config_url_prefix : "/");
}
void content_login_oauth(struct session* ssn, mastodont_t* api, char** data)
{
struct mstdnt_storage storage = { 0 }, oauth_storage = { 0 };
struct mstdnt_app app;
struct mstdnt_oauth_token token;
char* orig_url = api->url;
char* redirect_url = getenv("SERVER_NAME");
char* decode_url = NULL;
char* urlify_redirect_url = NULL;
if (ssn->query.code)
{
apply_access_token(ssn->query.code);
}
else if (ssn->post.instance)
{
decode_url = curl_easy_unescape(api->curl, ssn->post.instance, 0, NULL);
easprintf(&urlify_redirect_url, "https://%s/login/oauth", redirect_url );
api->url = decode_url;
struct mstdnt_args args_app = {
.client_name = "Treebird",
.redirect_uris = urlify_redirect_url,
.scopes = "read+write+follow+push",
.website = ssn->post.instance
};
if (mastodont_register_app(api, &args_app, &storage, &app) == 0)
{
struct mstdnt_args args_token = {
.grant_type = "password",
.client_id = app.client_id,
.client_secret = app.client_secret,
.redirect_uri = NULL,
.scope = NULL,
.code = NULL,
.username = ssn->post.username,
.password = ssn->post.password
};
char* url;
char* encode_id = curl_easy_escape(api->curl, app.client_id, 0);
easprintf(&url, "%s/oauth/authorize?response_type=code&client_id=%s&redirect_uri=%s",
decode_url, encode_id, urlify_redirect_url);
// Set cookie and redirect
printf("Set-Cookie: instance_url=%s; Path=/; Max-Age=3153600\r\n", decode_url);
redirect(REDIRECT_303, url);
free(url);
curl_free(encode_id);
}
}
api->url = orig_url;
mastodont_storage_cleanup(&storage);
mastodont_storage_cleanup(&oauth_storage);
if (urlify_redirect_url) free(urlify_redirect_url);
if (decode_url) curl_free(decode_url);
}
void content_login(struct session* ssn, mastodont_t* api, char** data)
{
struct mstdnt_storage storage = { 0 }, oauth_store = { 0 };
struct mstdnt_app app;
struct mstdnt_oauth_token token;
char* error = NULL;
char* page;
if (ssn->post.username && ssn->post.password)
{
// Getting the client id/secret
struct mstdnt_args args_app = {
.client_name = "Treebird",
.redirect_uris = "http://localhost/",
.scopes = "read+write+follow+push",
.website = NULL
};
// Check if the username contains an @ symbol
char* address = strstr(ssn->post.username, "%40");
// If it fails, we need to restore
char* orig_url = api->url;
char* url_link = NULL;
if (address)
{
// Let is viewable as username
*address = '\0';
address += sizeof("%40")-1;
easprintf(&url_link, "https://%s/", address);
api->url = url_link;
}
else {
// Reset to instance url
api->url = config_instance_url;
}
if (mastodont_register_app(api, &args_app, &storage, &app) != 0)
{
error = construct_error(oauth_store.error, E_ERROR, 1, NULL);
}
else {
struct mstdnt_args args_token = {
.grant_type = "password",
.client_id = app.client_id,
.client_secret = app.client_secret,
.redirect_uri = NULL,
.scope = NULL,
.code = NULL,
.username = ssn->post.username,
.password = ssn->post.password
};
if (mastodont_obtain_oauth_token(api, &args_token, &oauth_store,
&token) != 0 && oauth_store.error)
{
error = construct_error(oauth_store.error, E_ERROR, 1, NULL);
}
else {
if (url_link)
printf("Set-Cookie: instance_url=%s; Path=/; Max-Age=31536000\r\n", url_link);
else
// Clear
printf("Set-Cookie: instance_url=; Path=/; Max-Age=-1\r\n");
apply_access_token(token.access_token);
if (url_link) free(url_link);
return;
}
}
if (url_link)
{
// Restore and cleanup, an error occured
api->url = orig_url;
free(url_link);
}
}
// Concat
easprintf(&page, data_login_html,
L10N[L10N_EN_US][L10N_LOGIN],
error ? error : "",
config_url_prefix,
L10N[L10N_EN_US][L10N_USERNAME],
L10N[L10N_EN_US][L10N_PASSWORD],
L10N[L10N_EN_US][L10N_LOGIN_BTN],
"Or",
config_url_prefix,
"Instance url",
"Authorize");
struct base_page b = {
.category = BASE_CAT_NONE,
.locale = L10N_EN_US,
.content = page,
.sidebar_left = NULL
};
// Output
render_base_page(&b, ssn, api);
// Cleanup
mastodont_storage_cleanup(&storage);
mastodont_storage_cleanup(&oauth_store);
if (error) free(error);
if (page) free(page);
}