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