2016-12-28 22:49:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import rndstr from 'rndstr';
|
2017-01-06 02:50:46 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
import App from '../../models/app';
|
2016-12-28 22:49:51 +00:00
|
|
|
import AuthSess from '../../models/auth-session';
|
2017-01-06 03:09:57 +00:00
|
|
|
import AccessToken from '../../models/access-token';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2017-01-05 16:28:59 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /auth/accept:
|
|
|
|
* post:
|
|
|
|
* summary: Accept a session
|
|
|
|
* parameters:
|
|
|
|
* - $ref: "#/parameters/ShouldSecureKey"
|
|
|
|
* -
|
|
|
|
* name: token
|
|
|
|
* description: Session Token
|
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
|
|
|
* responses:
|
|
|
|
* 204:
|
|
|
|
* description: OK
|
|
|
|
*
|
|
|
|
* default:
|
|
|
|
* description: Failed
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Error"
|
|
|
|
*/
|
|
|
|
|
2016-12-28 22:49:51 +00:00
|
|
|
/**
|
|
|
|
* Accept
|
|
|
|
*
|
|
|
|
* @param {Object} params
|
|
|
|
* @param {Object} user
|
|
|
|
* @return {Promise<object>}
|
|
|
|
*/
|
|
|
|
module.exports = (params, user) =>
|
|
|
|
new Promise(async (res, rej) =>
|
|
|
|
{
|
|
|
|
// Get 'token' parameter
|
2017-01-06 03:09:57 +00:00
|
|
|
const sesstoken = params.token;
|
|
|
|
if (sesstoken == null) {
|
2016-12-28 22:49:51 +00:00
|
|
|
return rej('token is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch token
|
|
|
|
const session = await AuthSess
|
2017-01-06 03:09:57 +00:00
|
|
|
.findOne({ token: sesstoken });
|
2016-12-28 22:49:51 +00:00
|
|
|
|
|
|
|
if (session === null) {
|
|
|
|
return rej('session not found');
|
|
|
|
}
|
|
|
|
|
2017-01-06 03:09:57 +00:00
|
|
|
// Generate access token
|
|
|
|
const token = rndstr('a-zA-Z0-9', 32);
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2017-01-06 03:09:57 +00:00
|
|
|
// Fetch exist access token
|
|
|
|
const exist = await AccessToken.findOne({
|
2016-12-28 22:49:51 +00:00
|
|
|
app_id: session.app_id,
|
|
|
|
user_id: user._id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (exist === null) {
|
2017-01-06 02:50:46 +00:00
|
|
|
// Lookup app
|
|
|
|
const app = await App.findOne({
|
|
|
|
app_id: session.app_id
|
|
|
|
});
|
|
|
|
|
|
|
|
// Generate Hash
|
|
|
|
const sha512 = crypto.createHash('sha512');
|
2017-01-06 03:09:57 +00:00
|
|
|
sha512.update(token + app.secret);
|
2017-01-06 02:50:46 +00:00
|
|
|
const hash = sha512.digest('hex');
|
|
|
|
|
2017-01-06 03:09:57 +00:00
|
|
|
// Insert access token doc
|
|
|
|
await AccessToken.insert({
|
2016-12-28 22:49:51 +00:00
|
|
|
created_at: new Date(),
|
|
|
|
app_id: session.app_id,
|
|
|
|
user_id: user._id,
|
2017-01-06 03:09:57 +00:00
|
|
|
token: token,
|
2017-01-06 02:50:46 +00:00
|
|
|
hash: hash
|
2016-12-28 22:49:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update session
|
|
|
|
await AuthSess.updateOne({
|
|
|
|
_id: session._id
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
user_id: user._id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res();
|
|
|
|
});
|