2016-12-28 23:49:51 +01:00
|
|
|
import rndstr from 'rndstr';
|
2019-01-30 03:51:29 +01:00
|
|
|
import * as crypto from 'crypto';
|
2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2018-03-29 13:32:18 +02:00
|
|
|
import App from '../../../../models/app';
|
|
|
|
import AuthSess from '../../../../models/auth-session';
|
|
|
|
import AccessToken from '../../../../models/access-token';
|
2018-11-02 05:47:44 +01:00
|
|
|
import define from '../../define';
|
2019-02-22 03:46:58 +01:00
|
|
|
import { ApiError } from '../../error';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
|
|
|
requireCredential: true,
|
2018-11-02 04:49:08 +01:00
|
|
|
|
|
|
|
secure: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
token: {
|
|
|
|
validator: $.str
|
|
|
|
}
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchSession: {
|
|
|
|
message: 'No such session.',
|
|
|
|
code: 'NO_SUCH_SESSION',
|
|
|
|
id: '9c72d8de-391a-43c1-9d06-08d29efde8df'
|
|
|
|
},
|
2018-11-02 04:49:08 +01:00
|
|
|
}
|
2018-07-16 21:36:44 +02:00
|
|
|
};
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps, user) => {
|
2016-12-28 23:49:51 +01:00
|
|
|
// Fetch token
|
|
|
|
const session = await AuthSess
|
2018-11-02 04:49:08 +01:00
|
|
|
.findOne({ token: ps.token });
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
if (session === null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchSession);
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 04:09:57 +01:00
|
|
|
// Generate access token
|
2017-03-03 11:39:41 +01:00
|
|
|
const accessToken = rndstr('a-zA-Z0-9', 32);
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-01-06 04:09:57 +01:00
|
|
|
// Fetch exist access token
|
|
|
|
const exist = await AccessToken.findOne({
|
2018-03-29 07:48:47 +02:00
|
|
|
appId: session.appId,
|
|
|
|
userId: user._id,
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (exist === null) {
|
2017-01-06 03:50:46 +01:00
|
|
|
// Lookup app
|
|
|
|
const app = await App.findOne({
|
2018-03-29 07:48:47 +02:00
|
|
|
_id: session.appId
|
2017-01-06 03:50:46 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Generate Hash
|
2017-02-08 14:43:46 +01:00
|
|
|
const sha256 = crypto.createHash('sha256');
|
2017-03-03 11:39:41 +01:00
|
|
|
sha256.update(accessToken + app.secret);
|
2017-02-08 14:43:46 +01:00
|
|
|
const hash = sha256.digest('hex');
|
2017-01-06 03:50:46 +01:00
|
|
|
|
2017-01-06 04:09:57 +01:00
|
|
|
// Insert access token doc
|
|
|
|
await AccessToken.insert({
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
appId: session.appId,
|
|
|
|
userId: user._id,
|
2017-03-03 11:39:41 +01:00
|
|
|
token: accessToken,
|
2017-01-06 03:50:46 +01:00
|
|
|
hash: hash
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update session
|
2017-01-17 03:11:22 +01:00
|
|
|
await AuthSess.update(session._id, {
|
2017-02-08 17:34:22 +01:00
|
|
|
$set: {
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: user._id
|
2017-02-08 17:34:22 +01:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
return;
|
|
|
|
});
|