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 { Apps, AuthSessions, AccessTokens, Users } from '../../../../../models';
|
2019-04-12 18:43:22 +02:00
|
|
|
import { ensure } from '../../../../../prelude/ensure';
|
2018-11-02 04:49:08 +01:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['auth'],
|
|
|
|
|
2018-11-02 04:49:08 +01:00
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
appSecret: {
|
2019-02-24 04:40:17 +01:00
|
|
|
validator: $.str,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'アプリケーションのシークレットキー',
|
|
|
|
'en-US': 'The secret key of your application.'
|
|
|
|
}
|
2018-11-02 04:49:08 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
token: {
|
2019-02-24 04:40:17 +01:00
|
|
|
validator: $.str,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'セッションのトークン',
|
|
|
|
'en-US': 'The token of a session.'
|
|
|
|
}
|
2018-11-02 04:49:08 +01:00
|
|
|
}
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
2019-02-24 20:18:09 +01:00
|
|
|
res: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
accessToken: {
|
|
|
|
type: 'string',
|
|
|
|
description: 'ユーザーのアクセストークン',
|
|
|
|
},
|
|
|
|
|
|
|
|
user: {
|
|
|
|
type: 'User',
|
|
|
|
description: '認証したユーザー'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01: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 04:49:08 +01:00
|
|
|
}
|
|
|
|
};
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps) => {
|
2017-03-03 20:28:38 +01:00
|
|
|
// Lookup app
|
2019-04-07 14:50:36 +02:00
|
|
|
const app = await Apps.findOne({
|
2018-11-02 04:49:08 +01:00
|
|
|
secret: ps.appSecret
|
2017-03-03 20:28:38 +01:00
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
if (app == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchApp);
|
2017-03-03 20:28:38 +01:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Fetch token
|
2019-04-07 14:50:36 +02:00
|
|
|
const session = await AuthSessions.findOne({
|
|
|
|
token: ps.token,
|
|
|
|
appId: app.id
|
|
|
|
});
|
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);
|
2017-03-03 20:28:38 +01:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (session.userId == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.pendingSession);
|
2017-03-03 20:28:38 +01:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Lookup access token
|
2019-04-07 14:50:36 +02:00
|
|
|
const accessToken = await AccessTokens.findOne({
|
|
|
|
appId: app.id,
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: session.userId
|
2019-04-12 18:43:22 +02:00
|
|
|
}).then(ensure);
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Delete session
|
2019-04-07 14:50:36 +02:00
|
|
|
AuthSessions.delete(session.id);
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
return {
|
2018-03-29 07:48:47 +02:00
|
|
|
accessToken: accessToken.token,
|
2019-04-07 14:50:36 +02:00
|
|
|
user: await Users.pack(session.userId, null, {
|
2017-03-03 20:28:38 +01:00
|
|
|
detail: true
|
|
|
|
})
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|
|
|
|
});
|