Back to index

webcit  8.12-dfsg
openid.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1996-2012 by the citadel.org team
00003  *
00004  * This program is open source software.  You can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License, version 3.
00006  *
00007  * This program is distributed in the hope that it will be useful,
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  * GNU General Public License for more details.
00011  */
00012 
00013 #include "webcit.h"
00014 #include "webserver.h"
00015 
00016 /*
00017  * Display the OpenIDs associated with an account
00018  */
00019 void display_openids(void)
00020 {
00021        wcsession *WCC = WC;
00022        char buf[1024];
00023        int bg = 0;
00024 
00025        output_headers(1, 1, 1, 0, 0, 0);
00026 
00027        do_template("box_begin_1");
00028        StrBufAppendBufPlain(WCC->WBuf, _("Manage Account/OpenID Associations"), -1, 0);
00029        do_template("box_begin_2");
00030 
00031        if (WCC->serv_info->serv_supports_openid) {
00032 
00033               wc_printf("<table class=\"altern\">");
00034        
00035               serv_puts("OIDL");
00036               serv_getln(buf, sizeof buf);
00037               if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00038                      bg = 1 - bg;
00039                      wc_printf("<tr class=\"%s\">", (bg ? "even" : "odd"));
00040                      wc_printf("<td><img src=\"static/webcit_icons/openid-small.gif\"></td><td>");
00041                      escputs(buf);
00042                      wc_printf("</td><td>");
00043                      wc_printf("<a href=\"openid_detach?id_to_detach=");
00044                      urlescputs(buf);
00045                      wc_printf("\" onClick=\"return confirm('%s');\">",
00046                             _("Do you really want to delete this OpenID?"));
00047                      wc_printf("%s</a>", _("(delete)"));
00048                      wc_printf("</td></tr>\n");
00049               }
00050        
00051               wc_printf("</table><br>\n");
00052        
00053                wc_printf("<form method=\"POST\" action=\"openid_attach\">\n");
00054               wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WCC->nonce);
00055               wc_printf(_("Add an OpenID: "));
00056                wc_printf("<input type=\"text\" name=\"openid_url\" class=\"openid_urlarea\" size=\"40\">\n");
00057                wc_printf("<input type=\"submit\" name=\"attach_button\" value=\"%s\">"
00058                      "</form></center>\n", _("Attach"));
00059        }
00060 
00061        else {
00062               wc_printf(_("%s does not permit authentication via OpenID."), ChrPtr(WCC->serv_info->serv_humannode));
00063        }
00064 
00065        do_template("box_end");
00066        wDumpContent(2);
00067 }
00068 
00069 
00070 /*
00071  * Attempt to attach an OpenID to an existing, logged-in account
00072  */
00073 void openid_attach(void) {
00074        char buf[4096];
00075 
00076        if (havebstr("attach_button")) {
00077 
00078               syslog(LOG_DEBUG, "Attempting to attach %s\n", bstr("openid_url"));
00079 
00080               snprintf(buf, sizeof buf,
00081                      "OIDS %s|%s/finalize_openid_login?attach_existing=1|%s",
00082                      bstr("openid_url"),
00083                      ChrPtr(site_prefix),
00084                      ChrPtr(site_prefix)
00085               );
00086 
00087               serv_puts(buf);
00088               serv_getln(buf, sizeof buf);
00089               if (buf[0] == '2') {
00090                      syslog(LOG_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
00091                      http_redirect(&buf[4]);
00092                      return;
00093               }
00094               else {
00095                      syslog(LOG_DEBUG, "OpenID attach failed: %s\n", &buf[4]);
00096               }
00097        }
00098 
00099        /* If we get to this point then something failed. */
00100        display_openids();
00101 }
00102 
00103 
00104 /*
00105  * Detach an OpenID from the currently logged-in account
00106  */
00107 void openid_detach(void) {
00108        StrBuf *Line;
00109 
00110        if (havebstr("id_to_detach")) {
00111               serv_printf("OIDD %s", bstr("id_to_detach"));
00112               Line = NewStrBuf();
00113               StrBuf_ServGetln(Line);
00114               GetServerStatusMsg(Line, NULL, 1, 2);
00115               FreeStrBuf(&Line);
00116        }
00117 
00118        display_openids();
00119 }
00120 
00121 void 
00122 InitModule_OPENID
00123 (void)
00124 {
00125        WebcitAddUrlHandler(HKEY("display_openids"), "", 0, display_openids, 0);
00126        WebcitAddUrlHandler(HKEY("openid_attach"), "", 0, openid_attach, 0);
00127        WebcitAddUrlHandler(HKEY("openid_detach"), "", 0, openid_detach, 0);
00128 }