From: Bill Erickson Date: Wed, 22 Jul 2015 22:02:19 +0000 (-0400) Subject: LP#1468422 open-ils.auth API changes, in progress X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=347f750cccdaa1a19aca9c169724a3118b94b753;p=working%2FEvergreen.git LP#1468422 open-ils.auth API changes, in progress Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/c-apps/oils_auth.c b/Open-ILS/src/c-apps/oils_auth.c index bd79770a2e..a0d52de605 100644 --- a/Open-ILS/src/c-apps/oils_auth.c +++ b/Open-ILS/src/c-apps/oils_auth.c @@ -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