2016-12-29 00:49:51 +02:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import * as uuid from 'uuid';
|
2017-03-08 20:50:09 +02:00
|
|
|
import $ from 'cafy';
|
2018-03-29 14:32:18 +03:00
|
|
|
import App from '../../../../../models/app';
|
|
|
|
import AuthSess from '../../../../../models/auth-session';
|
2018-03-28 19:20:40 +03:00
|
|
|
import config from '../../../../../conf';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2017-01-04 05:59:20 +02:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /auth/session/generate:
|
|
|
|
* post:
|
|
|
|
* summary: Generate a session
|
|
|
|
* parameters:
|
|
|
|
* -
|
2018-03-29 08:48:47 +03:00
|
|
|
* name: appSecret
|
2017-01-04 08:04:54 +02:00
|
|
|
* description: App Secret
|
2017-01-04 05:59:20 +02:00
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
2017-03-01 10:37:01 +02:00
|
|
|
*
|
2017-01-04 05:59:20 +02:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* token:
|
|
|
|
* type: string
|
2017-01-05 16:42:27 +02:00
|
|
|
* description: Session Token
|
2017-01-04 05:59:20 +02:00
|
|
|
* url:
|
|
|
|
* type: string
|
2017-01-05 16:42:27 +02:00
|
|
|
* description: Authentication form's URL
|
2017-01-05 17:39:56 +02:00
|
|
|
* default:
|
2017-01-04 05:59:20 +02:00
|
|
|
* description: Failed
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Error"
|
|
|
|
*/
|
|
|
|
|
2016-12-29 00:49:51 +02:00
|
|
|
/**
|
|
|
|
* Generate a session
|
|
|
|
*
|
2017-03-01 10:37:01 +02:00
|
|
|
* @param {any} params
|
|
|
|
* @return {Promise<any>}
|
2016-12-29 00:49:51 +02:00
|
|
|
*/
|
2017-03-03 21:28:38 +02:00
|
|
|
module.exports = (params) => new Promise(async (res, rej) => {
|
2018-03-29 08:48:47 +03:00
|
|
|
// Get 'appSecret' parameter
|
|
|
|
const [appSecret, appSecretErr] = $(params.appSecret).string().$;
|
|
|
|
if (appSecretErr) return rej('invalid appSecret param');
|
2016-12-29 00:49:51 +02: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 10:38:05 +02:00
|
|
|
const doc = await AuthSess.insert({
|
2018-03-29 08:48:47 +03:00
|
|
|
createdAt: new Date(),
|
|
|
|
appId: app._id,
|
2016-12-29 00:49:51 +02:00
|
|
|
token: token
|
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res({
|
|
|
|
token: doc.token,
|
|
|
|
url: `${config.auth_url}/${doc.token}`
|
|
|
|
});
|
|
|
|
});
|