2016-12-28 16:49:51 -06:00
|
|
|
import rndstr from 'rndstr';
|
2017-01-05 20:50:46 -06:00
|
|
|
const crypto = require('crypto');
|
2017-03-08 12:50:09 -06:00
|
|
|
import $ from 'cafy';
|
2018-03-29 06:32:18 -05:00
|
|
|
import App from '../../../../models/app';
|
|
|
|
import AuthSess from '../../../../models/auth-session';
|
|
|
|
import AccessToken from '../../../../models/access-token';
|
2018-06-17 19:54:53 -05:00
|
|
|
import { ILocalUser } from '../../../../models/user';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-01-05 10:28:59 -06:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /auth/accept:
|
2018-04-07 12:30:37 -05:00
|
|
|
* note:
|
2017-01-05 10:28:59 -06:00
|
|
|
* summary: Accept a session
|
|
|
|
* parameters:
|
2017-01-06 00:13:46 -06:00
|
|
|
* - $ref: "#/parameters/NativeToken"
|
2017-03-01 02:37:01 -06:00
|
|
|
* -
|
2017-01-05 10:28:59 -06:00
|
|
|
* name: token
|
|
|
|
* description: Session Token
|
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
|
|
|
* responses:
|
|
|
|
* 204:
|
|
|
|
* description: OK
|
2017-03-01 02:37:01 -06:00
|
|
|
*
|
2017-01-05 10:28:59 -06:00
|
|
|
* default:
|
|
|
|
* description: Failed
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Error"
|
|
|
|
*/
|
|
|
|
|
2016-12-28 16:49:51 -06:00
|
|
|
/**
|
|
|
|
* Accept
|
|
|
|
*/
|
2018-07-05 12:58:29 -05:00
|
|
|
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
|
2016-12-28 16:49:51 -06:00
|
|
|
// Get 'token' parameter
|
2018-05-02 04:06:16 -05:00
|
|
|
const [token, tokenErr] = $.str.get(params.token);
|
2017-03-03 04:39:41 -06:00
|
|
|
if (tokenErr) return rej('invalid token param');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
// Fetch token
|
|
|
|
const session = await AuthSess
|
2018-03-27 02:04:22 -05:00
|
|
|
.findOne({ token: token });
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
if (session === null) {
|
|
|
|
return rej('session not found');
|
|
|
|
}
|
|
|
|
|
2017-01-05 21:09:57 -06:00
|
|
|
// Generate access token
|
2017-03-03 04:39:41 -06:00
|
|
|
const accessToken = rndstr('a-zA-Z0-9', 32);
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-01-05 21:09:57 -06:00
|
|
|
// Fetch exist access token
|
|
|
|
const exist = await AccessToken.findOne({
|
2018-03-29 00:48:47 -05:00
|
|
|
appId: session.appId,
|
|
|
|
userId: user._id,
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
if (exist === null) {
|
2017-01-05 20:50:46 -06:00
|
|
|
// Lookup app
|
|
|
|
const app = await App.findOne({
|
2018-03-29 00:48:47 -05:00
|
|
|
_id: session.appId
|
2017-01-05 20:50:46 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Generate Hash
|
2017-02-08 07:43:46 -06:00
|
|
|
const sha256 = crypto.createHash('sha256');
|
2017-03-03 04:39:41 -06:00
|
|
|
sha256.update(accessToken + app.secret);
|
2017-02-08 07:43:46 -06:00
|
|
|
const hash = sha256.digest('hex');
|
2017-01-05 20:50:46 -06:00
|
|
|
|
2017-01-05 21:09:57 -06:00
|
|
|
// Insert access token doc
|
|
|
|
await AccessToken.insert({
|
2018-03-29 00:48:47 -05:00
|
|
|
createdAt: new Date(),
|
|
|
|
appId: session.appId,
|
|
|
|
userId: user._id,
|
2017-03-03 04:39:41 -06:00
|
|
|
token: accessToken,
|
2017-01-05 20:50:46 -06:00
|
|
|
hash: hash
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update session
|
2017-01-16 20:11:22 -06:00
|
|
|
await AuthSess.update(session._id, {
|
2017-02-08 10:34:22 -06:00
|
|
|
$set: {
|
2018-03-29 00:48:47 -05:00
|
|
|
userId: user._id
|
2017-02-08 10:34:22 -06:00
|
|
|
}
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res();
|
|
|
|
});
|