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';
|
2018-11-02 13:47:44 +09:00
|
|
|
import define from '../../../define';
|
2019-02-22 11:46:58 +09:00
|
|
|
import { ApiError } from '../../../error';
|
2018-11-02 12:49:08 +09:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
appSecret: {
|
|
|
|
validator: $.str
|
|
|
|
},
|
|
|
|
|
|
|
|
token: {
|
|
|
|
validator: $.str
|
|
|
|
}
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchApp: {
|
|
|
|
message: 'No such app.',
|
|
|
|
code: 'NO_SUCH_APP',
|
|
|
|
id: 'fcab192a-2c5a-43b7-8ad8-9b7054d8d40d'
|
|
|
|
},
|
|
|
|
|
|
|
|
noSuchSession: {
|
|
|
|
message: 'No such session.',
|
|
|
|
code: 'NO_SUCH_SESSION',
|
|
|
|
id: '5b5a1503-8bc8-4bd0-8054-dc189e8cdcb3'
|
|
|
|
},
|
|
|
|
|
|
|
|
pendingSession: {
|
|
|
|
message: 'This session is not completed yet.',
|
|
|
|
code: 'PENDING_SESSION',
|
|
|
|
id: '8c8a4145-02cc-4cca-8e66-29ba60445a8e'
|
|
|
|
}
|
2018-11-02 12:49:08 +09:00
|
|
|
}
|
|
|
|
};
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps) => {
|
2017-03-04 04:28:38 +09:00
|
|
|
// Lookup app
|
|
|
|
const app = await App.findOne({
|
2018-11-02 12:49:08 +09:00
|
|
|
secret: ps.appSecret
|
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 (app == null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.noSuchApp);
|
2017-03-04 04:28:38 +09:00
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-03-04 04:28:38 +09:00
|
|
|
// Fetch token
|
|
|
|
const session = await AuthSess
|
|
|
|
.findOne({
|
2018-11-02 12:49:08 +09:00
|
|
|
token: ps.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) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.noSuchSession);
|
2017-03-04 04:28:38 +09:00
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-03-29 14:48:47 +09:00
|
|
|
if (session.userId == null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.pendingSession);
|
2017-03-04 04:28:38 +09:00
|
|
|
}
|
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
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
return {
|
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
|
|
|
|
})
|
2019-02-22 11:46:58 +09:00
|
|
|
};
|
|
|
|
});
|