1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4 #include "opensrf/log.h"
5 #include "oils_utils.h"
7 #define OILS_AUTH_CACHE_PRFX "oils_auth_"
10 #define MODULENAME "open-ils.auth"
12 int osrfAppInitialize();
13 int osrfAppChildInit();
14 int osrfMathRun( osrfMethodContext* );
17 int osrfAppInitialize() {
19 osrfAppRegisterMethod(
21 "open-ils.auth.authenticate.init",
23 "Start the authentication process and returns the intermediate authentication seed"
24 " PARAMS( username )", 1, 0 );
26 osrfAppRegisterMethod(
28 "open-ils.auth.authenticate.complete",
30 "Completes the authentication process and returns the auth token "
31 "PARAMS(username, md5sum( seed + password ) )", 2, 0 );
33 osrfAppRegisterMethod(
35 "open-ils.auth.session.retrieve",
36 "oilsAuthSessionRetrieve",
37 "Returns the user object (password blanked) for the given login session "
38 "PARAMS( authToken )", 1, 0 );
40 osrfAppRegisterMethod(
42 "open-ils.auth.session.delete",
43 "oilsAuthSessionDelete",
44 "Destroys the given login session "
45 "PARAMS( authToken )", 1, 0 );
50 int osrfAppChildInit() {
54 int oilsAuthInit( osrfMethodContext* ctx ) {
55 OSRF_METHOD_VERIFY_CONTEXT(ctx);
58 char* username = NULL;
63 if( (username = jsonObjectGetString(jsonObjectGetIndex(ctx->params, 0))) ) {
65 seed = va_list_to_string( "%d.%d.%s", time(NULL), getpid(), username );
66 key = va_list_to_string( "%s%s", OILS_AUTH_CACHE_PRFX, username );
68 md5seed = md5sum(seed);
69 osrfCachePutString( key, md5seed, 30 );
71 osrfLogDebug( "oilsAuthInit(): has seed %s and key %s", md5seed, key );
73 resp = jsonNewObject(md5seed);
74 osrfAppRespondComplete( ctx, resp );
87 int oilsAuthComplete( osrfMethodContext* ctx ) {
88 OSRF_METHOD_VERIFY_CONTEXT(ctx);
90 char* uname = jsonObjectGetString(jsonObjectGetIndex(ctx->params, 0));
91 char* password = jsonObjectGetString(jsonObjectGetIndex(ctx->params, 1));
92 char* storageMethod = "open-ils.storage.direct.actor.user.search.usrname.atomic";
93 osrfMessage* omsg = NULL;
95 if( uname && password ) {
97 /* grab the user object from storage */
98 osrfLogDebug( "oilsAuth calling method %s with username %s", storageMethod, uname );
100 osrfAppSession* session = osrfAppSessionClientInit( "open-ils.storage" ); /**/
101 //jsonObject* params = jsonNewObject(uname); /**/
102 jsonObject* params = jsonParseString("[\"%s\"]", uname);
103 int reqid = osrfAppSessionMakeRequest( session, params, storageMethod, 1, NULL );
104 jsonObjectFree(params);
105 osrfLogInternal("oilsAuth waiting from response from storage...");
106 omsg = osrfAppSessionRequestRecv( session, reqid, 60 ); /**/
107 osrfLogInternal("oilsAuth storage request returned");
110 osrfAppSessionFree(session);
111 return osrfAppRequestRespondException( ctx->session, ctx->request,
112 "No response from storage server for method %s", storageMethod );
115 jsonObject* userObj = osrfMessageGetResult(omsg);
117 char* _j = jsonObjectToJSON(userObj);
118 osrfLogDebug( "Auth received user object from storage: %s", _j );
121 /* the method is atomic, grab the first user we receive */
122 if( userObj ) userObj = jsonObjectGetIndex(userObj, 0);
124 if(!userObj) { /* XXX needs to be a 'friendly' exception */
125 osrfMessageFree(omsg);
126 osrfAppSessionFree(session);
127 return osrfAppRequestRespondException( ctx->session,
128 ctx->request, "User %s not found in the database", uname );
131 char* realPassword = oilsFMGetString( userObj, "passwd" ); /**/
132 char* seed = osrfCacheGetString( "%s%s", OILS_AUTH_CACHE_PRFX, uname ); /**/
135 osrfMessageFree(omsg);
136 osrfAppSessionFree(session);
137 return osrfAppRequestRespondException( ctx->session,
138 ctx->request, "No authentication seed found. "
139 "open-ils.auth.authenticate.init must be called first");
142 osrfLogDebug( "oilsAuth retrieved seed from cache: %s", seed );
143 char* maskedPw = md5sum( "%s%s", seed, realPassword );
144 if(!maskedPw) return -1;
145 osrfLogDebug( "oilsAuth generated masked password %s. "
146 "Testing against provided password %s", maskedPw, password );
148 jsonObject* response;
150 if( !strcmp( maskedPw, password ) ) {
152 osrfLogInfo( "Login successful for %s", uname );
153 char* string = va_list_to_string( "%d.%d.%s", getpid(), time(NULL), uname ); /**/
154 char* authToken = md5sum(string); /**/
155 char* authKey = va_list_to_string( "%s%s", OILS_AUTH_CACHE_PRFX, authToken ); /**/
157 osrfLogInternal("oilsAuthComplete(): Setting fieldmapper string on the user object");
158 oilsFMSetString( userObj, "passwd", "" );
159 osrfCachePutObject( authKey, userObj, 28800 ); /* XXX config value */
160 osrfLogInternal("oilsAuthComplete(): Placed user object into cache");
161 response = jsonNewObject( authToken );
162 free(string); free(authToken); free(authKey);
166 osrfLogInfo( "Login failed for for %s", uname );
167 response = jsonNewNumberObject(0);
170 osrfLogInternal("oilsAuthComplete responding to client");
171 osrfAppRespondComplete( ctx, response );
172 jsonObjectFree(response);
173 osrfMessageFree(omsg);
174 osrfAppSessionFree(session);
177 return osrfAppRequestRespondException( ctx->session, ctx->request,
178 "username and password required for method: %s", ctx->method->name );
185 int oilsAuthSessionRetrieve( osrfMethodContext* ctx ) {
186 OSRF_METHOD_VERIFY_CONTEXT(ctx);
188 char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0));
189 jsonObject* userObj = NULL;
192 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken ); /**/
193 userObj = osrfCacheGetObject( key ); /**/
197 osrfAppRespondComplete( ctx, userObj );
198 jsonObjectFree(userObj);
202 int oilsAuthSessionDelete( osrfMethodContext* ctx ) {
203 OSRF_METHOD_VERIFY_CONTEXT(ctx);
205 char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
206 jsonObject* resp = NULL;
209 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken ); /**/
210 osrfCacheRemove(key);
211 resp = jsonNewObject(authToken); /**/
215 osrfAppRespondComplete( ctx, resp );
216 jsonObjectFree(resp);