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