2016-12-28 23:49:51 +01:00
|
|
|
import * as uuid from 'uuid';
|
2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2018-04-02 06:15:53 +02:00
|
|
|
import config from '../../../../../config';
|
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 } from '../../../../../models';
|
|
|
|
import { genId } from '../../../../../misc/gen-id';
|
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,
|
2019-04-23 15:35:26 +02:00
|
|
|
|
2019-04-15 16:26:20 +02:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'アプリを認証するためのトークンを作成します。',
|
|
|
|
'en-US': 'Generate a token for authorize application.'
|
|
|
|
},
|
2018-11-02 04:49:08 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
2019-02-24 20:18:09 +01:00
|
|
|
res: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-24 20:18:09 +01:00
|
|
|
properties: {
|
|
|
|
token: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-24 20:18:09 +01:00
|
|
|
description: 'セッションのトークン'
|
|
|
|
},
|
|
|
|
url: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
format: 'url',
|
2019-02-24 20:18:09 +01:00
|
|
|
description: 'セッションのURL'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
noSuchApp: {
|
|
|
|
message: 'No such app.',
|
|
|
|
code: 'NO_SUCH_APP',
|
|
|
|
id: '92f93e63-428e-4f2f-a5a4-39e1407fe998'
|
|
|
|
}
|
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) => {
|
2016-12-28 23:49:51 +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
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (app == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchApp);
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate token
|
|
|
|
const token = uuid.v4();
|
|
|
|
|
|
|
|
// Create session token document
|
2019-04-07 14:50:36 +02:00
|
|
|
const doc = await AuthSessions.save({
|
|
|
|
id: genId(),
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: new Date(),
|
2019-04-07 14:50:36 +02:00
|
|
|
appId: app.id,
|
2016-12-28 23:49:51 +01:00
|
|
|
token: token
|
|
|
|
});
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
return {
|
2016-12-28 23:49:51 +01:00
|
|
|
token: doc.token,
|
2019-02-24 04:53:22 +01:00
|
|
|
url: `${config.authUrl}/${doc.token}`
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|
|
|
|
});
|