1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "opensrf/osrf_settings.h"
4 #include "objson/object.h"
5 #include "opensrf/log.h"
6 #include "oils_utils.h"
7 #include "oils_constants.h"
8 #include "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 int __oilsAuthOPACTimeout = 0;
22 int __oilsAuthStaffTimeout = 0;
23 int __oilsAuthOverrideTimeout = 0;
26 int osrfAppInitialize() {
28 osrfLogInfo(OSRF_LOG_MARK, "Initializing Auth Server...");
30 osrfAppRegisterMethod(
32 "open-ils.auth.authenticate.init",
34 "Start the authentication process and returns the intermediate authentication seed"
35 " PARAMS( username )", 1, 0 );
37 osrfAppRegisterMethod(
39 "open-ils.auth.authenticate.complete",
41 "Completes the authentication process. Returns an object like so: "
42 "{authtoken : <token>, authtime:<time>}, where authtoken is the login "
43 "tokena and authtime is the number of seconds the session will be active"
44 "PARAMS(username, md5sum( seed + password ), type, org_id ) "
45 "type can be one of 'opac','staff', or 'temp' and it defaults to 'staff' "
46 "org_id is the location at which the login should be considered "
47 "active for login timeout purposes" , 1, 0 );
49 osrfAppRegisterMethod(
51 "open-ils.auth.session.retrieve",
52 "oilsAuthSessionRetrieve",
53 "Pass in the auth token and this retrieves the user object. The auth "
54 "timeout is reset when this call is made "
55 "Returns the user object (password blanked) for the given login session "
56 "PARAMS( authToken )", 1, 0 );
58 osrfAppRegisterMethod(
60 "open-ils.auth.session.delete",
61 "oilsAuthSessionDelete",
62 "Destroys the given login session "
63 "PARAMS( authToken )", 1, 0 );
65 osrfAppRegisterMethod(
67 "open-ils.auth.session.reset_timeout",
68 "oilsAuthResetTimeout",
69 "Resets the login timeout for the given session "
70 "Returns an ILS Event with payload = session_timeout of session "
71 "is found, otherwise returns the NO_SESSION event"
72 "PARAMS( authToken )", 1, 0 );
77 int osrfAppChildInit() {
81 int oilsAuthInit( osrfMethodContext* ctx ) {
82 OSRF_METHOD_VERIFY_CONTEXT(ctx);
86 char* username = NULL;
91 if( (username = jsonObjectToSimpleString(jsonObjectGetIndex(ctx->params, 0))) ) {
93 if( strchr( username, ' ' ) ) {
95 /* spaces are not allowed */
96 resp = jsonNewObject("x"); /* 'x' will never be a valid seed */
97 osrfAppRespondComplete( ctx, resp );
101 seed = va_list_to_string( "%d.%d.%s", time(NULL), getpid(), username );
102 key = va_list_to_string( "%s%s", OILS_AUTH_CACHE_PRFX, username );
104 md5seed = md5sum(seed);
105 osrfCachePutString( key, md5seed, 30 );
107 osrfLogDebug( OSRF_LOG_MARK, "oilsAuthInit(): has seed %s and key %s", md5seed, key );
109 resp = jsonNewObject(md5seed);
110 osrfAppRespondComplete( ctx, resp );
118 jsonObjectFree(resp);
125 /** Verifies that the user has permission to login with the
126 * given type. If the permission fails, an oilsEvent is returned
128 * @return -1 if the permission check failed, 0 if ther permission
131 int oilsAuthCheckLoginPerm(
132 osrfMethodContext* ctx, jsonObject* userObj, char* type ) {
134 if(!(userObj && type)) return -1;
135 oilsEvent* perm = NULL;
137 if(!strcasecmp(type, OILS_AUTH_OPAC)) {
138 char* permissions[] = { "OPAC_LOGIN" };
139 perm = oilsUtilsCheckPerms( oilsFMGetObjectId( userObj ), -1, permissions, 1 );
141 } else if(!strcasecmp(type, OILS_AUTH_STAFF)) {
142 char* permissions[] = { "STAFF_LOGIN" };
143 perm = oilsUtilsCheckPerms( oilsFMGetObjectId( userObj ), -1, permissions, 1 );
145 } else if(!strcasecmp(type, OILS_AUTH_TEMP)) {
146 char* permissions[] = { "STAFF_LOGIN" };
147 perm = oilsUtilsCheckPerms( oilsFMGetObjectId( userObj ), -1, permissions, 1 );
151 osrfAppRespondComplete( ctx, oilsEventToJSON(perm) );
160 * Returns 1 if the password provided matches the user's real password
161 * Returns 0 otherwise
162 * Returns -1 on error
164 int oilsAuthVerifyPassword(
165 osrfMethodContext* ctx, jsonObject* userObj, char* uname, char* password ) {
168 char* realPassword = oilsFMGetString( userObj, "passwd" ); /**/
169 char* seed = osrfCacheGetString( "%s%s", OILS_AUTH_CACHE_PRFX, uname ); /**/
172 return osrfAppRequestRespondException( ctx->session,
173 ctx->request, "No authentication seed found. "
174 "open-ils.auth.authenticate.init must be called first");
177 osrfLogDebug(OSRF_LOG_MARK, "oilsAuth retrieved seed from cache: %s", seed );
178 char* maskedPw = md5sum( "%s%s", seed, realPassword );
179 if(!maskedPw) return -1;
180 osrfLogDebug(OSRF_LOG_MARK, "oilsAuth generated masked password %s. "
181 "Testing against provided password %s", maskedPw, password );
183 if( !strcmp( maskedPw, password ) ) ret = 1;
193 * Calculates the login timeout
194 * 1. If orgloc is 1 or greater and has a timeout specified as an
195 * org unit setting, it is used
196 * 2. If orgloc is not valid, we check the org unit auth timeout
197 * setting for the home org unit of the user logging in
198 * 3. If that setting is not defined, we use the configured defaults
200 double oilsAuthGetTimeout( jsonObject* userObj, char* type, double orgloc ) {
202 if(!__oilsAuthOPACTimeout) { /* Load the default timeouts */
204 __oilsAuthOPACTimeout =
206 osrf_settings_host_value_object(
207 "/apps/open-ils.auth/app_settings/default_timeout/opac"));
209 __oilsAuthStaffTimeout =
211 osrf_settings_host_value_object(
212 "/apps/open-ils.auth/app_settings/default_timeout/staff" ));
214 __oilsAuthOverrideTimeout =
216 osrf_settings_host_value_object(
217 "/apps/open-ils.auth/app_settings/default_timeout/temp" ));
220 osrfLogInfo(OSRF_LOG_MARK, "Set default auth timetouts: opac => %d : staff => %d : temp => %d",
221 __oilsAuthOPACTimeout, __oilsAuthStaffTimeout, __oilsAuthOverrideTimeout );
224 char* setting = NULL;
226 double home_ou = jsonObjectGetNumber( oilsFMGetObject( userObj, "home_ou" ) );
227 if(orgloc < 1) orgloc = (int) home_ou;
229 if(!strcmp(type, OILS_AUTH_OPAC))
230 setting = OILS_ORG_SETTING_OPAC_TIMEOUT;
231 else if(!strcmp(type, OILS_AUTH_STAFF))
232 setting = OILS_ORG_SETTING_STAFF_TIMEOUT;
233 else if(!strcmp(type, OILS_AUTH_TEMP))
234 setting = OILS_ORG_SETTING_TEMP_TIMEOUT;
236 char* timeout = oilsUtilsFetchOrgSetting( orgloc, setting );
239 if( orgloc != home_ou ) {
240 osrfLogDebug(OSRF_LOG_MARK, "Auth timeout not defined for org %d, "
241 "trying home_ou %d", orgloc, home_ou );
242 timeout = oilsUtilsFetchOrgSetting( (int) home_ou, setting );
245 if(!strcmp(type, OILS_AUTH_STAFF)) return __oilsAuthStaffTimeout;
246 if(!strcmp(type, OILS_AUTH_TEMP)) return __oilsAuthOverrideTimeout;
247 return __oilsAuthOPACTimeout;
251 double t = atof(timeout);
256 /* Adds the authentication token to the user cache. The timeout for the
257 * auth token is based on the type of login as well as (if type=='opac')
258 * the org location id.
259 * Returns the event that should be returned to the user.
260 * Event must be freed
262 oilsEvent* oilsAuthHandleLoginOK(
263 jsonObject* userObj, char* uname, char* type, double orgloc, char* workstation ) {
268 char* wsorg = jsonObjectToSimpleString(oilsFMGetObject(userObj, "ws_ou"));
269 if(wsorg) { /* if there is a workstation, use it for the timeout */
270 osrfLogDebug( OSRF_LOG_MARK,
271 "Auth session trying workstation id %d for auth timeout", atoi(wsorg));
272 timeout = oilsAuthGetTimeout( userObj, type, atoi(wsorg) );
275 osrfLogDebug( OSRF_LOG_MARK,
276 "Auth session trying org from param [%d] for auth timeout", orgloc );
277 timeout = oilsAuthGetTimeout( userObj, type, orgloc );
279 osrfLogDebug(OSRF_LOG_MARK, "Auth session timeout for %s: %lf", uname, timeout );
281 char* string = va_list_to_string(
282 "%d.%d.%s", getpid(), time(NULL), uname );
283 char* authToken = md5sum(string);
284 char* authKey = va_list_to_string(
285 "%s%s", OILS_AUTH_CACHE_PRFX, authToken );
287 char* ws = (workstation) ? workstation : "";
288 osrfLogActivity(OSRF_LOG_MARK,
289 "successful login: username=%s, authtoken=%s, workstation=%s", uname, authToken, ws );
291 oilsFMSetString( userObj, "passwd", "" );
292 jsonObject* cacheObj = jsonParseStringFmt("{\"authtime\": %lf}", timeout);
293 jsonObjectSetKey( cacheObj, "userobj", jsonObjectClone(userObj));
295 osrfCachePutObject( authKey, cacheObj, timeout );
296 jsonObjectFree(cacheObj);
297 osrfLogInternal(OSRF_LOG_MARK, "oilsAuthComplete(): Placed user object into cache");
298 jsonObject* payload = jsonParseStringFmt(
299 "{ \"authtoken\": \"%s\", \"authtime\": %lf }", authToken, timeout );
301 response = oilsNewEvent2( OSRF_LOG_MARK, OILS_EVENT_SUCCESS, payload );
302 free(string); free(authToken); free(authKey);
303 jsonObjectFree(payload);
308 oilsEvent* oilsAuthVerifyWorkstation(
309 osrfMethodContext* ctx, jsonObject* userObj, char* ws ) {
310 osrfLogInfo(OSRF_LOG_MARK, "Attaching workstation to user at login: %s", ws);
311 jsonObject* workstation = oilsUtilsFetchWorkstationByName(ws);
312 if(!workstation) return oilsNewEvent(OSRF_LOG_MARK, "WORKSTATION_NOT_FOUND");
313 long wsid = oilsFMGetObjectId(workstation);
314 LONG_TO_STRING(wsid);
315 char* orgid = oilsFMGetString(workstation, "owning_lib");
316 oilsFMSetString(userObj, "wsid", LONGSTR);
317 oilsFMSetString(userObj, "ws_ou", orgid);
324 /* see if the card used to login is marked as barred */
325 static oilsEvent* oilsAuthCheckCard( osrfMethodContext* ctx, jsonObject* userObj, char* barcode) {
326 if(!(ctx && userObj && barcode)) return NULL;
327 osrfLogDebug(OSRF_LOG_MARK, "Checking to see if barcode %s is active", barcode);
329 jsonObject* params = jsonParseStringFmt("{\"barcode\":\"%s\"}", barcode);
330 jsonObject* card = oilsUtilsQuickReq(
331 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
333 char* active = oilsFMGetString(card, "active");
334 if( ! oilsUtilsIsDBTrue(active) ) {
336 osrfLogInfo(OSRF_LOG_MARK, "barcode %s is not active, returning event", barcode);
337 return oilsNewEvent(OSRF_LOG_MARK, "PATRON_CARD_INACTIVE");
346 int oilsAuthComplete( osrfMethodContext* ctx ) {
347 OSRF_METHOD_VERIFY_CONTEXT(ctx);
349 jsonObject* args = jsonObjectGetIndex(ctx->params, 0);
351 char* uname = jsonObjectGetString(jsonObjectGetKey(args, "username"));
352 char* password = jsonObjectGetString(jsonObjectGetKey(args, "password"));
353 char* type = jsonObjectGetString(jsonObjectGetKey(args, "type"));
354 double orgloc = jsonObjectGetNumber(jsonObjectGetKey(args, "org"));
355 char* workstation = jsonObjectGetString(jsonObjectGetKey(args, "workstation"));
356 char* barcode = jsonObjectToSimpleString(jsonObjectGetKey(args, "barcode"));
358 char* ws = (workstation) ? workstation : "";
361 if(!type) type = OILS_AUTH_STAFF;
363 if( !( (uname || barcode) && password) ) {
365 return osrfAppRequestRespondException( ctx->session, ctx->request,
366 "username/barocode and password required for method: %s", ctx->method->name );
369 oilsEvent* response = NULL;
370 jsonObject* userObj = NULL;
372 if(uname) userObj = oilsUtilsFetchUserByUsername( uname );
373 else if(barcode) userObj = oilsUtilsFetchUserByBarcode( barcode );
376 response = oilsNewEvent( OSRF_LOG_MARK, OILS_EVENT_AUTH_FAILED );
377 osrfLogInfo(OSRF_LOG_MARK, "failed login: username=%s, barcode=%s, workstation=%s", uname, barcode, ws );
378 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
379 oilsEventFree(response);
384 /* first let's see if they have the right credentials */
386 if(uname) passOK = oilsAuthVerifyPassword( ctx, userObj, uname, password );
388 passOK = oilsAuthVerifyPassword( ctx, userObj, barcode, password );
395 /* first see if their account is inactive */
396 char* active = oilsFMGetString(userObj, "active");
397 if( !oilsUtilsIsDBTrue(active) ) {
398 response = oilsNewEvent(OSRF_LOG_MARK, "PATRON_INACTIVE");
399 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
400 oilsEventFree(response);
407 /* then see if the barcode they used is active */
408 if( barcode && (response = oilsAuthCheckCard( ctx, userObj, barcode )) ) {
409 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
410 oilsEventFree(response);
416 /* check to see if the user is even allowed to login */
417 if( oilsAuthCheckLoginPerm( ctx, userObj, type ) == -1 ) {
418 jsonObjectFree(userObj);
424 /* if a workstation is defined, flesh the user with the workstation info */
425 if( workstation != NULL ) {
426 osrfLogDebug(OSRF_LOG_MARK, "Workstation is %s", workstation);
427 response = oilsAuthVerifyWorkstation( ctx, userObj, workstation );
429 jsonObjectFree(userObj);
430 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
431 oilsEventFree(response);
437 /* otherwise, use the home org as the workstation org on the user */
438 char* orgid = oilsFMGetString(userObj, "home_ou");
439 oilsFMSetString(userObj, "ws_ou", orgid);
445 uname = oilsFMGetString( userObj, "usrname" );
450 response = oilsAuthHandleLoginOK( userObj, uname, type, orgloc, workstation );
453 response = oilsNewEvent( OSRF_LOG_MARK, OILS_EVENT_AUTH_FAILED );
454 osrfLogInfo(OSRF_LOG_MARK, "failed login: username=%s, barcode=%s, workstation=%s", uname, barcode, ws );
457 jsonObjectFree(userObj);
458 osrfAppRespondComplete( ctx, oilsEventToJSON(response) );
459 oilsEventFree(response);
462 if(freeuname) free(uname);
469 int oilsAuthSessionDelete( osrfMethodContext* ctx ) {
470 OSRF_METHOD_VERIFY_CONTEXT(ctx);
472 char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
473 jsonObject* resp = NULL;
476 osrfLogDebug(OSRF_LOG_MARK, "Removing auth session: %s", authToken );
477 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken ); /**/
478 osrfCacheRemove(key);
479 resp = jsonNewObject(authToken); /**/
483 osrfAppRespondComplete( ctx, resp );
484 jsonObjectFree(resp);
488 /** Resets the auth login timeout
489 * @return The event object, OILS_EVENT_SUCCESS, or OILS_EVENT_NO_SESSION
491 oilsEvent* _oilsAuthResetTimeout( char* authToken ) {
492 if(!authToken) return NULL;
494 oilsEvent* evt = NULL;
497 osrfLogDebug(OSRF_LOG_MARK, "Resetting auth timeout for session %s", authToken);
498 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken );
499 jsonObject* cacheObj = osrfCacheGetObject( key );
502 osrfLogError(OSRF_LOG_MARK, "No user in the cache exists with key %s", key);
503 evt = oilsNewEvent(OSRF_LOG_MARK, OILS_EVENT_NO_SESSION);
507 timeout = jsonObjectGetNumber( jsonObjectGetKey( cacheObj, "authtime"));
508 osrfCacheSetExpire( timeout, key );
509 jsonObject* payload = jsonNewNumberObject(timeout);
510 evt = oilsNewEvent2(OSRF_LOG_MARK, OILS_EVENT_SUCCESS, payload);
511 jsonObjectFree(payload);
512 jsonObjectFree(cacheObj);
519 int oilsAuthResetTimeout( osrfMethodContext* ctx ) {
520 OSRF_METHOD_VERIFY_CONTEXT(ctx);
521 char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0));
522 oilsEvent* evt = _oilsAuthResetTimeout(authToken);
523 osrfAppRespondComplete( ctx, oilsEventToJSON(evt) );
529 int oilsAuthSessionRetrieve( osrfMethodContext* ctx ) {
530 OSRF_METHOD_VERIFY_CONTEXT(ctx);
532 char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0));
533 jsonObject* cacheObj = NULL;
534 oilsEvent* evt = NULL;
538 evt = _oilsAuthResetTimeout(authToken);
540 if( evt && strcmp(evt->event, OILS_EVENT_SUCCESS) ) {
541 osrfAppRespondComplete( ctx, oilsEventToJSON(evt) );
546 osrfLogDebug(OSRF_LOG_MARK, "Retrieving auth session: %s", authToken);
547 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken );
548 cacheObj = osrfCacheGetObject( key );
550 osrfAppRespondComplete( ctx, jsonObjectGetKey( cacheObj, "userobj"));
551 jsonObjectFree(cacheObj);
553 oilsEvent* evt = oilsNewEvent(OSRF_LOG_MARK, OILS_EVENT_NO_SESSION);
554 osrfAppRespondComplete( ctx, oilsEventToJSON(evt) ); /* should be event.. */
562 evt = oilsNewEvent(OSRF_LOG_MARK, OILS_EVENT_NO_SESSION);
563 osrfAppRespondComplete( ctx, oilsEventToJSON(evt) );