2018-07-02 23:30:14 -04:00
|
|
|
const Config = require('../lib/Config');
|
2016-09-09 14:48:06 -04:00
|
|
|
const sessionToken = 'legacySessionToken';
|
2018-09-24 17:07:51 -04:00
|
|
|
const request = require('../lib/request');
|
2016-09-09 14:48:06 -04:00
|
|
|
const Parse = require('parse/node');
|
|
|
|
|
|
|
|
|
|
function createUser() {
|
2017-10-23 08:43:05 -04:00
|
|
|
const config = Config.get(Parse.applicationId);
|
2016-09-09 14:48:06 -04:00
|
|
|
const user = {
|
|
|
|
|
objectId: '1234567890',
|
|
|
|
|
username: 'hello',
|
|
|
|
|
password: 'pass',
|
2018-09-01 13:58:06 -04:00
|
|
|
_session_token: sessionToken,
|
|
|
|
|
};
|
2016-09-09 14:48:06 -04:00
|
|
|
return config.database.create('_User', user);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-09 17:02:15 -04:00
|
|
|
describe_only_db('mongo')('revocable sessions', () => {
|
2018-09-01 13:58:06 -04:00
|
|
|
beforeEach(done => {
|
2016-09-09 14:48:06 -04:00
|
|
|
// Create 1 user with the legacy
|
|
|
|
|
createUser().then(done);
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-24 15:47:41 -05:00
|
|
|
it('should upgrade legacy session token', done => {
|
2016-12-07 15:17:05 -08:00
|
|
|
const user = Parse.Object.fromJSON({
|
2016-09-09 14:48:06 -04:00
|
|
|
className: '_User',
|
|
|
|
|
objectId: '1234567890',
|
2018-09-01 13:58:06 -04:00
|
|
|
sessionToken: sessionToken,
|
2016-09-09 14:48:06 -04:00
|
|
|
});
|
2018-09-01 13:58:06 -04:00
|
|
|
user
|
|
|
|
|
._upgradeToRevocableSession()
|
|
|
|
|
.then(res => {
|
|
|
|
|
expect(res.getSessionToken().indexOf('r:')).toBe(0);
|
|
|
|
|
const config = Config.get(Parse.applicationId);
|
|
|
|
|
// use direct access to the DB to make sure we're not
|
|
|
|
|
// getting the session token stripped
|
|
|
|
|
return config.database
|
|
|
|
|
.loadSchema()
|
|
|
|
|
.then(schemaController => {
|
|
|
|
|
return schemaController.getOneSchema('_User', true);
|
|
|
|
|
})
|
|
|
|
|
.then(schema => {
|
2020-10-25 15:06:58 -05:00
|
|
|
return config.database.adapter.find('_User', schema, { objectId: '1234567890' }, {});
|
2018-09-01 13:58:06 -04:00
|
|
|
})
|
|
|
|
|
.then(results => {
|
|
|
|
|
expect(results.length).toBe(1);
|
|
|
|
|
expect(results[0].sessionToken).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(
|
|
|
|
|
() => {
|
|
|
|
|
done();
|
|
|
|
|
},
|
|
|
|
|
err => {
|
|
|
|
|
jfail(err);
|
|
|
|
|
done();
|
|
|
|
|
}
|
|
|
|
|
);
|
2016-09-09 14:48:06 -04:00
|
|
|
});
|
|
|
|
|
|
2016-11-24 15:47:41 -05:00
|
|
|
it('should be able to become with revocable session token', done => {
|
2016-12-07 15:17:05 -08:00
|
|
|
const user = Parse.Object.fromJSON({
|
2016-09-09 14:48:06 -04:00
|
|
|
className: '_User',
|
|
|
|
|
objectId: '1234567890',
|
2018-09-01 13:58:06 -04:00
|
|
|
sessionToken: sessionToken,
|
2016-09-09 14:48:06 -04:00
|
|
|
});
|
2018-09-01 13:58:06 -04:00
|
|
|
user
|
|
|
|
|
._upgradeToRevocableSession()
|
|
|
|
|
.then(res => {
|
|
|
|
|
expect(res.getSessionToken().indexOf('r:')).toBe(0);
|
|
|
|
|
return Parse.User.logOut()
|
|
|
|
|
.then(() => {
|
|
|
|
|
return Parse.User.become(res.getSessionToken());
|
|
|
|
|
})
|
|
|
|
|
.then(user => {
|
|
|
|
|
expect(user.id).toEqual('1234567890');
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(
|
|
|
|
|
() => {
|
|
|
|
|
done();
|
|
|
|
|
},
|
|
|
|
|
err => {
|
|
|
|
|
jfail(err);
|
|
|
|
|
done();
|
|
|
|
|
}
|
|
|
|
|
);
|
2016-09-09 14:48:06 -04:00
|
|
|
});
|
|
|
|
|
|
2016-11-24 15:47:41 -05:00
|
|
|
it('should not upgrade bad legacy session token', done => {
|
2018-09-24 17:07:51 -04:00
|
|
|
request({
|
|
|
|
|
method: 'POST',
|
2017-01-11 12:31:40 -08:00
|
|
|
url: Parse.serverURL + '/upgradeToRevocableSession',
|
2016-09-09 14:48:06 -04:00
|
|
|
headers: {
|
|
|
|
|
'X-Parse-Application-Id': Parse.applicationId,
|
|
|
|
|
'X-Parse-Rest-API-Key': 'rest',
|
2018-09-01 13:58:06 -04:00
|
|
|
'X-Parse-Session-Token': 'badSessionToken',
|
2016-09-09 14:48:06 -04:00
|
|
|
},
|
2018-09-01 13:58:06 -04:00
|
|
|
})
|
|
|
|
|
.then(
|
|
|
|
|
() => {
|
|
|
|
|
fail('should not be able to upgrade a bad token');
|
|
|
|
|
},
|
|
|
|
|
response => {
|
2018-09-24 17:07:51 -04:00
|
|
|
expect(response.status).toBe(400);
|
|
|
|
|
expect(response.data).not.toBeUndefined();
|
|
|
|
|
expect(response.data.code).toBe(Parse.Error.INVALID_SESSION_TOKEN);
|
|
|
|
|
expect(response.data.error).toEqual('invalid legacy session token');
|
2018-09-01 13:58:06 -04:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.then(() => {
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-09-09 14:48:06 -04:00
|
|
|
});
|
2016-09-17 15:52:52 -04:00
|
|
|
|
2016-11-24 15:47:41 -05:00
|
|
|
it('should not crash without session token #2720', done => {
|
2018-09-24 17:07:51 -04:00
|
|
|
request({
|
|
|
|
|
method: 'POST',
|
2017-01-11 12:31:40 -08:00
|
|
|
url: Parse.serverURL + '/upgradeToRevocableSession',
|
2016-09-17 15:52:52 -04:00
|
|
|
headers: {
|
|
|
|
|
'X-Parse-Application-Id': Parse.applicationId,
|
2018-09-01 13:58:06 -04:00
|
|
|
'X-Parse-Rest-API-Key': 'rest',
|
2016-09-17 15:52:52 -04:00
|
|
|
},
|
2018-09-01 13:58:06 -04:00
|
|
|
})
|
|
|
|
|
.then(
|
|
|
|
|
() => {
|
|
|
|
|
fail('should not be able to upgrade a bad token');
|
|
|
|
|
},
|
|
|
|
|
response => {
|
2018-09-24 17:07:51 -04:00
|
|
|
expect(response.status).toBe(404);
|
|
|
|
|
expect(response.data).not.toBeUndefined();
|
|
|
|
|
expect(response.data.code).toBe(Parse.Error.OBJECT_NOT_FOUND);
|
|
|
|
|
expect(response.data.error).toEqual('invalid session');
|
2018-09-01 13:58:06 -04:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.then(() => {
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-09-17 15:52:52 -04:00
|
|
|
});
|
2018-09-01 13:58:06 -04:00
|
|
|
});
|