1
1
mirror of https://github.com/kokonect-link/cherrypick synced 2025-01-09 19:34:00 +09:00
cherrypick/src/server/api/endpoints/auth/session/userkey.ts

74 lines
1.5 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
/**
* Module dependencies
*/
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';
import { pack } from '../../../../../models/user';
2016-12-29 07:49:51 +09:00
/**
* Generate a session
*
2017-03-01 17:37:01 +09:00
* @param {any} params
* @return {Promise<any>}
2016-12-29 07:49:51 +09:00
*/
2018-07-06 02:58:29 +09:00
export default (params: any) => new Promise(async (res, rej) => {
2018-03-29 14:48:47 +09:00
// Get 'appSecret' parameter
2018-05-02 18:06:16 +09:00
const [appSecret, appSecretErr] = $.str.get(params.appSecret);
2018-03-29 14:48:47 +09:00
if (appSecretErr) return rej('invalid appSecret param');
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Lookup app
const app = await App.findOne({
secret: appSecret
});
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
if (app == null) {
return rej('app not found');
}
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Get 'token' parameter
2018-05-02 18:06:16 +09:00
const [token, tokenErr] = $.str.get(params.token);
2017-03-04 04:28:38 +09:00
if (tokenErr) return rej('invalid token param');
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Fetch token
const session = await AuthSess
.findOne({
token: token,
2018-03-29 14:48:47 +09:00
appId: app._id
2017-03-04 04:28:38 +09:00
});
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
if (session === null) {
return rej('session not found');
}
2016-12-29 07:49:51 +09:00
2018-03-29 14:48:47 +09:00
if (session.userId == null) {
2017-03-04 04:28:38 +09:00
return rej('this session is not allowed yet');
}
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Lookup access token
const accessToken = await AccessToken.findOne({
2018-03-29 14:48:47 +09:00
appId: app._id,
userId: session.userId
2017-03-04 04:28:38 +09:00
});
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Delete session
2017-02-09 02:15:34 +09:00
2017-03-04 04:28:38 +09:00
/* https://github.com/Automattic/monk/issues/178
AuthSess.deleteOne({
_id: session._id
});
*/
AuthSess.remove({
_id: session._id
});
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Response
res({
2018-03-29 14:48:47 +09:00
accessToken: accessToken.token,
user: await pack(session.userId, null, {
2017-03-04 04:28:38 +09:00
detail: true
})
2016-12-29 07:49:51 +09:00
});
2017-03-04 04:28:38 +09:00
});