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