summaryrefslogtreecommitdiff
path: root/src/client/libwebidoidc-client.c
blob: f31c100aae75fba75e62b75cb44f4ef97c593577 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// disfluid, implementation of the Solid specification
// Copyright (C) 2020, 2021  Vivien Kraus

// 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 <Disfluid-0.h>
#include <libguile.h>
#include <string.h>

struct DisfluidApi
{
  SCM scm_make_client;
  SCM scm_get_client_id;
  SCM scm_get_client_jwk;
  SCM scm_get_client_redirect_uri;
  SCM scm_make_account_full;
  SCM scm_get_account_subject;
  SCM scm_get_account_issuer;
  SCM scm_get_account_key_pair;
  SCM scm_get_account_id_token_header;
  SCM scm_get_account_id_token;
  SCM scm_get_account_access_token;
  SCM scm_get_account_refresh_token;
};

struct user_code
{
  DisfluidUser func;
  void *data;
};

static void *
inner_guile (void *data)
{
  struct user_code *user = data;
  struct DisfluidApi api;
  api.scm_make_client =
    scm_c_public_ref ("webid-oidc client reverse-stubs", "make-client");
  api.scm_get_client_id =
    scm_c_public_ref ("webid-oidc client reverse-stubs", "get-client-id");
  api.scm_get_client_jwk =
    scm_c_public_ref ("webid-oidc client reverse-stubs", "get-client-jwk");
  api.scm_get_client_redirect_uri =
    scm_c_public_ref ("webid-oidc client reverse-stubs",
		      "get-client-redirect-uri");
  api.scm_make_account_full =
    scm_c_public_ref ("webid-oidc client reverse-stubs", "make-account-full");
  api.scm_get_account_subject =
    scm_c_public_ref ("webid-oidc client reverse-stubs",
		      "get-account-subject");
  api.scm_get_account_issuer =
    scm_c_public_ref ("webid-oidc client reverse-stubs",
		      "get-account-issuer");
  api.scm_get_account_key_pair =
    scm_c_public_ref ("webid-oidc client reverse-stubs",
		      "get-account-key-pair");
  api.scm_get_account_id_token_header =
    scm_c_public_ref ("webid-oidc client reverse-stubs",
		      "get-account-id-token-header");
  api.scm_get_account_id_token =
    scm_c_public_ref ("webid-oidc client reverse-stubs",
		      "get-account-id-token");
  api.scm_get_account_access_token =
    scm_c_public_ref ("webid-oidc client reverse-stubs",
		      "get-account-access-token");
  api.scm_get_account_refresh_token =
    scm_c_public_ref ("webid-oidc client reverse-stubs",
		      "get-account-refresh-token");
  return user->func (&api, user->data);
}

void *
disfluid_api_init (DisfluidUser func, void *data)
{
  struct user_code code;
  code.func = func;
  code.data = data;
  return scm_with_guile (inner_guile, &code);
}

struct DisfluidClient
{
  SCM object;
};

void
disfluid_api_make_client (const struct DisfluidApi *api,
			  struct DisfluidClient **client,
			  const char *client_id,
			  const char *redirect_uri, const char *jwk)
{
  SCM scm_client_id = scm_from_utf8_string (client_id);
  SCM scm_redirect_uri = scm_from_utf8_string (redirect_uri);
  SCM scm_jwk = SCM_BOOL_F;
  if (jwk)
    {
      scm_jwk = scm_from_utf8_string (jwk);
    }
  SCM object = scm_call_3 (api->scm_make_client, scm_client_id, scm_jwk,
			   scm_redirect_uri);
  scm_dynwind_begin (0);
  *client = scm_malloc (sizeof (struct DisfluidClient));
  scm_dynwind_unwind_handler (free, *client, 0);
  (*client)->object = scm_gc_protect_object (object);
  scm_dynwind_end ();
}

void
disfluid_client_free (struct DisfluidClient *client)
{
  if (client)
    {
      scm_gc_unprotect_object (client->object);
    }
  free (client);
}

void
disfluid_client_copy (const DisfluidClient * client, const DisfluidApi * api,
		      DisfluidClient ** copy)
{
  scm_dynwind_begin (0);
  *copy = scm_malloc (sizeof (struct DisfluidClient));
  scm_dynwind_unwind_handler (free, *copy, 0);
  (*copy)->object = scm_gc_protect_object (client->object);
  scm_dynwind_end ();
}

static size_t
copy_scm_string (SCM string, size_t start, size_t max, char *dest)
{
  size_t total_length = 0;
  scm_dynwind_begin (0);
  char *all_bytes = NULL;
  if (scm_is_true (string))
    {
      all_bytes = scm_to_utf8_stringn (string, &total_length);
      scm_dynwind_free (all_bytes);
    }
  const size_t requested_length = max;
  const size_t available_length = total_length - start;
  size_t copied_length = requested_length;
  if (available_length < copied_length)
    {
      copied_length = available_length;
    }
  memcpy (dest, all_bytes + start, copied_length);
  if (copied_length < max)
    {
      dest[copied_length] = '\0';
    }
  scm_dynwind_end ();
  return total_length;
}

size_t
disfluid_client_get_id (const struct DisfluidClient *client,
			const struct DisfluidApi *api,
			size_t start, size_t max, char *id)
{
  SCM scm_id = scm_call_1 (api->scm_get_client_id, client->object);
  return copy_scm_string (scm_id, start, max, id);
}

size_t
disfluid_client_get_key_pair (const struct DisfluidClient *client,
			      const struct DisfluidApi *api,
			      size_t start, size_t max, char *jwk)
{
  SCM scm_jwk = scm_call_1 (api->scm_get_client_jwk, client->object);
  return copy_scm_string (scm_jwk, start, max, jwk);
}

size_t
disfluid_client_get_redirect_uri (const struct DisfluidClient *client,
				  const struct DisfluidApi *api,
				  size_t start, size_t max,
				  char *redirect_uri)
{
  SCM scm_uri = scm_call_1 (api->scm_get_client_redirect_uri, client->object);
  return copy_scm_string (scm_uri, start, max, redirect_uri);
}

struct DisfluidAccount
{
  SCM object;
};

void
disfluid_api_make_account_full (const struct DisfluidApi *api,
				struct DisfluidAccount **account,
				const char *subject,
				const char *issuer,
				const char *key_pair,
				const char *id_token_header,
				const char *id_token,
				const char *access_token,
				const char *refresh_token)
{
  SCM scm_subject = scm_from_utf8_string (subject);
  SCM scm_issuer = scm_from_utf8_string (issuer);
  SCM scm_key_pair = scm_from_utf8_string (key_pair);
  SCM scm_id_token_header = SCM_BOOL_F;
  SCM scm_id_token = SCM_BOOL_F;
  SCM scm_access_token = SCM_BOOL_F;
  SCM scm_refresh_token = SCM_BOOL_F;
  if (id_token_header)
    {
      scm_id_token_header = scm_from_utf8_string (id_token_header);
    }
  if (id_token)
    {
      scm_id_token = scm_from_utf8_string (id_token);
    }
  if (access_token)
    {
      scm_access_token = scm_from_utf8_string (access_token);
    }
  if (refresh_token)
    {
      scm_refresh_token = scm_from_utf8_string (refresh_token);
    }
}

void
disfluid_account_copy (const DisfluidAccount * account,
		       const DisfluidApi * api, DisfluidAccount ** copy)
{
  scm_dynwind_begin (0);
  *copy = scm_malloc (sizeof (struct DisfluidAccount));
  scm_dynwind_unwind_handler (free, *copy, 0);
  (*copy)->object = scm_gc_protect_object (account->object);
  scm_dynwind_end ();
}

void
disfluid_account_free (struct DisfluidAccount *account)
{
  if (account)
    {
      scm_gc_unprotect_object (account->object);
    }
  free (account);
}

size_t
disfluid_account_get_subject (const struct DisfluidAccount *account,
			      const struct DisfluidApi *api,
			      size_t start, size_t max, char *subject)
{
  SCM scm_id = scm_call_1 (api->scm_get_account_subject, account->object);
  return copy_scm_string (scm_id, start, max, subject);
}

size_t
disfluid_account_get_issuer (const struct DisfluidAccount *account,
			     const struct DisfluidApi *api,
			     size_t start, size_t max, char *issuer)
{
  SCM scm_id = scm_call_1 (api->scm_get_account_issuer, account->object);
  return copy_scm_string (scm_id, start, max, issuer);
}

size_t
disfluid_account_get_key_pair (const struct DisfluidAccount *account,
			       const struct DisfluidApi *api,
			       size_t start, size_t max, char *jwk)
{
  SCM scm_id = scm_call_1 (api->scm_get_account_key_pair, account->object);
  return copy_scm_string (scm_id, start, max, jwk);
}

size_t
disfluid_account_get_id_token_header (const struct DisfluidAccount *account,
				      const struct DisfluidApi *api,
				      size_t start, size_t max, char *header)
{
  SCM scm_id =
    scm_call_1 (api->scm_get_account_id_token_header, account->object);
  return copy_scm_string (scm_id, start, max, header);
}

size_t
disfluid_account_get_id_token (const struct DisfluidAccount *account,
			       const struct DisfluidApi *api,
			       size_t start, size_t max, char *token)
{
  SCM scm_id = scm_call_1 (api->scm_get_account_id_token, account->object);
  return copy_scm_string (scm_id, start, max, token);
}

size_t
disfluid_account_get_access_token (const struct DisfluidAccount *account,
				   const struct DisfluidApi *api,
				   size_t start, size_t max, char *token)
{
  SCM scm_id =
    scm_call_1 (api->scm_get_account_access_token, account->object);
  return copy_scm_string (scm_id, start, max, token);
}

size_t
disfluid_account_get_refresh_token (const struct DisfluidAccount *account,
				    const struct DisfluidApi *api,
				    size_t start, size_t max, char *token)
{
  SCM scm_id =
    scm_call_1 (api->scm_get_account_refresh_token, account->object);
  return copy_scm_string (scm_id, start, max, token);
}