aboutsummaryrefslogtreecommitdiffstats
path: root/src/login.c
blob: 67b6b22a069bf64cbaef214bb47da1baaea415d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
  This file is part of demiurge.

  demiurge is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  demiurge 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with demiurge.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <readline/readline.h>
#include <json-c/json.h>
#include "asprintf.h"
#include "util.h"

/* I am confident to present you, without a doubt, the worst code i've
 * ever written */

static int
get_client_id(struct config *config)
{
	char *api_url = "/api/v1/apps";

	CURL *curl = curl_easy_init();

	if(curl == NULL) {
		fprintf(stderr, "Error creating curl (what?)\n");
		return -1;
	}

	struct memory chunk = {0};
	
	char *post_url = NULL;
	dm_asprintf(&post_url, "%s%s", config->instance, api_url);
	curl_easy_setopt(curl, CURLOPT_URL, post_url);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

	/* TODO: Better way lol */
	/* If someone wants to do it, use curl_mime thing, I am to lazy
	 * to do this */

	curl_easy_setopt(curl,
		CURLOPT_POSTFIELDS,
		"client_name=demiurge"
		"&redirect_uris=urn:ietf:wg:oauth:2.0:oob"
		"&scope=read+write+follow"
		"&website=https://example.org");

	curl_easy_perform(curl);
	curl_easy_cleanup(curl);
	free(post_url);

	struct json_object *parsed_json;
	struct json_object *json_client_id;
	struct json_object *json_client_secret;
	parsed_json = json_tokener_parse(chunk.response);
	if(parsed_json == NULL) {
		fprintf(stderr, "error\n");
		free_response(&chunk);
		return -1;
	}

	json_object_object_get_ex(parsed_json, "client_id", &json_client_id);
	json_object_object_get_ex(parsed_json, "client_secret", &json_client_secret);

	const char *client_id = json_object_get_string(json_client_id);
	const char *client_secret = json_object_get_string(json_client_secret);
	int ret;

	if(!client_id || !client_secret) {
		eputs("Can't get client_id or client_secret");
		ret = -1;
	} else {
		dm_strncpy(config->client_id, client_id, sizeof(config->client_id));
		dm_strncpy(config->client_secret, client_secret, sizeof(config->client_secret));
		ret = 0;
	}

	free_response(&chunk);
	return ret;
}

static char *
ask_for_token(struct config *config)
{
	const char *api_url = "/oauth/authorize?";
	char *fmt = "%s%s"
		"client_id=%s"
		"&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
		"&response_type=code"
		"&scope=read+write+follow\n";

	printf(fmt, config->instance, api_url, config->client_id);

	return readline("Please log in in that url and paste the code here: ");
}

static int
get_token(struct config *config, const char *code)
{
	char *fmt =
		"client_id=%s"
		"&client_secret=%s"
		"&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
		"&code=%s"
		"&grant_type=authorization_code"
		"&scope=read+write+follow";
	const char *api_url = "/oauth/token";

	CURL *curl = curl_easy_init();
	if(curl == NULL) {
		fprintf(stderr, "Error creating curl (what?)\n");
		return -1;
	}

	struct memory chunk = {0};

	char *post_token_url;
	char *post_url;

	dm_asprintf(&post_token_url, fmt, config->client_id, config->client_secret, code);
	dm_asprintf(&post_url, "%s%s", config->instance, api_url);

	curl_easy_setopt(curl, CURLOPT_URL, post_url);
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_token_url);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

	curl_easy_perform(curl);
	curl_easy_cleanup(curl);
	free(post_token_url);
	free(post_url);

	struct json_object *parsed_json;
	struct json_object *json_access_token;
	
	parsed_json = json_tokener_parse(chunk.response);
	json_object_object_get_ex(parsed_json, "access_token", &json_access_token);
	const char *access_token = json_object_get_string(json_access_token);
	int ret;
	if(!access_token) {
		eputs("Can't get access_token");
		ret = -1;
	} else {
		dm_strncpy(config->access_token, access_token, sizeof(config->access_token));
		ret = 0;
	}

	free_response(&chunk);

	return ret;
}

int
setup()
{
	struct config config;

	dm_strncpy(config.instance, readline("Enter your instance (e.g. https://social.fnord.tld) "),
		sizeof(config.instance));

	if(get_client_id(&config) < 0)
		return -1;

	char *code = ask_for_token(&config);

	if(get_token(&config, code) < 0)
	{
		free(code);
		return -1;
	}

	store_config(&config);

	free(code);
	return 0;
}