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

46 lines
933 B
TypeScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
/**
* Module dependencies
*/
import * as uuid from 'uuid';
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';
2018-04-02 06:15:53 +02:00
import config from '../../../../../config';
2016-12-28 23:49:51 +01:00
/**
* Generate a session
*
2017-03-01 09:37:01 +01:00
* @param {any} params
* @return {Promise<any>}
2016-12-28 23:49:51 +01:00
*/
2018-07-05 19:58:29 +02:00
export default (params: any) => new Promise(async (res, rej) => {
2018-03-29 07:48:47 +02:00
// Get 'appSecret' parameter
2018-05-02 11:06:16 +02:00
const [appSecret, appSecretErr] = $.str.get(params.appSecret);
2018-03-29 07:48:47 +02:00
if (appSecretErr) return rej('invalid appSecret param');
2016-12-28 23:49:51 +01:00
// Lookup app
const app = await App.findOne({
secret: appSecret
});
if (app == null) {
return rej('app not found');
}
// Generate token
const token = uuid.v4();
// Create session token document
2017-01-20 09:38:05 +01:00
const doc = await AuthSess.insert({
2018-03-29 07:48:47 +02:00
createdAt: new Date(),
appId: app._id,
2016-12-28 23:49:51 +01:00
token: token
});
// Response
res({
token: doc.token,
url: `${config.auth_url}/${doc.token}`
});
});