2016-02-11 20:40:15 -08:00
|
|
|
|
|
|
|
|
import ClassesRouter from './ClassesRouter';
|
2016-11-24 15:47:41 -05:00
|
|
|
import Parse from 'parse/node';
|
2016-04-25 20:42:19 -07:00
|
|
|
import rest from '../rest';
|
|
|
|
|
import Auth from '../Auth';
|
2016-09-09 14:48:06 -04:00
|
|
|
import RestWrite from '../RestWrite';
|
|
|
|
|
import { newToken } from '../cryptoUtils';
|
2016-02-11 20:40:15 -08:00
|
|
|
|
|
|
|
|
export class SessionsRouter extends ClassesRouter {
|
|
|
|
|
handleFind(req) {
|
|
|
|
|
req.params.className = '_Session';
|
|
|
|
|
return super.handleFind(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleGet(req) {
|
|
|
|
|
req.params.className = '_Session';
|
|
|
|
|
return super.handleGet(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleCreate(req) {
|
|
|
|
|
req.params.className = '_Session';
|
|
|
|
|
return super.handleCreate(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleUpdate(req) {
|
|
|
|
|
req.params.className = '_Session';
|
|
|
|
|
return super.handleUpdate(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleDelete(req) {
|
|
|
|
|
req.params.className = '_Session';
|
|
|
|
|
return super.handleDelete(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleMe(req) {
|
|
|
|
|
// TODO: Verify correct behavior
|
|
|
|
|
if (!req.info || !req.info.sessionToken) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN,
|
|
|
|
|
'Session token required.');
|
|
|
|
|
}
|
2016-07-12 10:06:13 -04:00
|
|
|
return rest.find(req.config, Auth.master(req.config), '_Session', { sessionToken: req.info.sessionToken }, undefined, req.info.clientSDK)
|
2016-02-11 20:40:15 -08:00
|
|
|
.then((response) => {
|
|
|
|
|
if (!response.results || response.results.length == 0) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN,
|
|
|
|
|
'Session token not found.');
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
response: response.results[0]
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-09 14:48:06 -04:00
|
|
|
handleUpdateToRevocableSession(req) {
|
|
|
|
|
const config = req.config;
|
|
|
|
|
const masterAuth = Auth.master(config)
|
|
|
|
|
const user = req.auth.user;
|
2016-09-17 15:52:52 -04:00
|
|
|
// Issue #2720
|
|
|
|
|
// Calling without a session token would result in a not found user
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'invalid session');
|
|
|
|
|
}
|
2016-09-09 14:48:06 -04:00
|
|
|
const expiresAt = config.generateSessionExpiresAt();
|
|
|
|
|
const sessionData = {
|
|
|
|
|
sessionToken: 'r:' + newToken(),
|
|
|
|
|
user: {
|
|
|
|
|
__type: 'Pointer',
|
|
|
|
|
className: '_User',
|
|
|
|
|
objectId: user.id
|
|
|
|
|
},
|
|
|
|
|
createdWith: {
|
|
|
|
|
'action': 'upgrade',
|
|
|
|
|
},
|
|
|
|
|
restricted: false,
|
|
|
|
|
installationId: req.auth.installationId,
|
|
|
|
|
expiresAt: Parse._encode(expiresAt)
|
|
|
|
|
};
|
|
|
|
|
const create = new RestWrite(config, masterAuth, '_Session', null, sessionData);
|
2016-11-24 15:47:41 -05:00
|
|
|
return create.execute().then(() => {
|
2016-09-09 14:48:06 -04:00
|
|
|
// delete the session token, use the db to skip beforeSave
|
|
|
|
|
return config.database.update('_User', {
|
|
|
|
|
objectId: user.id
|
|
|
|
|
}, {
|
|
|
|
|
sessionToken: {__op: 'Delete'}
|
|
|
|
|
});
|
2016-11-24 15:47:41 -05:00
|
|
|
}).then(() => {
|
2016-09-09 14:48:06 -04:00
|
|
|
return Promise.resolve({ response: sessionData });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 23:47:44 -05:00
|
|
|
mountRoutes() {
|
2016-09-09 17:28:41 -04:00
|
|
|
this.route('GET','/sessions/me', req => { return this.handleMe(req); });
|
2016-02-19 23:47:44 -05:00
|
|
|
this.route('GET', '/sessions', req => { return this.handleFind(req); });
|
|
|
|
|
this.route('GET', '/sessions/:objectId', req => { return this.handleGet(req); });
|
|
|
|
|
this.route('POST', '/sessions', req => { return this.handleCreate(req); });
|
|
|
|
|
this.route('PUT', '/sessions/:objectId', req => { return this.handleUpdate(req); });
|
|
|
|
|
this.route('DELETE', '/sessions/:objectId', req => { return this.handleDelete(req); });
|
2016-11-24 15:47:41 -05:00
|
|
|
this.route('POST', '/upgradeToRevocableSession', req => { return this.handleUpdateToRevocableSession(req); })
|
2016-02-11 20:40:15 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default SessionsRouter;
|