New global variable 'site_prefix' which replaces the often-repeated code which constr...
[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
70                 lprintf(CTDL_DEBUG, "Attempting to attach %s\n", bstr("openid_url"));
71
72                 snprintf(buf, sizeof buf,
73                         "OIDS %s|%s/finalize_openid_login|%s",
74                         bstr("openid_url"),
75                         ChrPtr(site_prefix),
76                         ChrPtr(site_prefix)
77                 );
78
79                 serv_puts(buf);
80                 serv_getln(buf, sizeof buf);
81                 if (buf[0] == '2') {
82                         lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
83                         http_redirect(&buf[4]);
84                         return;
85                 }
86                 else {
87                         lprintf(CTDL_DEBUG, "OpenID attach failed: %s\n", &buf[4]);
88                 }
89         }
90
91         /* If we get to this point then something failed. */
92         display_openids();
93 }
94
95
96 /*
97  * Detach an OpenID from the currently logged-in account
98  */
99 void openid_detach(void) {
100         char buf[1024];
101
102         if (havebstr("id_to_detach")) {
103                 serv_printf("OIDD %s", bstr("id_to_detach"));
104                 serv_getln(buf, sizeof buf);
105                 if (buf[0] != '2') {
106                         strcpy(WC->ImportantMessage, &buf[4]);
107                 }
108         }
109
110         display_openids();
111 }
112
113 void 
114 InitModule_OPENID
115 (void)
116 {
117         WebcitAddUrlHandler(HKEY("display_openids"), "", 0, display_openids, 0);
118         WebcitAddUrlHandler(HKEY("openid_attach"), "", 0, openid_attach, 0);
119         WebcitAddUrlHandler(HKEY("openid_detach"), "", 0, openid_detach, 0);
120 }