iceshrimp-legacy/src/server/api/endpoints/auth/session/userkey.ts

117 lines
2.2 KiB
TypeScript
Raw Normal View History

2017-03-08 19:50:09 +01:00
import $ from 'cafy';
2018-03-29 13:32:18 +02: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 05:47:44 +01:00
import define from '../../../define';
import { ApiError } from '../../../error';
2018-11-02 04:49:08 +01:00
export const meta = {
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-24 20:18:09 +01:00
res: {
type: 'object',
properties: {
accessToken: {
type: 'string',
description: 'ユーザーのアクセストークン',
},
user: {
type: 'User',
description: '認証したユーザー'
},
}
},
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
export default define(meta, async (ps) => {
2017-03-03 20:28:38 +01:00
// Lookup app
const app = await App.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) {
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
const session = await AuthSess
.findOne({
2018-11-02 04:49:08 +01:00
token: ps.token,
2018-03-29 07:48:47 +02:00
appId: app._id
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 (session === null) {
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) {
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
const accessToken = await AccessToken.findOne({
2018-03-29 07:48:47 +02:00
appId: app._id,
userId: session.userId
2017-03-03 20:28:38 +01:00
});
2016-12-28 23:49:51 +01:00
2017-03-03 20:28:38 +01:00
// Delete session
2017-02-08 18:15:34 +01:00
2017-03-03 20:28:38 +01:00
/* https://github.com/Automattic/monk/issues/178
AuthSess.deleteOne({
_id: session._id
});
*/
AuthSess.remove({
_id: session._id
});
2016-12-28 23:49:51 +01:00
return {
2018-03-29 07:48:47 +02:00
accessToken: accessToken.token,
user: await pack(session.userId, null, {
2017-03-03 20:28:38 +01:00
detail: true
})
};
});