2016-12-28 23:49:51 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-05 04:00:39 +01:00
|
|
|
import it from 'cafy';
|
2016-12-28 23:49:51 +01:00
|
|
|
import App from '../../../models/app';
|
|
|
|
import AuthSess from '../../../models/auth-session';
|
2017-01-06 04:09:57 +01:00
|
|
|
import AccessToken from '../../../models/access-token';
|
2016-12-28 23:49:51 +01:00
|
|
|
import serialize from '../../../serializers/user';
|
|
|
|
|
2017-01-04 07:04:54 +01:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /auth/session/userkey:
|
|
|
|
* post:
|
2017-02-23 16:28:43 +01:00
|
|
|
* summary: Get an access token(userkey)
|
2017-01-04 07:04:54 +01:00
|
|
|
* parameters:
|
|
|
|
* -
|
|
|
|
* name: app_secret
|
|
|
|
* description: App Secret
|
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
|
|
|
* -
|
|
|
|
* name: token
|
2017-01-05 15:42:27 +01:00
|
|
|
* description: Session Token
|
2017-01-04 07:04:54 +01:00
|
|
|
* in: formData
|
|
|
|
* required: true
|
|
|
|
* type: string
|
2017-02-23 16:28:18 +01:00
|
|
|
*
|
2017-01-04 07:04:54 +01:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* userkey:
|
|
|
|
* type: string
|
2017-01-06 07:13:46 +01:00
|
|
|
* description: Access Token
|
2017-01-04 07:04:54 +01:00
|
|
|
* user:
|
|
|
|
* $ref: "#/definitions/User"
|
2017-01-05 16:39:56 +01:00
|
|
|
* default:
|
2017-01-04 07:04:54 +01:00
|
|
|
* description: Failed
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/definitions/Error"
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
*/
|
2017-03-03 20:28:38 +01:00
|
|
|
module.exports = (params) => new Promise(async (res, rej) => {
|
|
|
|
// Get 'app_secret' parameter
|
2017-03-05 04:00:39 +01:00
|
|
|
const [appSecret, appSecretErr] = it(params.app_secret).expect.string().required().get();
|
2017-03-03 20:28:38 +01:00
|
|
|
if (appSecretErr) return rej('invalid app_secret param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Lookup app
|
|
|
|
const app = await App.findOne({
|
|
|
|
secret: appSecret
|
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
if (app == null) {
|
|
|
|
return rej('app not found');
|
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Get 'token' parameter
|
2017-03-05 04:00:39 +01:00
|
|
|
const [token, tokenErr] = it(params.token).expect.string().required().get();
|
2017-03-03 20:28:38 +01:00
|
|
|
if (tokenErr) return rej('invalid token param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Fetch token
|
|
|
|
const session = await AuthSess
|
|
|
|
.findOne({
|
|
|
|
token: token,
|
|
|
|
app_id: app._id
|
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
if (session === null) {
|
|
|
|
return rej('session not found');
|
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
if (session.user_id == null) {
|
|
|
|
return rej('this session is not allowed yet');
|
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Lookup access token
|
|
|
|
const accessToken = await AccessToken.findOne({
|
|
|
|
app_id: app._id,
|
|
|
|
user_id: session.user_id
|
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Delete session
|
2017-02-08 18:15:34 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
/* https://github.com/Automattic/monk/issues/178
|
|
|
|
AuthSess.deleteOne({
|
|
|
|
_id: session._id
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
AuthSess.remove({
|
|
|
|
_id: session._id
|
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-03 20:28:38 +01:00
|
|
|
// Response
|
|
|
|
res({
|
|
|
|
access_token: accessToken.token,
|
|
|
|
user: await serialize(session.user_id, null, {
|
|
|
|
detail: true
|
|
|
|
})
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
2017-03-03 20:28:38 +01:00
|
|
|
});
|