]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_auth.c
incorporated ilseents.xml file into opac
[Evergreen.git] / Open-ILS / src / c-apps / oils_auth.c
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"
6 #include "oils_constants.h"
7
8 #define OILS_AUTH_CACHE_PRFX "oils_auth_"
9
10 #define MODULENAME "open-ils.auth"
11
12 int osrfAppInitialize();
13 int osrfAppChildInit();
14 int osrfMathRun( osrfMethodContext* );
15
16
17 int osrfAppInitialize() {
18
19         osrfAppRegisterMethod( 
20                 MODULENAME, 
21                 "open-ils.auth.authenticate.init", 
22                 "oilsAuthInit", 
23                 "Start the authentication process and returns the intermediate authentication seed"
24                 " PARAMS( username )", 1, 0 );
25
26         osrfAppRegisterMethod( 
27                 MODULENAME, 
28                 "open-ils.auth.authenticate.complete", 
29                 "oilsAuthComplete", 
30                 "Completes the authentication process and returns the auth token "
31                 "PARAMS(username, md5sum( seed + password ) )", 2, 0 );
32
33         osrfAppRegisterMethod( 
34                 MODULENAME, 
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 );
39
40         osrfAppRegisterMethod( 
41                 MODULENAME, 
42                 "open-ils.auth.session.delete", 
43                 "oilsAuthSessionDelete", 
44                 "Destroys the given login session "
45                 "PARAMS( authToken )",  1, 0 );
46
47         return 0;
48 }
49
50 int osrfAppChildInit() {
51         return 0;
52 }
53
54 int oilsAuthInit( osrfMethodContext* ctx ) {
55         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
56
57         jsonObject* resp;
58         char* username = NULL;
59         char* seed = NULL;
60         char* md5seed = NULL;
61         char* key = NULL;
62
63         if( (username = jsonObjectGetString(jsonObjectGetIndex(ctx->params, 0))) ) {
64
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 );
67
68                 md5seed = md5sum(seed);
69                 osrfCachePutString( key, md5seed, 30 );
70
71                 osrfLogDebug( "oilsAuthInit(): has seed %s and key %s", md5seed, key );
72
73                 resp = jsonNewObject(md5seed);  
74                 osrfAppRespondComplete( ctx, resp );
75
76                 jsonObjectFree(resp);
77                 free(seed);
78                 free(md5seed);
79                 free(key);
80                 return 0;
81         }
82
83         return -1;
84 }
85
86
87 int oilsAuthComplete( osrfMethodContext* ctx ) {
88         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
89
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;
94
95         if( uname && password ) {
96
97                 jsonObject* response = jsonParseString("{\"ilsevent\":0}");
98
99                 /* grab the user object from storage */
100                 osrfLogDebug( "oilsAuth calling method %s with username %s", storageMethod, uname );
101
102                 osrfAppSession* session = osrfAppSessionClientInit( "open-ils.storage" ); /**/
103                 //jsonObject* params = jsonNewObject(uname); /**/
104                 jsonObject* params = jsonParseString("[\"%s\"]", uname);
105                 int reqid = osrfAppSessionMakeRequest( session, params, storageMethod, 1, NULL );
106                 jsonObjectFree(params);
107                 osrfLogInternal("oilsAuth waiting from response from storage...");
108                 omsg = osrfAppSessionRequestRecv( session, reqid, 60 ); /**/
109                 osrfLogInternal("oilsAuth storage request returned");
110
111                 if(!omsg) { 
112                         osrfAppSessionFree(session);
113                         return osrfAppRequestRespondException( ctx->session, ctx->request,
114                                 "No response from storage server for method %s", storageMethod ); 
115                 }
116
117                 jsonObject* userObj = osrfMessageGetResult(omsg);
118
119                 char* _j = jsonObjectToJSON(userObj);
120                 osrfLogDebug( "Auth received user object from storage: %s", _j );
121                 free(_j);
122
123                 /* the method is atomic, grab the first user we receive */
124                 if( userObj ) userObj = jsonObjectGetIndex(userObj, 0);
125                 
126                 if(!userObj) { /* XXX needs to be a 'friendly' exception */
127                         osrfMessageFree(omsg);
128                         osrfAppSessionFree(session);
129                         jsonObjectSetKey(response, OILS_ILS_EVENT, 
130                                                 jsonNewNumberObject(OILS_ILS_EVENT_AUTH_FAILED));
131                         osrfAppRespondComplete( ctx, response ); 
132                         jsonObjectFree(response);
133                         return 0;
134
135                         /*
136                         return osrfAppRequestRespondException( ctx->session, 
137                                         ctx->request, "User %s not found in the database", uname );
138                                         */
139                 }
140
141                 char* realPassword = oilsFMGetString( userObj, "passwd" ); /**/
142                 char* seed = osrfCacheGetString( "%s%s", OILS_AUTH_CACHE_PRFX, uname ); /**/
143
144                 if(!seed) {
145                         osrfMessageFree(omsg);
146                         osrfAppSessionFree(session);
147                         return osrfAppRequestRespondException( ctx->session,
148                                 ctx->request, "No authentication seed found. "
149                                 "open-ils.auth.authenticate.init must be called first");
150                 }
151
152                 osrfLogDebug( "oilsAuth retrieved seed from cache: %s", seed );
153                 char* maskedPw = md5sum( "%s%s", seed, realPassword );
154                 if(!maskedPw) return -1;
155                 osrfLogDebug( "oilsAuth generated masked password %s. "
156                                 "Testing against provided password %s", maskedPw, password );
157
158
159                 if( !strcmp( maskedPw, password ) ) {
160
161                         osrfLogActivity( "User %s successfully logged in", uname );
162
163                         char* string = va_list_to_string( "%d.%d.%s", getpid(), time(NULL), uname ); /**/
164                         char* authToken = md5sum(string); /**/
165                         char* authKey = va_list_to_string( "%s%s", OILS_AUTH_CACHE_PRFX, authToken ); /**/
166
167                         osrfLogInternal("oilsAuthComplete(): Setting fieldmapper string on the user object");
168                         oilsFMSetString( userObj, "passwd", "" );
169                         osrfCachePutObject( authKey, userObj, 28800 ); /* XXX config value */
170                         osrfLogInternal("oilsAuthComplete(): Placed user object into cache");
171                 //      response = jsonNewObject( authToken );
172                         jsonObjectSetKey( response, "authtoken", jsonNewObject(authToken) );    
173                         free(string); free(authToken); free(authKey);
174
175                 } else {
176
177                         jsonObjectSetKey(response, OILS_ILS_EVENT, 
178                                                 jsonNewNumberObject(OILS_ILS_EVENT_AUTH_FAILED));
179                         osrfLogInfo( "Login failed for for %s", uname );
180                 }
181
182                 osrfLogInternal("oilsAuthComplete responding to client");
183                 osrfAppRespondComplete( ctx, response ); 
184                 jsonObjectFree(response);
185                 osrfMessageFree(omsg);
186                 osrfAppSessionFree(session);
187
188         } else {
189                 return osrfAppRequestRespondException( ctx->session, ctx->request, 
190                         "username and password required for method: %s", ctx->method->name );
191         }
192
193         return 0;
194
195 }
196
197 int oilsAuthSessionRetrieve( osrfMethodContext* ctx ) {
198         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
199
200         char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0));
201         jsonObject* userObj = NULL;
202
203         if( authToken ){
204                 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken ); /**/
205                 userObj = osrfCacheGetObject( key ); /**/
206                 free(key);
207         }
208
209         osrfAppRespondComplete( ctx, userObj );
210         jsonObjectFree(userObj);
211         return 0;
212 }
213
214 int oilsAuthSessionDelete( osrfMethodContext* ctx ) {
215         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
216
217         char* authToken = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
218         jsonObject* resp = NULL;
219
220         if( authToken ) {
221                 char* key = va_list_to_string("%s%s", OILS_AUTH_CACHE_PRFX, authToken ); /**/
222                 osrfCacheRemove(key);
223                 resp = jsonNewObject(authToken); /**/
224                 free(key);
225         }
226
227         osrfAppRespondComplete( ctx, resp );
228         jsonObjectFree(resp);
229         return 0;
230 }
231
232
233