2019-01-29 20:51:29 -06:00
|
|
|
import * as crypto from 'crypto';
|
2021-08-19 07:55:45 -05:00
|
|
|
import define from '../../define';
|
|
|
|
import { ApiError } from '../../error';
|
|
|
|
import { AuthSessions, AccessTokens, Apps } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
|
|
|
import { secureRndstr } from '@/misc/secure-rndstr';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-07-16 14:36:44 -05:00
|
|
|
export const meta = {
|
2019-02-22 20:20:58 -06:00
|
|
|
tags: ['auth'],
|
|
|
|
|
2022-01-18 07:27:10 -06:00
|
|
|
requireCredential: true,
|
2018-11-01 22:49:08 -05:00
|
|
|
|
|
|
|
secure: true,
|
|
|
|
|
2019-02-21 20:46:58 -06:00
|
|
|
errors: {
|
|
|
|
noSuchSession: {
|
|
|
|
message: 'No such session.',
|
|
|
|
code: 'NO_SUCH_SESSION',
|
2021-12-09 08:58:30 -06:00
|
|
|
id: '9c72d8de-391a-43c1-9d06-08d29efde8df',
|
2019-02-21 20:46:58 -06:00
|
|
|
},
|
2021-12-09 08:58:30 -06:00
|
|
|
},
|
2022-01-18 07:27:10 -06:00
|
|
|
} as const;
|
2018-07-16 14:36:44 -05:00
|
|
|
|
2022-02-18 23:05:32 -06:00
|
|
|
const paramDef = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
token: { type: 'string' },
|
|
|
|
},
|
|
|
|
required: ['token'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 11:12:50 -06:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-18 23:05:32 -06:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2016-12-28 16:49:51 -06:00
|
|
|
// Fetch token
|
2019-04-07 07:50:36 -05:00
|
|
|
const session = await AuthSessions
|
2018-11-01 22:49:08 -05:00
|
|
|
.findOne({ token: ps.token });
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (session == null) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new ApiError(meta.errors.noSuchSession);
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|
|
|
|
|
2017-01-05 21:09:57 -06:00
|
|
|
// Generate access token
|
2020-03-29 09:16:36 -05:00
|
|
|
const accessToken = secureRndstr(32, true);
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-01-05 21:09:57 -06:00
|
|
|
// Fetch exist access token
|
2019-04-07 07:50:36 -05:00
|
|
|
const exist = await AccessTokens.findOne({
|
2018-03-29 00:48:47 -05:00
|
|
|
appId: session.appId,
|
2019-04-07 07:50:36 -05:00
|
|
|
userId: user.id,
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (exist == null) {
|
2017-01-05 20:50:46 -06:00
|
|
|
// Lookup app
|
2021-02-13 00:33:38 -06:00
|
|
|
const app = await Apps.findOneOrFail(session.appId);
|
2017-01-05 20:50:46 -06:00
|
|
|
|
|
|
|
// Generate Hash
|
2017-02-08 07:43:46 -06:00
|
|
|
const sha256 = crypto.createHash('sha256');
|
2017-03-03 04:39:41 -06:00
|
|
|
sha256.update(accessToken + app.secret);
|
2017-02-08 07:43:46 -06:00
|
|
|
const hash = sha256.digest('hex');
|
2017-01-05 20:50:46 -06:00
|
|
|
|
2020-08-04 09:09:48 -05:00
|
|
|
const now = new Date();
|
|
|
|
|
2017-01-05 21:09:57 -06:00
|
|
|
// Insert access token doc
|
2021-03-21 07:27:09 -05:00
|
|
|
await AccessTokens.insert({
|
2019-04-07 07:50:36 -05:00
|
|
|
id: genId(),
|
2020-08-04 09:09:48 -05:00
|
|
|
createdAt: now,
|
|
|
|
lastUsedAt: now,
|
2018-03-29 00:48:47 -05:00
|
|
|
appId: session.appId,
|
2019-04-07 07:50:36 -05:00
|
|
|
userId: user.id,
|
2017-03-03 04:39:41 -06:00
|
|
|
token: accessToken,
|
2021-12-09 08:58:30 -06:00
|
|
|
hash: hash,
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update session
|
2019-04-07 07:50:36 -05:00
|
|
|
await AuthSessions.update(session.id, {
|
2021-12-09 08:58:30 -06:00
|
|
|
userId: user.id,
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|
2019-02-21 20:46:58 -06:00
|
|
|
});
|