]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/openid/serv_openid_rp.c
Began laying the framework for creating new users with OpenID
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
index 01ee711f74690d88ce0b0c14ec011868e4a4ce6b..d0515618a8b89b036bc1e8f4040d1c1e3f276eb0 100644 (file)
@@ -56,7 +56,7 @@ struct ctdl_openid {
  * The structure of an openid record *key* is:
  *
  * |--------------claimed_id-------------|
- *     (auctual length of claimed id)
+ *     (actual length of claimed id)
  *
  *
  * The structure of an openid record *value* is:
@@ -67,6 +67,7 @@ struct ctdl_openid {
  */
 
 
+
 /*
  * Attach an OpenID to a Citadel account
  */
@@ -119,7 +120,40 @@ int attach_openid(struct ctdluser *who, char *claimed_id)
  * When a user is being deleted, we have to delete any OpenID associations
  */
 void openid_purge(struct ctdluser *usbuf) {
-       /* FIXME finish this */
+       struct cdbdata *cdboi;
+       HashList *keys = NULL;
+       HashPos *HashPos;
+       char *deleteme = NULL;
+       long len;
+       void *Value;
+       char *Key;
+
+       keys = NewHash(1, NULL);
+       if (!keys) return;
+
+
+       cdb_rewind(CDB_OPENID);
+       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+               if (cdboi->len > sizeof(long)) {
+                       if (((long)*(cdboi->ptr)) == usbuf->usernum) {
+                               deleteme = strdup(cdboi->ptr + sizeof(long)),
+                               Put(keys, deleteme, strlen(deleteme), deleteme, generic_free_handler);
+                       }
+               }
+               cdb_free(cdboi);
+       }
+
+       /* Go through the hash list, deleting keys we stored in it */
+
+       HashPos = GetNewHashPos();
+       while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
+       {
+               CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n", Value);
+               cdb_delete(CDB_OPENID, Value, strlen(Value));
+               /* note: don't free(Value) -- deleting the hash list will handle this for us */
+       }
+       DeleteHashPos(&HashPos);
+       DeleteHash(&keys);
 }
 
 
@@ -136,14 +170,53 @@ void cmd_oidl(char *argbuf) {
 
        while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
                if (cdboi->len > sizeof(long)) {
-               cprintf("%s\n", cdboi->ptr + sizeof(long));
+                       if (((long)*(cdboi->ptr)) == CC->user.usernum) {
+                               cprintf("%s\n", cdboi->ptr + sizeof(long));
+                       }
                }
+               cdb_free(cdboi);
        }
        cprintf("000\n");
 }
 
 
 
+/*
+ * Detach an OpenID from the currently logged in account
+ */
+void cmd_oidd(char *argbuf) {
+       struct cdbdata *cdboi;
+       char id_to_detach[1024];
+       int this_is_mine = 0;
+
+       if (CtdlAccessCheck(ac_logged_in)) return;
+       extract_token(id_to_detach, argbuf, 0, '|', sizeof id_to_detach);
+       if (IsEmptyStr(id_to_detach)) {
+               cprintf("%d An empty OpenID URL is not allowed.\n", ERROR + ILLEGAL_VALUE);
+       }
+
+       cdb_rewind(CDB_OPENID);
+       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+               if (cdboi->len > sizeof(long)) {
+                       if (((long)*(cdboi->ptr)) == CC->user.usernum) {
+                               this_is_mine = 1;
+                       }
+               }
+               cdb_free(cdboi);
+       }
+
+       if (!this_is_mine) {
+               cprintf("%d That OpenID was not found or not associated with your account.\n",
+                       ERROR + ILLEGAL_VALUE);
+               return;
+       }
+
+       cdb_delete(CDB_OPENID, id_to_detach, strlen(id_to_detach));
+       cprintf("%d %s detached from your account.\n", CIT_OK, id_to_detach);
+}
+
+
+
 /*
  * 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.
@@ -167,6 +240,17 @@ int getuserbyopenid(struct ctdluser *usbuf, char *claimed_id)
 
 
 
+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);
+}
+
+
 /**************************************************************************/
 /*                                                                        */
 /* Functions in this section handle OpenID protocol                       */
@@ -418,12 +502,6 @@ void cmd_oids(char *argbuf) {
 
 
 
-/*
- * Callback function to free a pointer (used below in the hash list)
- */
-void free_oid_key(void *ptr) {
-       free(ptr);
-}
 
 
 /*
@@ -434,7 +512,6 @@ void cmd_oidf(char *argbuf) {
        char thiskey[1024];
        char thisdata[1024];
        HashList *keys = NULL;
-       HashPos *HashPos;
        struct ctdl_openid *oiddata = (struct ctdl_openid *) CC->openid_data;
 
        keys = NewHash(1, NULL);
@@ -449,7 +526,7 @@ void cmd_oidf(char *argbuf) {
                extract_token(thiskey, buf, 0, '|', sizeof thiskey);
                extract_token(thisdata, buf, 1, '|', sizeof thisdata);
                CtdlLogPrintf(CTDL_DEBUG, "%s: [%d] %s\n", thiskey, strlen(thisdata), thisdata);
-               Put(keys, thiskey, strlen(thiskey), strdup(thisdata), free_oid_key);
+               Put(keys, thiskey, strlen(thiskey), strdup(thisdata), generic_free_handler);
        }
 
 
@@ -585,8 +662,17 @@ void cmd_oidf(char *argbuf) {
                                cprintf("authenticate\n%s\n%s\n", usbuf.fullname, usbuf.password);
                        }
 
+                       /*
+                        * 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);
+                       }
+
+                       /* FIXME right here we have to handle manual account creation */
+
                        else {
-                               cprintf("fail\n");              // FIXME do the login here!!
+                               cprintf("fail\n");
                        }
                }
        }
@@ -595,17 +681,21 @@ void cmd_oidf(char *argbuf) {
        }
        cprintf("000\n");
 
-       /* Free the hash list */
+       /*
+        * We will eventually do something with the data in the hash list.
+        *
        long len;
        void *Value;
        char *Key;
-
+       HashPos *HashPos;
        HashPos = GetNewHashPos();
        while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
        {
-               free(Value);
        }
        DeleteHashPos(&HashPos);
+        */
+
+       DeleteHash(&keys);              /* This will free() all the key data for us */
 }
 
 
@@ -650,6 +740,7 @@ CTDL_MODULE_INIT(openid_rp)
                CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
                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);
                CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
        }