]> code.citadel.org Git - citadel.git/blobdiff - webcit/auth.c
Success! We can now log in an existing user with OpenID.
[citadel.git] / webcit / auth.c
index cee4bbf508eec6bda13bb950bf38c8d47d8d8376..96503c1a7f33c5608694336a567d06b8dc5bb701 100644 (file)
@@ -78,7 +78,7 @@ void display_login(char *mesg)
                svput("NEWUSER_BUTTON_POST", WCS_STRING, "");
        }
 
-#ifdef TECH_PREVIEW
+       if (1) {        // FIXME we have to check whether the server offers openid
                svprintf(HKEY("OFFER_OPENID_LOGIN"), WCS_STRING,
                        "<div align=center>"
                        "<a href=\"display_openid_login\">"
@@ -88,9 +88,10 @@ void display_login(char *mesg)
                        ,
                        "Log in using OpenID"
                );
-#else
+       }
+       else {
                svput("OFFER_OPENID_LOGIN", WCS_STRING, "");
-#endif
+       }
 
        do_template("login");
 
@@ -249,6 +250,8 @@ void do_login(void)
        if (WC->logged_in) {
                if (WC->need_regi) {
                        display_reg(1);
+               } else if (WC->need_vali) {
+                       validate();
                } else {
                        do_welcome();
                }
@@ -259,88 +262,12 @@ void do_login(void)
 }
 
 
-/* 
- * Locate a <link> tag and, given its 'rel=' parameter, return its 'href' parameter
- */
-void extract_link(char *target_buf, int target_size, char *rel, char *source_buf)
-{
-       char *ptr = source_buf;
-
-       if (!target_buf) return;
-       if (!rel) return;
-       if (!source_buf) return;
-
-       target_buf[0] = 0;
-
-       while (ptr = bmstrcasestr(ptr, "<link"), ptr != NULL) {
-
-               char work_buffer[1024];
-               char *link_tag_start = NULL;
-               char *link_tag_end = NULL;
-
-               char rel_tag[1024];
-               char href_tag[1024];
-
-               link_tag_start = ptr;
-               link_tag_end = strchr(ptr, '>');
-               rel_tag[0] = 0;
-               href_tag[0] = 0;
-
-               if ((link_tag_end) && (link_tag_end > link_tag_start)) {
-                       int len;
-                       len = link_tag_end - link_tag_start;
-                       if (len > sizeof work_buffer) len = sizeof work_buffer;
-                       memcpy(work_buffer, link_tag_start, len);
-               
-                       char *rel_start = NULL;
-                       char *rel_end = NULL;
-                       rel_start = bmstrcasestr(work_buffer, "rel=");
-                       if (rel_start) {
-                               rel_start = strchr(rel_start, '\"');
-                               if (rel_start) {
-                                       ++rel_start;
-                                       rel_end = strchr(rel_start, '\"');
-                                       if ((rel_end) && (rel_end > rel_start)) {
-                                               safestrncpy(rel_tag, rel_start, rel_end - rel_start + 1);
-                                       }
-                               }
-                       }
-
-                       char *href_start = NULL;
-                       char *href_end = NULL;
-                       href_start = bmstrcasestr(work_buffer, "href=");
-                       if (href_start) {
-                               href_start = strchr(href_start, '\"');
-                               if (href_start) {
-                                       ++href_start;
-                                       href_end = strchr(href_start, '\"');
-                                       if ((href_end) && (href_end > href_start)) {
-                                               safestrncpy(href_tag, href_start, href_end - href_start + 1);
-                                       }
-                               }
-                       }
-
-                       if (!strcasecmp(rel, rel_tag)) {
-                               safestrncpy(target_buf, href_tag, target_size);
-                               return;
-                       }
-
-               }
-
-       ++ptr;
-       }
-
-
-}
-
-
 /* 
  * Perform authentication using OpenID
- * assemble the checkid_immediate request and then redirect to the user's identity provider
+ * assemble the checkid_setup request and then redirect to the user's identity provider
  */
 void do_openid_login(void)
 {
-       int i;
        char buf[4096];
 
        if (havebstr("language")) {
@@ -353,47 +280,22 @@ void do_openid_login(void)
                return;
        }
        if (havebstr("login_action")) {
-               i = fetch_http(bstr("openid_url"), buf, sizeof buf - 1);
-               buf[sizeof buf - 1] = 0;
-               if (i > 0) {
-                       char openid_server[1024];
-                       char openid_delegate[1024];
-                       
-                       extract_link(openid_server, sizeof openid_server, "openid.server", buf);
-                       extract_link(openid_delegate, sizeof openid_delegate, "openid.delegate", buf);
-
-                       /* Empty delegate is legal; we just use the openid_url instead */
-                       if (IsEmptyStr(openid_delegate)) {
-                               safestrncpy(openid_delegate, bstr("openid_url"), sizeof openid_delegate);
-                       }
+               snprintf(buf, sizeof buf,
+                       "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
+                       bstr("openid_url"),
+                       (is_https ? "https" : "http"), WC->http_host,
+                       (is_https ? "https" : "http"), WC->http_host
+               );
 
-                       /* Now we know where to redirect to. */
-
-                       char redirect_string[4096];
-                       char escaped_identity[1024];
-                       char escaped_return_to[1024];
-                       char escaped_trust_root[1024];
-
-                       stresc(escaped_identity, sizeof escaped_identity, openid_delegate, 0, 1);
-
-                       snprintf(buf, sizeof buf, "%s://%s/finish_openid_login",
-                               (is_https ? "https" : "http"), WC->http_host);
-                       stresc(escaped_return_to, sizeof escaped_identity, buf, 0, 1);
-
-                       snprintf(buf, sizeof buf, "%s://%s",
-                               (is_https ? "https" : "http"), WC->http_host);
-                       stresc(escaped_trust_root, sizeof escaped_identity, buf, 0, 1);
-
-                       snprintf(redirect_string, sizeof redirect_string,
-                               "%s"
-                               "?openid.mode=checkid_immediate"
-                               "&openid_identity=%s"
-                               "&openid.return_to=%s"
-                               "&openid.trust_root=%s"
-                               ,
-                               openid_server, escaped_identity, escaped_return_to, escaped_trust_root
-                       );
-                       http_redirect(redirect_string);
+               serv_puts(buf);
+               serv_getln(buf, sizeof buf);
+               if (buf[0] == '2') {
+                       lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
+                       http_redirect(&buf[4]);
+                       return;
+               }
+               else {
+                       display_openid_login(&buf[4]);
                        return;
                }
        }
@@ -403,26 +305,82 @@ void do_openid_login(void)
 }
 
 /* 
- * Perform authentication using OpenID
- * assemble the checkid_immediate request and then redirect to the user's identity provider
+ * Complete the authentication using OpenID
+ * This function handles the positive or negative assertion from the user's Identity Provider
  */
-void finish_openid_login(void)
+void finalize_openid_login(void)
 {
+       char buf[1024];
+       struct wcsession *WCC = WC;
+       int already_logged_in = (WCC->logged_in) ;
+       int linecount = 0;
+       char result[128] = "";
+       char username[128] = "";
+       char password[128] = "";
+
        if (havebstr("openid.mode")) {
-               if (!strcasecmp(bstr("openid.mode"), "error")) {
-                       if (havebstr("openid.error")) {
-                               display_openid_login(bstr("openid.error"));
-                       }
-                       else {
-                               display_openid_login(_("Your password was not accepted."));
+               if (!strcasecmp(bstr("openid.mode"), "id_res")) {
+
+                       // FIXME id accepted but the code isn't finished
+                       serv_puts("OIDF");
+                       serv_getln(buf, sizeof buf);
+
+                       if (buf[0] == '8') {
+                               urlcontent *u;
+                               void *U;
+                               long HKLen;
+                               char *HKey;
+                               HashPos *Cursor;
+                               
+                               Cursor = GetNewHashPos ();
+                               while (GetNextHashPos(WCC->urlstrings, Cursor, &HKLen, &HKey, &U)) {
+                                       u = (urlcontent*) U;
+                                       if (!strncasecmp(u->url_key, "openid.", 7)) {
+                                               serv_printf("%s|%s", &u->url_key[7], u->url_data);
+                                       }
+                               }
+
+                               serv_puts("000");
+
+                               linecount = 0;
+                               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                                       if (linecount == 0) safestrncpy(result, buf, sizeof result);
+                                       if (!strcasecmp(result, "authenticate")) {
+                                               if (linecount == 1) {
+                                                       safestrncpy(username, buf, sizeof username);
+                                               }
+                                               else if (linecount == 2) {
+                                                       safestrncpy(password, buf, sizeof password);
+                                               }
+                                       }
+                                       ++linecount;
+                               }
                        }
-                       return;
                }
        }
 
+       /* If we were already logged in, this was an attempt to associate an OpenID account */
+       if (already_logged_in) {
+               display_openids();
+               return;
+       }
 
-       // FIXME finish this
+       /* Was the claimed ID associated with an existing account?  Then log in that account now. */
+       if (!strcasecmp(result, "authenticate")) {
+               serv_printf("USER %s", username);
+               serv_getln(buf, sizeof buf);
+               if (buf[0] == '3') {
+                       serv_printf("PASS %s", password);
+                       serv_getln(buf, sizeof buf);
+                       if (buf[0] == '2') {
+                               become_logged_in(username, password, buf);
+                       }
+               }
+       }
+
+       /* FIXME -- right here we have to put the code to log in a new user */
 
+       /* Did we manage to log in?  If so, continue with the normal flow... */
        if (WC->logged_in) {
                if (WC->need_regi) {
                        display_reg(1);
@@ -436,16 +394,10 @@ void finish_openid_login(void)
 }
 
 
-
-
-
-
-
-
 /*
- * display the user a welcome screen.
+ * Display a welcome screen to the user.
  *
- * if this is the first time login, and the web based setup is enabled, 
+ * If this is the first time login, and the web based setup is enabled, 
  * lead the user through the setup routines
  */
 void do_welcome(void)