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.
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