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