2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import rndstr from 'rndstr';
|
2017-01-06 11:50:46 +09:00
|
|
|
const crypto = require('crypto');
|
2017-03-09 03:50:09 +09:00
|
|
|
import $ from 'cafy';
|
2017-01-06 11:50:46 +09:00
|
|
|
import App from '../../models/app';
|
2016-12-29 07:49:51 +09:00
|
|
|
import AuthSess from '../../models/auth-session';
|
2017-01-06 12:09:57 +09:00
|
|
|
import AccessToken from '../../models/access-token';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-01-06 01:28:59 +09:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /auth/accept:
|
|
|
|
* post:
|
|
|
|
* summary: Accept a session
|
|
|
|
* parameters:
|
2017-01-06 15:13:46 +09:00
|
|
|
* - $ref: "#/parameters/NativeToken"
|
2017-03-01 17:37:01 +09:00
|
|
|
* -
|
2017-01-06 01:28:59 +09:00
|
|
|
* name: token
|
|
|
|
* description: Session Token
|
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
|
|
|
* responses:
|
|
|
|
* 204:
|
|
|
|
* description: OK
|
2017-03-01 17:37:01 +09:00
|
|
|
*
|
2017-01-06 01:28:59 +09:00
|
|
|
* default:
|
|
|
|
* description: Failed
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Error"
|
|
|
|
*/
|
|
|
|
|
2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Accept
|
|
|
|
*
|
2017-03-01 17:37:01 +09:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-29 07:49:51 +09:00
|
|
|
*/
|
2017-03-04 04:28:38 +09:00
|
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
2016-12-29 07:49:51 +09:00
|
|
|
// Get 'token' parameter
|
2017-03-09 03:50:09 +09:00
|
|
|
const [token, tokenErr] = $(params.token).string().$;
|
2017-03-03 19:39:41 +09:00
|
|
|
if (tokenErr) return rej('invalid token param');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
// Fetch token
|
|
|
|
const session = await AuthSess
|
2017-03-03 19:39:41 +09:00
|
|
|
.findOne({ token: token });
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
if (session === null) {
|
|
|
|
return rej('session not found');
|
|
|
|
}
|
|
|
|
|
2017-01-06 12:09:57 +09:00
|
|
|
// Generate access token
|
2017-03-03 19:39:41 +09:00
|
|
|
const accessToken = rndstr('a-zA-Z0-9', 32);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-01-06 12:09:57 +09:00
|
|
|
// Fetch exist access token
|
|
|
|
const exist = await AccessToken.findOne({
|
2016-12-29 07:49:51 +09:00
|
|
|
app_id: session.app_id,
|
|
|
|
user_id: user._id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (exist === null) {
|
2017-01-06 11:50:46 +09:00
|
|
|
// Lookup app
|
|
|
|
const app = await App.findOne({
|
2017-01-26 23:29:55 +09:00
|
|
|
_id: session.app_id
|
2017-01-06 11:50:46 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// Generate Hash
|
2017-02-08 22:43:46 +09:00
|
|
|
const sha256 = crypto.createHash('sha256');
|
2017-03-03 19:39:41 +09:00
|
|
|
sha256.update(accessToken + app.secret);
|
2017-02-08 22:43:46 +09:00
|
|
|
const hash = sha256.digest('hex');
|
2017-01-06 11:50:46 +09:00
|
|
|
|
2017-01-06 12:09:57 +09:00
|
|
|
// Insert access token doc
|
|
|
|
await AccessToken.insert({
|
2016-12-29 07:49:51 +09:00
|
|
|
created_at: new Date(),
|
|
|
|
app_id: session.app_id,
|
|
|
|
user_id: user._id,
|
2017-03-03 19:39:41 +09:00
|
|
|
token: accessToken,
|
2017-01-06 11:50:46 +09:00
|
|
|
hash: hash
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update session
|
2017-01-17 11:11:22 +09:00
|
|
|
await AuthSess.update(session._id, {
|
2017-02-09 01:34:22 +09:00
|
|
|
$set: {
|
|
|
|
user_id: user._id
|
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res();
|
|
|
|
});
|