1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-28 21:48:26 +09:00
misskey/src/server/api/endpoints/auth/accept.ts

70 lines
1.3 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
import rndstr from 'rndstr';
2017-01-06 11:50:46 +09:00
const crypto = require('crypto');
2017-03-09 03:50:09 +09:00
import $ from 'cafy';
2018-03-29 20:32:18 +09:00
import App from '../../../../models/app';
import AuthSess from '../../../../models/auth-session';
import AccessToken from '../../../../models/access-token';
2018-11-02 13:47:44 +09:00
import define from '../../define';
2016-12-29 07:49:51 +09:00
2018-07-17 04:36:44 +09:00
export const meta = {
requireCredential: true,
2018-11-02 12:49:08 +09:00
secure: true,
params: {
token: {
validator: $.str
}
}
2018-07-17 04:36:44 +09:00
};
2018-11-02 13:47:44 +09:00
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
2016-12-29 07:49:51 +09:00
// Fetch token
const session = await AuthSess
2018-11-02 12:49:08 +09:00
.findOne({ token: ps.token });
2016-12-29 07:49:51 +09:00
if (session === null) {
return rej('session not found');
}
// Generate access token
2017-03-03 19:39:41 +09:00
const accessToken = rndstr('a-zA-Z0-9', 32);
2016-12-29 07:49:51 +09:00
// Fetch exist access token
const exist = await AccessToken.findOne({
2018-03-29 14:48:47 +09:00
appId: session.appId,
userId: user._id,
2016-12-29 07:49:51 +09:00
});
if (exist === null) {
2017-01-06 11:50:46 +09:00
// Lookup app
const app = await App.findOne({
2018-03-29 14:48:47 +09:00
_id: session.appId
2017-01-06 11:50:46 +09:00
});
// Generate Hash
2017-02-08 22:43:46 +09:00
const sha256 = crypto.createHash('sha256');
2017-03-03 19:39:41 +09:00
sha256.update(accessToken + app.secret);
2017-02-08 22:43:46 +09:00
const hash = sha256.digest('hex');
2017-01-06 11:50:46 +09:00
// Insert access token doc
await AccessToken.insert({
2018-03-29 14:48:47 +09:00
createdAt: new Date(),
appId: session.appId,
userId: user._id,
2017-03-03 19:39:41 +09:00
token: accessToken,
2017-01-06 11:50:46 +09:00
hash: hash
2016-12-29 07:49:51 +09:00
});
}
// Update session
2017-01-17 11:11:22 +09:00
await AuthSess.update(session._id, {
2017-02-09 01:34:22 +09:00
$set: {
2018-03-29 14:48:47 +09:00
userId: user._id
2017-02-09 01:34:22 +09:00
}
2016-12-29 07:49:51 +09:00
});
// Response
res();
2018-11-02 13:47:44 +09:00
}));