* move some more vars from the session context to strbuf (the use of StrBufAppendTemp...
[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                 lprintf(CTDL_DEBUG, "Attempting to attach %s\n", bstr("openid_url"));
71
72                 snprintf(buf, sizeof buf,
73                         "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
74                         bstr("openid_url"),
75                         (is_https ? "https" : "http"), WC->http_host,
76                         (is_https ? "https" : "http"), WC->http_host
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         }
87
88         /* If we get to this point then something failed. */
89         display_openids();
90 }
91
92
93 /*
94  * Detach an OpenID from the currently logged-in account
95  */
96 void openid_detach(void) {
97         char buf[1024];
98
99         if (havebstr("id_to_detach")) {
100                 serv_printf("OIDD %s", bstr("id_to_detach"));
101                 serv_getln(buf, sizeof buf);
102                 if (buf[0] != '2') {
103                         strcpy(WC->ImportantMessage, &buf[4]);
104                 }
105         }
106
107         display_openids();
108 }
109
110 void 
111 InitModule_OPENID
112 (void)
113 {
114         WebcitAddUrlHandler(HKEY("display_openids"), display_openids, 0);
115         WebcitAddUrlHandler(HKEY("openid_attach"), openid_attach, 0);
116         WebcitAddUrlHandler(HKEY("openid_detach"), openid_detach, 0);
117 }