LP#1468422 open-ils.auth API changes, in progress
authorBill Erickson <berickxx@gmail.com>
Wed, 22 Jul 2015 22:02:19 +0000 (18:02 -0400)
committerBill Erickson <berickxx@gmail.com>
Mon, 23 Nov 2015 16:17:05 +0000 (11:17 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/c-apps/oils_auth.c

index bd79770..a0d52de 100644 (file)
@@ -144,6 +144,72 @@ int osrfAppChildInit() {
        return 0;
 }
 
+// free() response
+static char* oilsAuthGetSalt(int userId) {
+    char* saltString = NULL;
+
+    jsonObject* params = jsonParseFmt(
+        "{\"from\":[\"actor.get_salt\",%d,\"%s\"]}", userId, "main");
+
+    jsonObject* saltObj = oilsUtilsQuickReq(
+        "open-ils.cstore", "open-ils.cstore.json_query", params);
+
+    jsonObjectFree(params);
+
+    if (saltObj) {
+
+        if (saltObj->type != JSON_NULL) {
+
+            const char* saltValue = jsonObjectGetString(
+                jsonObjectGetKeyConst(saltObj, "get_salt"));
+    
+            // caller expects a free-able string
+            if (saltValue) { saltString = strdup(saltValue); } 
+        }
+
+        jsonObjectFree(saltObj);
+    }
+
+    return saltString;
+}
+
+// ident is either a username or barcode
+// Returns the init seed -> requires free();
+static char* oilsAuthBuildInitCache(
+    int userId, const char* ident, const char* ident_type, const char* nonce) {
+
+    char* cachekey  = va_list_to_string(
+        "%s%s%s", OILS_AUTH_CACHE_PRFX, ident, nonce);
+
+    char* countkey = va_list_to_string(
+        "%s%s%s", OILS_AUTH_CACHE_PRFX, ident, OILS_AUTH_COUNT_SFFX);
+
+    char* seed = oilsAuthGetSalt(userId);
+
+    jsonObject* seedobject = jsonParseFmt(
+        "{\"%s\":\"%s\",\"user_id\":%d,\"seed\":\"%s\"}",
+        ident_type, ident, userId, seed);
+
+    jsonObject* countobject = osrfCacheGetObject(countkey);
+    if(!countobject) {
+        countobject = jsonNewNumberObject((double) 0);
+    }
+
+    osrfCachePutObject(cachekey, seedobject, _oilsAuthSeedTimeout);
+    osrfCachePutObject(countkey, countobject, _oilsAuthBlockTimeout);
+
+    osrfLogDebug(OSRF_LOG_MARK, 
+        "oilsAuthInit(): has seed %s and key %s", seed, cachekey);
+
+    free(cachekey);
+    free(countkey);
+    jsonObjectFree(countobject);
+    jsonObjectFree(seedobject);
+
+    return seed;
+}
+
+
 /**
        @brief Implement the "init" method.
        @param ctx The method context.
@@ -218,6 +284,39 @@ int oilsAuthInit( osrfMethodContext* ctx ) {
        return -1;  // Error: no username parameter
 }
 
+int oilsAuthInitUsername(osrfMethodContext* ctx) {
+    OSRF_METHOD_VERIFY_CONTEXT(ctx);
+
+    char* username = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0));
+    const char* nonce = jsonObjectGetString(jsonObjectGetIndex(ctx->params, 1));
+    if (!nonce) nonce = "";
+    if (!username) return -1;
+
+    jsonObject* resp = NULL;
+    jsonObject* userObj = NULL;
+
+    userObj = oilsUtilsFetchUserByUsername(username);
+
+    if (userObj && JSON_NULL == userObj->type) { // user not found
+        jsonObjectFree(userObj);
+        userObj = NULL; 
+        resp = jsonNewObject("x");
+
+    } else {
+        char* seed = oilsAuthBuildInitCache(
+            oilsFMGetObjectId(userObj), username, "username", nonce);
+        resp = jsonNewObject(seed);
+        free(seed);
+    }
+
+    osrfAppRespondComplete(ctx, resp);
+    jsonObjectFree(resp);
+    free(username);
+    return 0;
+}
+
+
+
 /**
        Verifies that the user has permission to login with the
        given type.  If the permission fails, an oilsEvent is returned