1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "opensrf/osrf_settings.h"
4 #include "opensrf/osrf_json.h"
5 #include "opensrf/log.h"
6 #include "openils/oils_utils.h"
7 #include "openils/oils_constants.h"
8 #include "openils/oils_event.h"
10 #define OILS_AUTH_CACHE_PRFX "oils_auth_"
12 #define MODULENAME "open-ils.auth"
14 #define OILS_AUTH_OPAC "opac"
15 #define OILS_AUTH_STAFF "staff"
16 #define OILS_AUTH_TEMP "temp"
18 int osrfAppInitialize();
19 int osrfAppChildInit();
21 static int _oilsAuthOPACTimeout = 0;
22 static int _oilsAuthStaffTimeout = 0;
23 static int _oilsAuthOverrideTimeout = 0;
26 int osrfAppInitialize() {
28 osrfLogInfo(OSRF_LOG_MARK, "Initializing Auth Server...");
30 /* load and parse the IDL */
31 if (!oilsInitIDL(NULL)) return 1; /* return non-zero to indicate error */
33 osrfAppRegisterMethod(
35 "open-ils.auth.authenticate.init",
37 "Start the authentication process and returns the intermediate authentication seed"
38 " PARAMS( username )", 1, 0 );
40 osrfAppRegisterMethod(
42 "open-ils.auth.authenticate.complete",
44 "Completes the authentication process. Returns an object like so: "
45 "{authtoken : <token>, authtime:<time>}, where authtoken is the login "
46 "token and authtime is the number of seconds the session will be active"
47 "PARAMS(username, md5sum( seed + password ), type, org_id ) "
48 "type can be one of 'opac','staff', or 'temp' and it defaults to 'staff' "
49 "org_id is the location at which the login should be considered "
50 "active for login timeout purposes" , 1, 0 );
52 osrfAppRegisterMethod(
54 "open-ils.auth.session.retrieve",
55 "oilsAuthSessionRetrieve",
56 "Pass in the auth token and this retrieves the user object. The auth "
57 "timeout is reset when this call is made "
58 "Returns the user object (password blanked) for the given login session "
59 "PARAMS( authToken )", 1, 0 );
61 osrfAppRegisterMethod(
63 "open-ils.auth.session.delete",
64 "oilsAuthSessionDelete",
65 "Destroys the given login session "
66 "PARAMS( authToken )", 1, 0 );
68 osrfAppRegisterMethod(
70 "open-ils.auth.session.reset_timeout",
71 "oilsAuthResetTimeout",
72 "Resets the login timeout for the given session "
73 "Returns an ILS Event with payload = session_timeout of session "
74 "if found, otherwise returns the NO_SESSION event"
75 "PARAMS( authToken )", 1, 0 );
80 int osrfAppChildInit() {
84 int oilsAuthInit( osrfMethodContext* ctx ) {
85 OSRF_METHOD_VERIFY_CONTEXT(ctx);
89 char* username = NULL;
94 if( (username = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0))) ) {
96 if( strchr( username, ' ' ) ) {
98 /* spaces are not allowed */
99 resp = jsonNewObject("x"); /* 'x' will never be a valid seed */
100 osrfAppRespondComplete( ctx, resp );
104 seed = va_list_to_string( "%d.%ld.%s", time(NULL), (long) getpid(), username );
105 key = va_list_to_string( "%s%s", OILS_AUTH_CACHE_PRFX, username );
107 md5seed = md5sum(seed);
108 osrfCachePutString( key, md5seed, 30 );
110 osrfLogDebug( OSRF_LOG_MARK, "oilsAuthInit(): has seed %s and key %s", md5seed, key );
112 resp = jsonNewObject(md5seed);
113 osrfAppRespondComplete( ctx, resp );
120 jsonObjectFree(resp);
128 /** Verifies that the user has permission to login with the
129 * given type. If the permission fails, an oilsEvent is returned
131 * @return -1 if the permission check failed, 0 if ther permission
134 static int oilsAuthCheckLoginPerm(
135 osrfMethodContext* ctx, const jsonObject* userObj, const char* type ) {
137 if(!(userObj && type)) return -1;
138 oilsEvent* perm = NULL;
140 if(!strcasecmp(type, OILS_AUTH_OPAC)) {
141 char* permissions[] = { "OPAC_LOGIN" };
142 perm = oilsUtilsCheckPerms( oilsFMGetObjectId( userObj ), -1, permissions, 1 );
144 } else if(!strcasecmp(type, OILS_AUTH_STAFF)) {
145 char* permissions[] = { "STAFF_LOGIN" };
146 perm = oilsUtilsCheckPerms( oilsFMGetObjectId( userObj ), -1, permissions, 1 );
148 } else if(!strcasecmp(type, OILS_AUTH_TEMP)) {
149 char* permissions[] = { "STAFF_LOGIN" };
150 perm = oilsUtilsCheckPerms( oilsFMGetObjectId( userObj ), -1, permissions, 1 );
154 osrfAppRespondComplete( ctx, oilsEventToJSON(perm) );
163 * Returns 1 if the password provided matches the user's real password
164 * Returns 0 otherwise
165 * Returns -1 on error
167 static int oilsAuthVerifyPassword( const osrfMethodContext* ctx,
168 const jsonObject* userObj, const char* uname, const char* password ) {
171 char* realPassword = oilsFMGetString( userObj, "passwd" ); /**/
172 char* seed = osrfCacheGetString( "%s%s", OILS_AUTH_CACHE_PRFX, uname ); /**/
176 return osrfAppRequestRespondException( ctx->session,
177 ctx->request, "No authentication seed found. "
178 "open-ils.auth.authenticate.init must be called first");
181 osrfLogInternal(OSRF_LOG_MARK, "oilsAuth retrieved real password: [%s]", realPassword);
182 osrfLogDebug(OSRF_LOG_MARK, "oilsAuth retrieved seed from cache: %s", seed );
183 char* maskedPw = md5sum( "%s%s", seed, realPassword );
189 osrfLogDebug(OSRF_LOG_MARK, "oilsAuth generated masked password %s. "
190 "Testing against provided password %s", maskedPw, password );
192 if( !strcmp( maskedPw, password ) ) ret = 1;
202 * Calculates the login timeout
203 * 1. If orgloc is 1 or greater and has a timeout specified as an
204 * org unit setting, it is used
205 * 2. If orgloc is not valid, we check the org unit auth timeout
206 * setting for the home org unit of the user logging in
207 * 3. If that setting is not defined, we use the configured defaults
209 static double oilsAuthGetTimeout( const jsonObject* userObj, const char* type, double orgloc ) {
211 if(!_oilsAuthOPACTimeout) { /* Load the default timeouts */
213 _oilsAuthOPACTimeout =
215 osrf_settings_host_value_object(
216 "/apps/open-ils.auth/app_settings/default_timeout/opac"));
218 _oilsAuthStaffTimeout =
220 osrf_settings_host_value_object(
221 "/apps/open-ils.auth/app_settings/default_timeout/staff" ));
223 _oilsAuthOverrideTimeout =
225 osrf_settings_host_value_object(
226 "/apps/open-ils.auth/app_settings/default_timeout/temp" ));
229 osrfLogInfo(OSRF_LOG_MARK, "Set default auth timetouts: opac => %d : staff => %d : temp => %d",
230 _oilsAuthOPACTimeout, _oilsAuthStaffTimeout, _oilsAuthOverrideTimeout );
233 char* setting = NULL;
235 double home_ou = jsonObjectGetNumber( oilsFMGetObject( userObj, "home_ou" ) );
236 if(orgloc < 1) orgloc = (int) home_ou;
238 if(!strcmp(type, OILS_AUTH_OPAC))
239 setting = OILS_ORG_SETTING_OPAC_TIMEOUT;
240 else if(!strcmp(type, OILS_AUTH_STAFF))
241 setting = OILS_ORG_SETTING_STAFF_TIMEOUT;
242 else if(!strcmp(type, OILS_AUTH_TEMP))
243 setting = OILS_ORG_SETTING_TEMP_TIMEOUT;
245 char* timeout = oilsUtilsFetchOrgSetting( orgloc, setting );
248 if( orgloc != home_ou ) {
249 osrfLogDebug(OSRF_LOG_MARK, "Auth timeout not defined for org %d, "
250 "trying home_ou %d", orgloc, home_ou );
251 timeout = oilsUtilsFetchOrgSetting( (int) home_ou, setting );
254 if(!strcmp(type, OILS_AUTH_STAFF)) return _oilsAuthStaffTimeout;
255 if(!strcmp(type, OILS_AUTH_TEMP)) return _oilsAuthOverrideTimeout;
256 return _oilsAuthOPACTimeout;
260 double t = atof(timeout);
265 /* Adds the authentication token to the user cache. The timeout for the
266 * auth token is based on the type of login as well as (if type=='opac')
267 * the org location id.
268 * Returns the event that should be returned to the user.
269 * Event must be freed
271 static oilsEvent* oilsAuthHandleLoginOK( jsonObject* userObj, const char* uname,
272 const char* type, double orgloc, const char* workstation ) {
277 char* wsorg = jsonObjectToSimpleString(oilsFMGetObject(userObj, "ws_ou"));
278 if(wsorg) { /* if there is a workstation, use it for the timeout */
279 osrfLogDebug( OSRF_LOG_MARK,
280 "Auth session trying workstation id %d for auth timeout", atoi(wsorg));
281 timeout = oilsAuthGetTimeout( userObj, type, atoi(wsorg) );
284 osrfLogDebug( OSRF_LOG_MARK,
285 "Auth session trying org from param [%d] for auth timeout", orgloc );
286 timeout = oilsAuthGetTimeout( userObj, type, orgloc );
288 osrfLogDebug(OSRF_LOG_MARK, "Auth session timeout for %s: %f", uname, timeout );
290 char* string = va_list_to_string(
291 "%d.%ld.%s", (long) getpid(), time(NULL), uname );
292 char* authToken = md5sum(string);
293 char* authKey = va_list_to_string(
294 "%s%s", OILS_AUTH_CACHE_PRFX, authToken );
296 const char* ws = (workstation) ? workstation : "";
297 osrfLogActivity(OSRF_LOG_MARK,
298 "successful login: username=%s, authtoken=%s, workstation=%s", uname, authToken, ws );
300 oilsFMSetString( userObj, "passwd", "" );
301 jsonObject* cacheObj = jsonParseStringFmt("{\"authtime\": %f}", timeout);
302 jsonObjectSetKey( cacheObj, "userobj", jsonObjectClone(userObj));
304 osrfCachePutObject( authKey, cacheObj, timeout );
305 jsonObjectFree(cacheObj);
306 osrfLogInternal(OSRF_LOG_MARK, "oilsAuthComplete(): Placed user object into cache");
307 jsonObject* payload = jsonParseStringFmt(
308 "{ \"authtoken\": \"%s\", \"authtime\": %f }", authToken, timeout );
310 response = oilsNewEvent2( OSRF_LOG_MARK, OILS_EVENT_SUCCESS, payload );
311 free(string); free(authToken); free(authKey);
312 jsonObjectFree(payload);
317 static oilsEvent* oilsAuthVerifyWorkstation(
318 const osrfMethodContext* ctx, jsonObject* userObj, const char* ws ) {
319 osrfLogInfo(OSRF_LOG_MARK, "Attaching workstation to user at login: %s", ws);
320 jsonObject* workstation = oilsUtilsFetchWorkstationByName(ws);
321 if(!workstation) return oilsNewEvent(OSRF_LOG_MARK, "WORKSTATION_NOT_FOUND");
322 long wsid = oilsFMGetObjectId(workstation);
323 LONG_TO_STRING(wsid);
324 char* orgid = oilsFMGetString(workstation, "owning_lib");
325 oilsFMSetString(userObj, "wsid", LONGSTR);
326 oilsFMSetString(userObj, "ws_ou", orgid);
328 jsonObjectFree(workstation);
334 /* see if the card used to login is marked as barred */
335 static oilsEvent* oilsAuthCheckCard( const char* barcode ) {
336 if(!barcode) return NULL;
337 osrfLogDebug(OSRF_LOG_MARK, "Checking to see if barcode %s is active", barcode);
339 jsonObject* params = jsonParseStringFmt("{\"barcode\":\"%s\"}", barcode);
340 jsonObject* card = oilsUtilsQuickReq(
341 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
342 jsonObjectFree(params);
344 char* active = oilsFMGetString(card, "active");
345 jsonObjectFree(card);
347 oilsEvent* return_event = NULL;
348 if( ! oilsUtilsIsDBTrue(active) ) {
349 osrfLogInfo(OSRF_LOG_MARK, "barcode %s is not active, returning event", barcode);
350 return_event = oilsNewEvent(OSRF_LOG_MARK, "PATRON_CARD_INACTIVE");
359 int oilsAuthComplete( osrfMethodContext* ctx ) {
360 OSRF_METHOD_VERIFY_CONTEXT(ctx);
362 const jsonObject* args = jsonObjectGetIndex(ctx->params, 0);
364 const char* uname = jsonObjectGetString(jsonObjectGetKeyConst(args, "username"));
365 const char* password = jsonObjectGetString(jsonObjectGetKeyConst(args, "password"));
366 const char* type = jsonObjectGetString(jsonObjectGetKeyConst(args, "type"));
367 double orgloc = jsonObjectGetNumber(jsonObjectGetKeyConst(args, "org"));
368 const char* workstation = jsonObjectGetString(jsonObjectGetKeyConst(args, "workstation"));
369 char* barcode = jsonObjectToSimpleString(jsonObjectGetKeyConst(args, "barcode"));
371 const char* ws = (workstation) ? workstation : "";
374 if(!type) type = OILS_AUTH_STAFF;
376 if( !( (uname || barcode) && password) ) {
378 return osrfAppRequestRespondException( ctx->session, ctx->request,
379 "username/barcode and password required for method: %s", ctx->method->name );
382 oilsEvent* response = NULL;
383 jsonObject* userObj = NULL;
385 if(uname) userObj = oilsUtilsFetchUserByUsername( uname );
386 else if(barcode) userObj = oilsUtilsFetchUserByBarcode( barcode );
389 response = oilsNewEvent( OSRF_LOG_MARK, OILS_EVENT_AUTH_FAILED );
390 osrfLogInfo(OSRF_LOG_MARK, "failed login: username=%s, barcode=%s, workstation=%s", uname, barcode, ws );
391 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
392 oilsEventFree(response);
397 /* first let's see if they have the right credentials */
399 if(uname) passOK = oilsAuthVerifyPassword( ctx, userObj, uname, password );
401 passOK = oilsAuthVerifyPassword( ctx, userObj, barcode, password );
404 jsonObjectFree(userObj);
409 /* first see if their account is inactive */
410 char* active = oilsFMGetString(userObj, "active");
411 if( !oilsUtilsIsDBTrue(active) ) {
412 response = oilsNewEvent(OSRF_LOG_MARK, "PATRON_INACTIVE");
413 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
414 oilsEventFree(response);
415 jsonObjectFree(userObj);
422 /* then see if the barcode they used is active */
423 if( barcode && ctx && userObj && (response = oilsAuthCheckCard( barcode )) ) {
424 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
425 oilsEventFree(response);
426 jsonObjectFree(userObj);
432 /* check to see if the user is even allowed to login */
433 if( oilsAuthCheckLoginPerm( ctx, userObj, type ) == -1 ) {
434 jsonObjectFree(userObj);
440 /* if a workstation is defined, flesh the user with the workstation info */
441 if( workstation != NULL ) {
442 osrfLogDebug(OSRF_LOG_MARK, "Workstation is %s", workstation);
443 response = oilsAuthVerifyWorkstation( ctx, userObj, workstation );
445 jsonObjectFree(userObj);
446 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
447 oilsEventFree(response);
453 /* otherwise, use the home org as the workstation org on the user */
454 char* orgid = oilsFMGetString(userObj, "home_ou");
455 oilsFMSetString(userObj, "ws_ou", orgid);
459 char* freeable_uname = NULL;
461 uname = freeable_uname = oilsFMGetString( userObj, "usrname" );
465 response = oilsAuthHandleLoginOK( userObj, uname, type, orgloc, workstation );
468 response = oilsNewEvent( OSRF_LOG_MARK, OILS_EVENT_AUTH_FAILED );
469 osrfLogInfo(OSRF_LOG_MARK, "failed login: username=%s, barcode=%s, workstation=%s", uname, barcode, ws );
472 jsonObjectFree(userObj);
473 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
474 oilsEventFree(response);
477 if(freeable_uname) free(freeable_uname);
484 int oilsAuthSessionDelete( osrfMethodContext* ctx ) {
485 OSRF_METHOD_VERIFY_CONTEXT(ctx);
487 const char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
488 jsonObject* resp = NULL;
491 osrfLogDebug(OSRF_LOG_MARK, "Removing auth session: %s", authToken );
492 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken ); /**/
493 osrfCacheRemove(key);
494 resp = jsonNewObject(authToken); /**/
498 osrfAppRespondComplete( ctx, resp );
499 jsonObjectFree(resp);
503 /** Resets the auth login timeout
504 * @return The event object, OILS_EVENT_SUCCESS, or OILS_EVENT_NO_SESSION
506 static oilsEvent* _oilsAuthResetTimeout( const char* authToken ) {
507 if(!authToken) return NULL;
509 oilsEvent* evt = NULL;
512 osrfLogDebug(OSRF_LOG_MARK, "Resetting auth timeout for session %s", authToken);
513 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken );
514 jsonObject* cacheObj = osrfCacheGetObject( key );
517 osrfLogInfo(OSRF_LOG_MARK, "No user in the cache exists with key %s", key);
518 evt = oilsNewEvent(OSRF_LOG_MARK, OILS_EVENT_NO_SESSION);
522 timeout = jsonObjectGetNumber( jsonObjectGetKeyConst( cacheObj, "authtime"));
523 osrfCacheSetExpire( timeout, key );
524 jsonObject* payload = jsonNewNumberObject(timeout);
525 evt = oilsNewEvent2(OSRF_LOG_MARK, OILS_EVENT_SUCCESS, payload);
526 jsonObjectFree(payload);
527 jsonObjectFree(cacheObj);
534 int oilsAuthResetTimeout( osrfMethodContext* ctx ) {
535 OSRF_METHOD_VERIFY_CONTEXT(ctx);
536 const char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0));
537 oilsEvent* evt = _oilsAuthResetTimeout(authToken);
538 osrfAppRespondComplete( ctx, oilsEventToJSON(evt) );
544 int oilsAuthSessionRetrieve( osrfMethodContext* ctx ) {
545 OSRF_METHOD_VERIFY_CONTEXT(ctx);
547 const char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0));
548 jsonObject* cacheObj = NULL;
549 oilsEvent* evt = NULL;
553 evt = _oilsAuthResetTimeout(authToken);
555 if( evt && strcmp(evt->event, OILS_EVENT_SUCCESS) ) {
556 osrfAppRespondComplete( ctx, oilsEventToJSON(evt) );
560 osrfLogDebug(OSRF_LOG_MARK, "Retrieving auth session: %s", authToken);
561 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken );
562 cacheObj = osrfCacheGetObject( key );
564 osrfAppRespondComplete( ctx, jsonObjectGetKeyConst( cacheObj, "userobj"));
565 jsonObjectFree(cacheObj);
567 oilsEvent* evt2 = oilsNewEvent(OSRF_LOG_MARK, OILS_EVENT_NO_SESSION);
568 osrfAppRespondComplete( ctx, oilsEventToJSON(evt2) ); /* should be event.. */
576 evt = oilsNewEvent(OSRF_LOG_MARK, OILS_EVENT_NO_SESSION);
577 osrfAppRespondComplete( ctx, oilsEventToJSON(evt) );