annotate src/c/openid.c @ 4:2d409aff8800

Received an OpenID authentication response, but haven't checked it yet
author Adam Chlipala <adam@chlipala.net>
date Sun, 26 Dec 2010 17:19:52 -0500
parents f59083771ee2
children 99496175078b
rev   line source
adam@1 1 #include <string.h>
adam@1 2
adam@0 3 #include <openssl/sha.h>
adam@0 4 #include <curl/curl.h>
adam@1 5 #include <expat.h>
adam@0 6
adam@1 7 #include <openid.h>
adam@0 8
adam@3 9 #define BUF_MAX 10240
adam@3 10 #define BUF_INIT 1024
adam@3 11
adam@2 12 struct uw_OpenidFfi_discovery {
adam@2 13 uw_Basis_string endpoint, localId;
adam@2 14 };
adam@2 15
adam@2 16 uw_Basis_string uw_OpenidFfi_endpoint(uw_context ctx, uw_OpenidFfi_discovery d) {
adam@2 17 return d->endpoint;
adam@2 18 }
adam@2 19
adam@2 20 uw_Basis_string uw_OpenidFfi_localId(uw_context ctx, uw_OpenidFfi_discovery d) {
adam@2 21 return d->localId;
adam@2 22 }
adam@2 23
adam@0 24 uw_unit uw_OpenidFfi_init(uw_context ctx) {
adam@4 25
adam@4 26
adam@0 27 curl_global_init(CURL_GLOBAL_ALL);
adam@0 28
adam@0 29 return uw_unit_v;
adam@0 30 }
adam@1 31
adam@1 32 static CURL *curl(uw_context ctx) {
adam@1 33 CURL *r;
adam@1 34
adam@1 35 if (!(r = uw_get_global(ctx, "curl"))) {
adam@1 36 r = curl_easy_init();
adam@1 37 if (r)
adam@1 38 uw_set_global(ctx, "curl", r, curl_easy_cleanup);
adam@1 39 }
adam@1 40
adam@1 41 return r;
adam@1 42 }
adam@1 43
adam@1 44 typedef struct {
adam@1 45 uw_context ctx;
adam@2 46 uw_OpenidFfi_discovery d;
adam@1 47 } endpoint;
adam@1 48
adam@1 49 static void XMLCALL startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
adam@1 50 endpoint *ep = userData;
adam@1 51
adam@1 52 if (!strcmp(name, "link")) {
adam@1 53 const XML_Char **attp;
adam@1 54 int found = 0;
adam@1 55
adam@1 56 for (attp = atts; *attp; attp += 2) {
adam@1 57 if (!strcmp(attp[0], "rel") && !strcmp(attp[1], "openid2.provider")) {
adam@1 58 found = 1;
adam@1 59 break;
adam@1 60 }
adam@1 61 }
adam@1 62
adam@1 63 if (found) {
adam@1 64 for (attp = atts; *attp; attp += 2) {
adam@1 65 if (!strcmp(attp[0], "href")) {
adam@2 66 ep->d->endpoint = uw_strdup(ep->ctx, attp[1]);
adam@1 67 return;
adam@1 68 }
adam@1 69 }
adam@1 70 }
adam@1 71 }
adam@1 72 }
adam@1 73
adam@1 74 static void XMLCALL endElement(void *userData, const XML_Char *name) {
adam@1 75 }
adam@1 76
adam@1 77 typedef struct {
adam@1 78 XML_Parser parser;
adam@1 79 int any_errors;
adam@3 80 } curl_discovery_data;
adam@1 81
adam@3 82 static size_t write_discovery_data(void *buffer, size_t size, size_t nmemb, void *userp) {
adam@3 83 curl_discovery_data *d = userp;
adam@1 84
adam@1 85 if (!XML_Parse(d->parser, buffer, size * nmemb, 0))
adam@1 86 d->any_errors = 1;
adam@1 87
adam@1 88 return size * nmemb;
adam@1 89 }
adam@1 90
adam@2 91 uw_OpenidFfi_discovery *uw_OpenidFfi_discover(uw_context ctx, uw_Basis_string id) {
adam@1 92 char *s;
adam@1 93 CURL *c = curl(ctx);
adam@3 94 curl_discovery_data cd = {};
adam@2 95 uw_OpenidFfi_discovery dy = uw_malloc(ctx, sizeof(struct uw_OpenidFfi_discovery));
adam@2 96 endpoint ep = {ctx, dy};
adam@1 97 CURLcode code;
adam@1 98
adam@2 99 dy->endpoint = dy->localId = NULL;
adam@2 100
adam@1 101 if (!strchr(id, ':')) {
adam@1 102 id = uw_Basis_strcat(ctx, "http://", id);
adam@1 103 if ((s = strchr(id, '#')) != NULL)
adam@1 104 *s = 0;
adam@1 105 } else if ((s = strchr(id, '#')) != NULL) {
adam@1 106 char *id2 = uw_malloc(ctx, s - id + 1);
adam@1 107 memcpy(id2, s, s - id);
adam@1 108 id2[s - id] = 0;
adam@1 109 id = id2;
adam@1 110 }
adam@1 111
adam@1 112 cd.parser = XML_ParserCreate(NULL);
adam@1 113 XML_SetUserData(cd.parser, &ep);
adam@1 114 uw_push_cleanup(ctx, (void (*)(void *))XML_ParserFree, cd.parser);
adam@1 115 XML_SetElementHandler(cd.parser, startElement, endElement);
adam@1 116
adam@1 117 curl_easy_setopt(c, CURLOPT_URL, id);
adam@3 118 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_discovery_data);
adam@1 119 curl_easy_setopt(c, CURLOPT_WRITEDATA, &cd);
adam@1 120
adam@1 121 code = curl_easy_perform(c);
adam@1 122 uw_pop_cleanup(ctx);
adam@1 123
adam@2 124 if (code || !ep.d->endpoint)
adam@1 125 return NULL;
adam@2 126 else {
adam@2 127 uw_OpenidFfi_discovery *dyp = malloc(sizeof(uw_OpenidFfi_discovery));
adam@2 128 *dyp = ep.d;
adam@2 129 return dyp;
adam@2 130 }
adam@1 131 }
adam@3 132
adam@3 133 uw_OpenidFfi_inputs uw_OpenidFfi_createInputs(uw_context ctx) {
adam@3 134 uw_buffer *r = uw_malloc(ctx, sizeof(uw_buffer));
adam@3 135 uw_buffer_init(BUF_MAX, r, BUF_INIT);
adam@3 136 return r;
adam@3 137 }
adam@3 138
adam@3 139 static int okForPost(const char *s) {
adam@3 140 for (; *s; ++s)
adam@3 141 if (*s == '=' || *s == '&')
adam@3 142 return 0;
adam@3 143 return 1;
adam@3 144 }
adam@3 145
adam@3 146 uw_unit uw_OpenidFfi_addInput(uw_context ctx, uw_OpenidFfi_inputs buf, uw_Basis_string key, uw_Basis_string value) {
adam@3 147 if (!okForPost(key))
adam@3 148 uw_error(ctx, FATAL, "Invalid key for OpenID inputs");
adam@3 149 if (!okForPost(value))
adam@3 150 uw_error(ctx, FATAL, "Invalid value for OpenID inputs");
adam@3 151
adam@3 152 if (uw_buffer_used(buf) > 0)
adam@3 153 uw_buffer_append(buf, "&", 1);
adam@3 154
adam@3 155 uw_buffer_append(buf, key, strlen(key));
adam@3 156 uw_buffer_append(buf, "=", 1);
adam@3 157 uw_buffer_append(buf, value, strlen(value));
adam@3 158
adam@3 159 return uw_unit_v;
adam@3 160 }
adam@3 161
adam@3 162 uw_Basis_string uw_OpenidFfi_getOutput(uw_context ctx, uw_OpenidFfi_outputs buf, uw_Basis_string key) {
adam@3 163 char *s = buf->start;
adam@3 164
adam@3 165 for (; *s; s = strchr(strchr(s, 0)+1, 0)+1)
adam@3 166 if (!strcmp(key, s))
adam@3 167 return strchr(s, 0)+1;
adam@3 168
adam@3 169 return NULL;
adam@3 170 }
adam@3 171
adam@3 172 static size_t write_buffer_data(void *buffer, size_t size, size_t nmemb, void *userp) {
adam@3 173 uw_buffer *buf = userp;
adam@3 174
adam@3 175 uw_buffer_append(buf, buffer, size * nmemb);
adam@3 176
adam@3 177 return size * nmemb;
adam@3 178 }
adam@3 179
adam@3 180 const char curl_failure[] = "error\0Error fetching URL";
adam@3 181
adam@4 182 uw_OpenidFfi_outputs uw_OpenidFfi_direct(uw_context ctx, uw_Basis_string url, uw_OpenidFfi_inputs inps) {
adam@3 183 uw_buffer *buf = uw_malloc(ctx, sizeof(uw_buffer));
adam@3 184 CURL *c = curl(ctx);
adam@3 185 CURLcode code;
adam@3 186
adam@3 187 uw_buffer_init(BUF_MAX, buf, BUF_INIT);
adam@3 188
adam@3 189 uw_buffer_append(inps, "", 1);
adam@3 190
adam@3 191 curl_easy_setopt(c, CURLOPT_URL, url);
adam@3 192 curl_easy_setopt(c, CURLOPT_POSTFIELDS, inps->start);
adam@3 193 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_buffer_data);
adam@3 194 curl_easy_setopt(c, CURLOPT_WRITEDATA, buf);
adam@3 195
adam@3 196 code = curl_easy_perform(c);
adam@3 197
adam@3 198 uw_buffer_append(buf, "", 1);
adam@3 199
adam@3 200 if (code) {
adam@3 201 uw_buffer_reset(buf);
adam@3 202 uw_buffer_append(buf, curl_failure, sizeof curl_failure);
adam@3 203 } else {
adam@3 204 char *s;
adam@3 205
adam@3 206 s = buf->start;
adam@3 207 while (*s) {
adam@3 208 char *colon = strchr(s, ':'), *newline;
adam@3 209
adam@3 210 if (!colon) {
adam@3 211 *s = 0;
adam@3 212 break;
adam@3 213 }
adam@3 214
adam@3 215 newline = strchr(colon+1, '\n');
adam@3 216
adam@3 217 if (!newline) {
adam@3 218 *s = 0;
adam@3 219 break;
adam@3 220 }
adam@3 221
adam@3 222 *colon = 0;
adam@3 223 *newline = 0;
adam@3 224 s = newline+1;
adam@3 225 }
adam@3 226 }
adam@3 227
adam@3 228 return buf;
adam@3 229 }
adam@4 230
adam@4 231 static uw_Basis_string deurl(uw_context ctx, uw_Basis_string s) {
adam@4 232 uw_Basis_string r = uw_malloc(ctx, strlen(s)), s2 = r;
adam@4 233
adam@4 234 for (; *s; ++s) {
adam@4 235 if (s[0] == '%' && s[1] && s[2]) {
adam@4 236 unsigned u;
adam@4 237
adam@4 238 sscanf(s+1, "%02x", &u);
adam@4 239 *s2++ = u;
adam@4 240 s += 2;
adam@4 241 } else
adam@4 242 *s2++ = *s;
adam@4 243 }
adam@4 244
adam@4 245 *s2 = 0;
adam@4 246 return r;
adam@4 247 }
adam@4 248
adam@4 249 uw_OpenidFfi_outputs uw_OpenidFfi_indirect(uw_context ctx, uw_Basis_string fields) {
adam@4 250 uw_OpenidFfi_outputs b = malloc(sizeof(uw_buffer));
adam@4 251
adam@4 252 uw_buffer_init(BUF_MAX, b, BUF_INIT);
adam@4 253
adam@4 254 while (*fields) {
adam@4 255 char *equal = strchr(fields, '='), *and, *s;
adam@4 256
adam@4 257 if (!equal)
adam@4 258 break;
adam@4 259
adam@4 260 *equal = 0;
adam@4 261 s = deurl(ctx, fields);
adam@4 262 uw_buffer_append(b, s, strlen(s));
adam@4 263 uw_buffer_append(b, "", 1);
adam@4 264
adam@4 265 and = strchr(equal+1, '&');
adam@4 266 if (and) {
adam@4 267 *and = 0;
adam@4 268 fields = and+1;
adam@4 269 } else
adam@4 270 fields = and = strchr(equal+1, 0);
adam@4 271 s = deurl(ctx, equal+1);
adam@4 272 uw_buffer_append(b, s, strlen(s));
adam@4 273 uw_buffer_append(b, "", 1);
adam@4 274 }
adam@4 275
adam@4 276 uw_buffer_append(b, "", 1);
adam@4 277 return b;
adam@4 278 }