misskey/src/api/endpoints/auth/session/generate.ts

77 lines
1.6 KiB
TypeScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
/**
* Module dependencies
*/
import * as uuid from 'uuid';
2017-03-03 11:39:41 +01:00
import it from '../../../it';
2016-12-28 23:49:51 +01:00
import App from '../../../models/app';
import AuthSess from '../../../models/auth-session';
2017-01-26 15:25:12 +01:00
import config from '../../../../conf';
2016-12-28 23:49:51 +01:00
2017-01-04 04:59:20 +01:00
/**
* @swagger
* /auth/session/generate:
* post:
* summary: Generate a session
* parameters:
* -
* name: app_secret
* description: App Secret
2017-01-04 04:59:20 +01:00
* in: formData
* required: true
* type: string
2017-03-01 09:37:01 +01:00
*
2017-01-04 04:59:20 +01:00
* responses:
* 200:
* description: OK
* schema:
* type: object
* properties:
* token:
* type: string
2017-01-05 15:42:27 +01:00
* description: Session Token
2017-01-04 04:59:20 +01:00
* url:
* type: string
2017-01-05 15:42:27 +01:00
* description: Authentication form's URL
2017-01-05 16:39:56 +01:00
* default:
2017-01-04 04:59:20 +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) => {
2016-12-28 23:49:51 +01:00
// Get 'app_secret' parameter
2017-03-03 11:39:41 +01:00
const [appSecret, appSecretErr] = it(params.app_secret).expect.string().required().qed();
if (appSecretErr) return rej('invalid app_secret param');
2016-12-28 23:49:51 +01:00
// Lookup app
const app = await App.findOne({
secret: appSecret
});
if (app == null) {
return rej('app not found');
}
// Generate token
const token = uuid.v4();
// Create session token document
2017-01-20 09:38:05 +01:00
const doc = await AuthSess.insert({
2016-12-28 23:49:51 +01:00
created_at: new Date(),
app_id: app._id,
token: token
});
// Response
res({
token: doc.token,
url: `${config.auth_url}/${doc.token}`
});
});