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-11-02 05:47:44 +01:00
|
|
|
import define from '../../define';
|
2019-02-22 03:46:58 +01:00
|
|
|
import { ApiError } from '../../error';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { AuthSessions, AccessTokens, Apps } from '../../../../models';
|
|
|
|
import { genId } from '../../../../misc/gen-id';
|
2019-04-12 18:43:22 +02:00
|
|
|
import { ensure } from '../../../../prelude/ensure';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['auth'],
|
|
|
|
|
2020-02-15 13:33:32 +01:00
|
|
|
requireCredential: true as const,
|
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
|
2019-04-07 14:50:36 +02:00
|
|
|
const session = await AuthSessions
|
2018-11-02 04:49:08 +01:00
|
|
|
.findOne({ token: ps.token });
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-04-07 14:50:36 +02: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
|
2019-04-15 18:05:21 +02: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
|
2019-04-07 14:50:36 +02:00
|
|
|
const exist = await AccessTokens.findOne({
|
2018-03-29 07:48:47 +02:00
|
|
|
appId: session.appId,
|
2019-04-07 14:50:36 +02:00
|
|
|
userId: user.id,
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (exist == null) {
|
2017-01-06 03:50:46 +01:00
|
|
|
// Lookup app
|
2019-04-12 18:43:22 +02:00
|
|
|
const app = await Apps.findOne(session.appId).then(ensure);
|
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
|
2019-04-07 14:50:36 +02:00
|
|
|
await AccessTokens.save({
|
|
|
|
id: genId(),
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
appId: session.appId,
|
2019-04-07 14:50:36 +02:00
|
|
|
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
|
2019-04-07 14:50:36 +02:00
|
|
|
await AuthSessions.update(session.id, {
|
|
|
|
userId: user.id
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|