]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/openid/serv_openid_rp.c
more openid work
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
index 0657f5acfb39d9860ff51c80bbd24fa5f791a9db..1c98d5372770f6ef8f12669d2305cf5ec1ad8b8a 100644 (file)
 #include <curl/curl.h>
 #include "ctdl_module.h"
 #include "config.h"
+#include "citserver.h"
 
 struct ctdl_openid {
        char claimed_id[1024];
        char server[1024];
+       int validated;
 };
 
 
@@ -50,21 +52,65 @@ struct ctdl_openid {
 
 
 /*
- * Attach or detach an OpenID to a Citadel account
+ * The structure of an openid record *key* is:
+ *
+ * |--------------claimed_id-------------|
+ *     (auctual length of claimed id)
+ *
+ *
+ * The structure of an openid record *value* is:
+ *
+ * |-----user_number----|------------claimed_id---------------|
+ *    (sizeof long)          (actual length of claimed id)
+ *
  */
 
-enum {
-       moa_detach,
-       moa_attach
-};
 
-int modify_openid_associations(struct ctdluser *who, char *claimed_id, int operation)
+/*
+ * Attach an OpenID to a Citadel account
+ */
+int attach_openid(struct ctdluser *who, char *claimed_id)
 {
+       struct cdbdata *cdboi;
+       long fetched_usernum;
+       char *data;
+       int data_len;
+
        if (!who) return(1);
        if (!claimed_id) return(1);
        if (IsEmptyStr(claimed_id)) return(1);
 
-       return(2);              // error because we are not done yet FIXME
+       /* Check to see if this OpenID is already in the database */
+
+       cdboi = cdb_fetch(CDB_OPENID, claimed_id, strlen(claimed_id));
+       if (cdboi != NULL) {
+               memcpy(&fetched_usernum, cdboi->ptr, sizeof(long));
+               cdb_free(cdboi);
+
+               if (fetched_usernum == who->usernum) {
+                       CtdlLogPrintf(CTDL_INFO, "%s already associated; no action is taken\n", claimed_id);
+                       return(0);
+               }
+               else {
+                       CtdlLogPrintf(CTDL_INFO, "%s already belongs to another user\n", claimed_id);
+                       return(3);
+               }
+       }
+
+       /* Not already in the database, so attach it now */
+
+       data_len = sizeof(long) + strlen(claimed_id) + 1;
+       data = malloc(data_len);
+
+       memcpy(data, &who->usernum, sizeof(long));
+       memcpy(&data[sizeof(long)], claimed_id, strlen(claimed_id) + 1);
+
+       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",
+               claimed_id, who->fullname, who->usernum);
+       return(0);
 }
 
 
@@ -77,6 +123,23 @@ void openid_purge(struct ctdluser *usbuf) {
 
 
 
+/*
+ * List the OpenIDs associated with the currently logged in account
+ */
+void cmd_oidl(char *argbuf) {
+       struct cdbdata *cdboi;
+
+       if (CtdlAccessCheck(ac_logged_in)) return;
+       cdb_rewind(CDB_OPENID);
+       cprintf("%d Associated OpenIDs:\n", LISTING_FOLLOWS);
+
+       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+               if (cdboi->len > sizeof(long)) {
+               cprintf("%s\n", cdboi->ptr + sizeof(long));
+               }
+       }
+       cprintf("000\n");
+}
 
 
 
@@ -275,6 +338,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;
 
        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);
@@ -461,21 +525,20 @@ void cmd_oidf(char *argbuf) {
        curl_formfree(formpost);
 
        valbuf[fh.total_bytes_received] = 0;
-       int success = 0;
 
        if (bmstrcasestr(valbuf, "is_valid:true")) {
-               success = 1;
+               oiddata->validated = 1;
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Authentication %s.\n", (success ? "succeeded" : "failed") );
+       CtdlLogPrintf(CTDL_DEBUG, "Authentication %s.\n", (oiddata->validated ? "succeeded" : "failed") );
 
        /* Respond to the client */
 
-       if (success) {
+       if (oiddata->validated) {
 
                /* If we were already logged in, attach the OpenID to the user's account */
                if (CC->logged_in) {
-                       if (modify_openid_associations(&CC->user, oiddata->claimed_id, moa_attach) == 0) {
+                       if (attach_openid(&CC->user, oiddata->claimed_id) == 0) {
                                cprintf("attach\n");
                        }
                        else {
@@ -548,6 +611,7 @@ CTDL_MODULE_INIT(openid_rp)
                curl_global_init(CURL_GLOBAL_ALL);
                CtdlRegisterProtoHook(cmd_oids, "OIDS", "Setup OpenID authentication");
                CtdlRegisterProtoHook(cmd_oidf, "OIDF", "Finalize OpenID authentication");
+               CtdlRegisterProtoHook(cmd_oidl, "OIDL", "List OpenIDs associated with an account");
                CtdlRegisterSessionHook(openid_cleanup_function, EVT_STOP);
                CtdlRegisterUserHook(openid_purge, EVT_PURGEUSER);
        }
@@ -555,3 +619,6 @@ CTDL_MODULE_INIT(openid_rp)
        /* return our Subversion id for the Log */
        return "$Id$";
 }
+
+
+/* FIXME ... we have to add the new openid database to serv_vandelay.c */