a4af9d257a1bb4dda1f7618017d3a14b19a8ad01
[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("beginboxx", NULL);
22
23         if (serv_info.serv_supports_openid) {
24
25                 wprintf("<table class=\"altern\">");
26         
27                 serv_puts("OIDL");
28                 serv_getln(buf, sizeof buf);
29                 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
30                         bg = 1 - bg;
31                         wprintf("<tr class=\"%s\">", (bg ? "even" : "odd"));
32                         wprintf("<td><img src=\"static/openid-small.gif\"></td><td>");
33                         escputs(buf);
34                         wprintf("</td><td>");
35                         wprintf("<a href=\"openid_detach?id_to_detach=");
36                         urlescputs(buf);
37                         wprintf("\" onClick=\"return confirm('%s');\">",
38                                 _("Do you really want to delete this OpenID?"));
39                         wprintf("%s</a>", _("(delete)"));
40                         wprintf("</td></tr>\n");
41                 }
42         
43                 wprintf("</table><br />\n");
44         
45                 wprintf("<form method=\"POST\" action=\"openid_attach\">\n");
46                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
47                 wprintf(_("Add an OpenID: "));
48                 wprintf("<input type=\"text\" name=\"openid_url\" class=\"openid_urlarea\" size=\"40\">\n");
49                 wprintf("<input type=\"submit\" name=\"attach_button\" value=\"%s\">"
50                         "</form></center>\n", _("Attach"));
51         }
52
53         else {
54                 wprintf(_("%s does not permit authentication via OpenID."), ChrPtr(serv_info.serv_humannode));
55         }
56
57         do_template("endbox", NULL);
58         wprintf("</div>");
59         wDumpContent(2);
60 }
61
62
63 /*
64  * Attempt to attach an OpenID to an existing, logged-in account
65  */
66 void openid_attach(void) {
67         char buf[4096];
68
69         if (havebstr("attach_button")) {
70                 wcsession *WCC = WC;
71
72                 lprintf(CTDL_DEBUG, "Attempting to attach %s\n", bstr("openid_url"));
73
74                 snprintf(buf, sizeof buf,
75                          "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
76                          bstr("openid_url"),
77                          (is_https ? "https" : "http"), ChrPtr(WCC->http_host),
78                          (is_https ? "https" : "http"), ChrPtr(WCC->http_host)
79                 );
80
81                 serv_puts(buf);
82                 serv_getln(buf, sizeof buf);
83                 if (buf[0] == '2') {
84                         lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
85                         http_redirect(&buf[4]);
86                         return;
87                 }
88         }
89
90         /* If we get to this point then something failed. */
91         display_openids();
92 }
93
94
95 /*
96  * Detach an OpenID from the currently logged-in account
97  */
98 void openid_detach(void) {
99         char buf[1024];
100
101         if (havebstr("id_to_detach")) {
102                 serv_printf("OIDD %s", bstr("id_to_detach"));
103                 serv_getln(buf, sizeof buf);
104                 if (buf[0] != '2') {
105                         strcpy(WC->ImportantMessage, &buf[4]);
106                 }
107         }
108
109         display_openids();
110 }
111
112 void 
113 InitModule_OPENID
114 (void)
115 {
116         WebcitAddUrlHandler(HKEY("display_openids"), display_openids, 0);
117         WebcitAddUrlHandler(HKEY("openid_attach"), openid_attach, 0);
118         WebcitAddUrlHandler(HKEY("openid_detach"), openid_detach, 0);
119 }