c9c0464e8f4b601d78ddde436580a18a5f2fcd95
[citadel.git] / webcit / openid.c
1 /*
2  * Copyright (c) 1996-2012 by the citadel.org team
3  *
4  * This program is open source software.  You can redistribute it and/or
5  * modify it under the terms of the GNU General Public License, version 3.
6  * 
7  * 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * 
15  * 
16  * 
17  */
18
19 #include "webcit.h"
20 #include "webserver.h"
21
22 /*
23  * Display the OpenIDs associated with an account
24  */
25 void display_openids(void)
26 {
27         wcsession *WCC = WC;
28         char buf[1024];
29         int bg = 0;
30
31         output_headers(1, 1, 1, 0, 0, 0);
32
33         do_template("box_begin_1");
34         StrBufAppendBufPlain(WCC->WBuf, _("Manage Account/OpenID Associations"), -1, 0);
35         do_template("box_begin_2");
36
37         if (WCC->serv_info->serv_supports_openid) {
38
39                 wc_printf("<table class=\"altern\">");
40         
41                 serv_puts("OIDL");
42                 serv_getln(buf, sizeof buf);
43                 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
44                         bg = 1 - bg;
45                         wc_printf("<tr class=\"%s\">", (bg ? "even" : "odd"));
46                         wc_printf("<td><img src=\"static/webcit_icons/openid-small.gif\"></td><td>");
47                         escputs(buf);
48                         wc_printf("</td><td>");
49                         wc_printf("<a href=\"openid_detach?id_to_detach=");
50                         urlescputs(buf);
51                         wc_printf("\" onClick=\"return confirm('%s');\">",
52                                 _("Do you really want to delete this OpenID?"));
53                         wc_printf("%s</a>", _("(delete)"));
54                         wc_printf("</td></tr>\n");
55                 }
56         
57                 wc_printf("</table><br>\n");
58         
59                 wc_printf("<form method=\"POST\" action=\"openid_attach\">\n");
60                 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WCC->nonce);
61                 wc_printf(_("Add an OpenID: "));
62                 wc_printf("<input type=\"text\" name=\"openid_url\" class=\"openid_urlarea\" size=\"40\">\n");
63                 wc_printf("<input type=\"submit\" name=\"attach_button\" value=\"%s\">"
64                         "</form></center>\n", _("Attach"));
65         }
66
67         else {
68                 wc_printf(_("%s does not permit authentication via OpenID."), ChrPtr(WCC->serv_info->serv_humannode));
69         }
70
71         do_template("box_end");
72         wDumpContent(2);
73 }
74
75
76 /*
77  * Attempt to attach an OpenID to an existing, logged-in account
78  */
79 void openid_attach(void) {
80         char buf[4096];
81
82         if (havebstr("attach_button")) {
83
84                 syslog(LOG_DEBUG, "Attempting to attach %s\n", bstr("openid_url"));
85
86                 snprintf(buf, sizeof buf,
87                         "OIDS %s|%s/finalize_openid_login?attach_existing=1|%s",
88                         bstr("openid_url"),
89                         ChrPtr(site_prefix),
90                         ChrPtr(site_prefix)
91                 );
92
93                 serv_puts(buf);
94                 serv_getln(buf, sizeof buf);
95                 if (buf[0] == '2') {
96                         syslog(LOG_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
97                         http_redirect(&buf[4]);
98                         return;
99                 }
100                 else {
101                         syslog(LOG_DEBUG, "OpenID attach failed: %s\n", &buf[4]);
102                 }
103         }
104
105         /* If we get to this point then something failed. */
106         display_openids();
107 }
108
109
110 /*
111  * Detach an OpenID from the currently logged-in account
112  */
113 void openid_detach(void) {
114         StrBuf *Line;
115
116         if (havebstr("id_to_detach")) {
117                 serv_printf("OIDD %s", bstr("id_to_detach"));
118                 Line = NewStrBuf();
119                 StrBuf_ServGetln(Line);
120                 GetServerStatusMsg(Line, NULL, 1, 2);
121                 FreeStrBuf(&Line);
122         }
123
124         display_openids();
125 }
126
127 void 
128 InitModule_OPENID
129 (void)
130 {
131         WebcitAddUrlHandler(HKEY("display_openids"), "", 0, display_openids, 0);
132         WebcitAddUrlHandler(HKEY("openid_attach"), "", 0, openid_attach, 0);
133         WebcitAddUrlHandler(HKEY("openid_detach"), "", 0, openid_detach, 0);
134 }