2016-12-29 07:49:51 +09:00
|
|
|
import * as uuid from 'uuid';
|
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';
|
2018-04-02 13:15:53 +09:00
|
|
|
import config from '../../../../../config';
|
2018-11-02 12:49:08 +09:00
|
|
|
import getParams from '../../../get-params';
|
|
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
|
|
params: {
|
|
|
|
|
appSecret: {
|
|
|
|
|
validator: $.str
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
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-11-02 12:49:08 +09:00
|
|
|
const [ps, psErr] = getParams(meta, params);
|
|
|
|
|
if (psErr) return rej(psErr);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
|
// Lookup app
|
|
|
|
|
const app = await App.findOne({
|
2018-11-02 12:49:08 +09:00
|
|
|
secret: ps.appSecret
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (app == null) {
|
|
|
|
|
return rej('app not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate token
|
|
|
|
|
const token = uuid.v4();
|
|
|
|
|
|
|
|
|
|
// Create session token document
|
2017-01-20 17:38:05 +09:00
|
|
|
const doc = await AuthSess.insert({
|
2018-03-29 14:48:47 +09:00
|
|
|
createdAt: new Date(),
|
|
|
|
|
appId: app._id,
|
2016-12-29 07:49:51 +09:00
|
|
|
token: token
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Response
|
|
|
|
|
res({
|
|
|
|
|
token: doc.token,
|
|
|
|
|
url: `${config.auth_url}/${doc.token}`
|
|
|
|
|
});
|
|
|
|
|
});
|