]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/openid/serv_openid_rp.c
Added a 'verify_only' result to OIDF, indicating the account
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
index d0515618a8b89b036bc1e8f4040d1c1e3f276eb0..0729c62de02722893a3a8e2be2c5d8add4096685 100644 (file)
@@ -38,7 +38,7 @@
 struct ctdl_openid {
        char claimed_id[1024];
        char server[1024];
-       int validated;
+       int verified;
 };
 
 
@@ -110,12 +110,13 @@ int attach_openid(struct ctdluser *who, char *claimed_id)
        cdb_store(CDB_OPENID, claimed_id, strlen(claimed_id), data, data_len);
        free(data);
 
-       CtdlLogPrintf(CTDL_INFO, "%s has been associated with %s (%ld)\n",
+       CtdlLogPrintf(CTDL_INFO, "%s has been attached to %s (%ld)\n",
                claimed_id, who->fullname, who->usernum);
        return(0);
 }
 
 
+
 /*
  * When a user is being deleted, we have to delete any OpenID associations
  */
@@ -218,11 +219,51 @@ void cmd_oidd(char *argbuf) {
 
 
 /*
- * getuserbyopenid() works the same way as getuser() and getuserbynumber().
- * If a user account exists which is associated with the Claimed ID, it fills usbuf and returns zero.
+ * Attempt to auto-create a new Citadel account using the nickname from Simple Registration Extension
+ */
+int openid_create_user_via_sri(char *claimed_id, HashList *sri_keys)
+{
+       char *desired_name = NULL;
+       char new_password[32];
+
+       if (config.c_auth_mode != AUTHMODE_NATIVE) return(1);
+       if (config.c_disable_newu) return(2);
+       if (CC->logged_in) return(3);
+       if (!GetHash(sri_keys, "sreg.nickname", 13, (void *) &desired_name)) return(4);
+
+       CtdlLogPrintf(CTDL_DEBUG, "The desired account name is <%s>\n", desired_name);
+
+       if (!getuser(&CC->user, desired_name)) {
+               CtdlLogPrintf(CTDL_DEBUG, "<%s> is already taken by another user.\n", desired_name);
+               memset(&CC->user, 0, sizeof(struct ctdluser));
+               return(5);
+       }
+
+       /* The desired account name is available.  Create the account and log it in! */
+       if (create_user(desired_name, 1)) return(6);
+
+       snprintf(new_password, sizeof new_password, "%08lx%08lx", random(), random());
+       CtdlSetPassword(new_password);
+       attach_openid(&CC->user, claimed_id);
+       return(0);
+}
+
+// FIXME we still have to set up the vCard
+
+// identity = [50]  http://uncensored.citadel.org/~ajc/MyID.config.php
+// sreg.nickname = [17]  IGnatius T Foobar
+// sreg.email = [26]  ajc@uncensored.citadel.org
+// sreg.fullname = [10]  Art Cancro
+// sreg.postcode = [5]  10549
+// sreg.country = [2]  US
+
+
+
+/*
+ * If a user account exists which is associated with the Claimed ID, log it in and return zero.
  * Otherwise it returns nonzero.
  */
-int getuserbyopenid(struct ctdluser *usbuf, char *claimed_id)
+int login_via_openid(char *claimed_id)
 {
        struct cdbdata *cdboi;
        long usernum = 0;
@@ -235,21 +276,20 @@ int getuserbyopenid(struct ctdluser *usbuf, char *claimed_id)
        memcpy(&usernum, cdboi->ptr, sizeof(long));
        cdb_free(cdboi);
 
-       return(getuserbynumber(usbuf, usernum));
+       if (!getuserbynumber(&CC->user, usernum)) {
+               /* Now become the user we just created */
+               safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
+               do_login();
+               return(0);
+       }
+       else {
+               memset(&CC->user, 0, sizeof(struct ctdluser));
+               return(-1);
+       }
 }
 
 
 
-int openid_create_user_via_sri(struct ctdluser *usbuf, char *claimed_id, HashList *sri_keys)
-{
-       if (config.c_auth_mode != AUTHMODE_NATIVE) return(1);
-       if (config.c_disable_newu) return(2);
-
-       /* FIXME do something */
-
-       return(99);
-}
-
 
 /**************************************************************************/
 /*                                                                        */
@@ -445,7 +485,7 @@ void cmd_oids(char *argbuf) {
        extract_token(oiddata->claimed_id, argbuf, 0, '|', sizeof oiddata->claimed_id);
        extract_token(return_to, argbuf, 1, '|', sizeof return_to);
        extract_token(trust_root, argbuf, 2, '|', sizeof trust_root);
-       oiddata->validated = 0;
+       oiddata->verified = 0;
 
        i = fetch_http(oiddata->claimed_id, buf, sizeof buf - 1, sizeof oiddata->claimed_id);
        CtdlLogPrintf(CTDL_DEBUG, "Normalized URL and Claimed ID is: %s\n", oiddata->claimed_id);
@@ -627,14 +667,14 @@ void cmd_oidf(char *argbuf) {
        valbuf[fh.total_bytes_received] = 0;
 
        if (bmstrcasestr(valbuf, "is_valid:true")) {
-               oiddata->validated = 1;
+               oiddata->verified = 1;
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Authentication %s.\n", (oiddata->validated ? "succeeded" : "failed") );
+       CtdlLogPrintf(CTDL_DEBUG, "Authentication %s.\n", (oiddata->verified ? "succeeded" : "failed") );
 
        /* Respond to the client */
 
-       if (oiddata->validated) {
+       if (oiddata->verified) {
 
                /* If we were already logged in, attach the OpenID to the user's account */
                if (CC->logged_in) {
@@ -646,10 +686,8 @@ void cmd_oidf(char *argbuf) {
                        }
                }
 
-               /* Otherwise, a user is attempting to log in using the validated OpenID */      
+               /* Otherwise, a user is attempting to log in using the verified OpenID */       
                else {
-                       struct ctdluser usbuf;
-
                        /*
                         * Existing user who has claimed this OpenID?
                         *
@@ -658,21 +696,33 @@ void cmd_oidf(char *argbuf) {
                         * is associated with the account, they already have password equivalency and can
                         * login, so they could just as easily change the password, etc.
                         */
-                       if (getuserbyopenid(&usbuf, oiddata->claimed_id) == 0) {
-                               cprintf("authenticate\n%s\n%s\n", usbuf.fullname, usbuf.password);
+                       if (login_via_openid(oiddata->claimed_id) == 0) {
+                               cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
+                               logged_in_response();
                        }
 
                        /*
                         * New user whose OpenID is verified and Simple Registration Extension is in use?
                         */
-                       else if (openid_create_user_via_sri(&usbuf, oiddata->claimed_id, keys) == 0) {
-                               cprintf("authenticate\n%s\n%s\n", usbuf.fullname, usbuf.password);
+                       else if (openid_create_user_via_sri(oiddata->claimed_id, keys) == 0) {
+                               cprintf("authenticate\n%s\n%s\n", CC->user.fullname, CC->user.password);
+                               logged_in_response();
                        }
 
-                       /* FIXME right here we have to handle manual account creation */
-
+                       /*
+                        * OpenID is verified, but the desired username either was not specified or
+                        * conflicts with an existing user.  Manual account creation is required.
+                        */
                        else {
-                               cprintf("fail\n");
+                               char *desired_name = NULL;
+                               cprintf("verify_only\n");
+                               cprintf("%s\n", oiddata->claimed_id);
+                               if (GetHash(keys, "sreg.nickname", 13, (void *) &desired_name)) {
+                                       cprintf("%s\n", desired_name);
+                               }
+                               else {
+                                       cprintf("\n");
+                               }
                        }
                }
        }
@@ -699,20 +749,6 @@ void cmd_oidf(char *argbuf) {
 }
 
 
-// mode = [6]  id_res
-// identity = [50]  http://uncensored.citadel.org/~ajc/MyID.config.php
-// assoc_handle = [26]  6ekac3ju181tgepk7v4h9r7ui7
-// return_to = [42]  http://jemcaterers.net/finish_openid_login
-// sreg.nickname = [17]  IGnatius T Foobar
-// sreg.email = [26]  ajc@uncensored.citadel.org
-// sreg.fullname = [10]  Art Cancro
-// sreg.postcode = [5]  10549
-// sreg.country = [2]  US
-// signed = [102]  mode,identity,assoc_handle,return_to,sreg.nickname,sreg.email,sreg.fullname,sreg.postcode,sreg.country
-// sig = [28]  vixxxU4MAqWfxxxxCfrHv3TxxxhEw=
-
-
-
 
 /**************************************************************************/
 /*                                                                        */
@@ -727,6 +763,7 @@ void cmd_oidf(char *argbuf) {
 void openid_cleanup_function(void) {
 
        if (CC->openid_data != NULL) {
+               CtdlLogPrintf(CTDL_DEBUG, "Clearing OpenID session state\n");
                free(CC->openid_data);
        }
 }
@@ -741,7 +778,7 @@ CTDL_MODULE_INIT(openid_rp)
                CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
                CtdlRegisterProtoHook(cmd_oidl, "OIDL", "List OpenIDs associated with an account");
                CtdlRegisterProtoHook(cmd_oidd, "OIDD", "Detach an OpenID from an account");
-               CtdlRegisterSessionHook(openid_cleanup_function, EVT_STOP);
+               CtdlRegisterSessionHook(openid_cleanup_function, EVT_LOGOUT);
                CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
        }