Implemented the code to detach an OpenID from an account.
[citadel.git] / webcit / openid.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7
8 /*
9  * Display the OpenIDs associated with an account
10  */
11 void display_openids(void)
12 {
13         char buf[1024];
14         int bg = 0;
15
16         output_headers(1, 1, 1, 0, 0, 0);
17
18         wprintf("<div class=\"fix_scrollbar_bug\">");
19
20         svput("BOXTITLE", WCS_STRING, _("Manage Account/OpenID Associations"));
21         do_template("beginbox");
22
23         wprintf("<table class=\"altern\">");
24
25         serv_puts("OIDL");
26         serv_getln(buf, sizeof buf);
27         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
28                 bg = 1 - bg;
29                 wprintf("<tr class=\"%s\">", (bg ? "even" : "odd"));
30                 wprintf("<td><img src=\"static/openid-small.gif\"></td><td>");
31                 escputs(buf);
32                 wprintf("</td><td>");
33                 wprintf("<a href=\"openid_detach?id_to_detach=");
34                 urlescputs(buf);
35                 wprintf("\" onClick=\"return confirm('%s');\">",
36                         _("Do you really want to delete this OpenID?"));
37                 wprintf("%s</a>", _("(delete)"));
38                 wprintf("</td></tr>\n");
39         }
40
41         wprintf("</table><br />\n");
42
43         wprintf("<form method=\"POST\" action=\"openid_attach\">\n");
44         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
45         wprintf(_("Add an OpenID: "));
46         wprintf("<input type=\"text\" name=\"openid_url\" class=\"openid_urlarea\" size=\"40\">\n");
47         wprintf("<input type=\"submit\" name=\"attach_button\" value=\"%s\">"
48                 "</form></center>\n", _("Attach"));
49         do_template("endbox");
50         wprintf("</div>");
51         wDumpContent(2);
52 }
53
54
55 /*
56  * Attempt to attach an OpenID to an existing, logged-in account
57  */
58 void openid_attach(void) {
59         char buf[4096];
60
61         if (havebstr("attach_button")) {
62                 lprintf(CTDL_DEBUG, "Attempting to attach %s\n", bstr("openid_url"));
63
64                 snprintf(buf, sizeof buf,
65                         "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
66                         bstr("openid_url"),
67                         (is_https ? "https" : "http"), WC->http_host,
68                         (is_https ? "https" : "http"), WC->http_host
69                 );
70
71                 serv_puts(buf);
72                 serv_getln(buf, sizeof buf);
73                 if (buf[0] == '2') {
74                         lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
75                         http_redirect(&buf[4]);
76                         return;
77                 }
78         }
79
80         /* If we get to this point then something failed. */
81         display_openids();
82 }
83
84
85 /*
86  * Detach an OpenID from the currently logged-in account
87  */
88 void openid_detach(void) {
89         char buf[1024];
90
91         if (havebstr("id_to_detach")) {
92                 serv_printf("OIDD %s", bstr("id_to_detach"));
93                 serv_getln(buf, sizeof buf);
94                 if (buf[0] != '2') {
95                         strcpy(WC->ImportantMessage, &buf[4]);
96                 }
97         }
98
99         display_openids();
100 }