summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/app
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/api/endpoints/app')
-rw-r--r--src/server/api/endpoints/app/create.ts108
-rw-r--r--src/server/api/endpoints/app/name_id/available.ts60
-rw-r--r--src/server/api/endpoints/app/show.ts68
3 files changed, 236 insertions, 0 deletions
diff --git a/src/server/api/endpoints/app/create.ts b/src/server/api/endpoints/app/create.ts
new file mode 100644
index 0000000000..4a55d33f2d
--- /dev/null
+++ b/src/server/api/endpoints/app/create.ts
@@ -0,0 +1,108 @@
+/**
+ * Module dependencies
+ */
+import rndstr from 'rndstr';
+import $ from 'cafy';
+import App, { isValidNameId, pack } from '../../../../models/app';
+
+/**
+ * @swagger
+ * /app/create:
+ * note:
+ * summary: Create an application
+ * parameters:
+ * - $ref: "#/parameters/AccessToken"
+ * -
+ * name: nameId
+ * description: Application unique name
+ * in: formData
+ * required: true
+ * type: string
+ * -
+ * name: name
+ * description: Application name
+ * in: formData
+ * required: true
+ * type: string
+ * -
+ * name: description
+ * description: Application description
+ * in: formData
+ * required: true
+ * type: string
+ * -
+ * name: permission
+ * description: Permissions that application has
+ * in: formData
+ * required: true
+ * type: array
+ * items:
+ * type: string
+ * collectionFormat: csv
+ * -
+ * name: callbackUrl
+ * description: URL called back after authentication
+ * in: formData
+ * required: false
+ * type: string
+ *
+ * responses:
+ * 200:
+ * description: Created application's information
+ * schema:
+ * $ref: "#/definitions/Application"
+ *
+ * default:
+ * description: Failed
+ * schema:
+ * $ref: "#/definitions/Error"
+ */
+
+/**
+ * Create an app
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'nameId' parameter
+ const [nameId, nameIdErr] = $(params.nameId).string().pipe(isValidNameId).$;
+ if (nameIdErr) return rej('invalid nameId param');
+
+ // Get 'name' parameter
+ const [name, nameErr] = $(params.name).string().$;
+ if (nameErr) return rej('invalid name param');
+
+ // Get 'description' parameter
+ const [description, descriptionErr] = $(params.description).string().$;
+ if (descriptionErr) return rej('invalid description param');
+
+ // Get 'permission' parameter
+ const [permission, permissionErr] = $(params.permission).array('string').unique().$;
+ if (permissionErr) return rej('invalid permission param');
+
+ // Get 'callbackUrl' parameter
+ // TODO: Check it is valid url
+ const [callbackUrl = null, callbackUrlErr] = $(params.callbackUrl).optional.nullable.string().$;
+ if (callbackUrlErr) return rej('invalid callbackUrl param');
+
+ // Generate secret
+ const secret = rndstr('a-zA-Z0-9', 32);
+
+ // Create account
+ const app = await App.insert({
+ createdAt: new Date(),
+ userId: user._id,
+ name: name,
+ nameId: nameId,
+ nameIdLower: nameId.toLowerCase(),
+ description: description,
+ permission: permission,
+ callbackUrl: callbackUrl,
+ secret: secret
+ });
+
+ // Response
+ res(await pack(app));
+});
diff --git a/src/server/api/endpoints/app/name_id/available.ts b/src/server/api/endpoints/app/name_id/available.ts
new file mode 100644
index 0000000000..ec2d692412
--- /dev/null
+++ b/src/server/api/endpoints/app/name_id/available.ts
@@ -0,0 +1,60 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import App from '../../../../../models/app';
+import { isValidNameId } from '../../../../../models/app';
+
+/**
+ * @swagger
+ * /app/nameId/available:
+ * note:
+ * summary: Check available nameId on creation an application
+ * parameters:
+ * -
+ * name: nameId
+ * description: Application unique name
+ * in: formData
+ * required: true
+ * type: string
+ *
+ * responses:
+ * 200:
+ * description: Success
+ * schema:
+ * type: object
+ * properties:
+ * available:
+ * description: Whether nameId is available
+ * type: boolean
+ *
+ * default:
+ * description: Failed
+ * schema:
+ * $ref: "#/definitions/Error"
+ */
+
+/**
+ * Check available nameId of app
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = async (params) => new Promise(async (res, rej) => {
+ // Get 'nameId' parameter
+ const [nameId, nameIdErr] = $(params.nameId).string().pipe(isValidNameId).$;
+ if (nameIdErr) return rej('invalid nameId param');
+
+ // Get exist
+ const exist = await App
+ .count({
+ nameIdLower: nameId.toLowerCase()
+ }, {
+ limit: 1
+ });
+
+ // Reply
+ res({
+ available: exist === 0
+ });
+});
diff --git a/src/server/api/endpoints/app/show.ts b/src/server/api/endpoints/app/show.ts
new file mode 100644
index 0000000000..99a2093b68
--- /dev/null
+++ b/src/server/api/endpoints/app/show.ts
@@ -0,0 +1,68 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import App, { pack } from '../../../../models/app';
+
+/**
+ * @swagger
+ * /app/show:
+ * note:
+ * summary: Show an application's information
+ * description: Require appId or nameId
+ * parameters:
+ * -
+ * name: appId
+ * description: Application ID
+ * in: formData
+ * type: string
+ * -
+ * name: nameId
+ * description: Application unique name
+ * in: formData
+ * type: string
+ *
+ * responses:
+ * 200:
+ * description: Success
+ * schema:
+ * $ref: "#/definitions/Application"
+ *
+ * default:
+ * description: Failed
+ * schema:
+ * $ref: "#/definitions/Error"
+ */
+
+/**
+ * Show an app
+ */
+module.exports = (params, user, app) => new Promise(async (res, rej) => {
+ const isSecure = user != null && app == null;
+
+ // Get 'appId' parameter
+ const [appId, appIdErr] = $(params.appId).optional.id().$;
+ if (appIdErr) return rej('invalid appId param');
+
+ // Get 'nameId' parameter
+ const [nameId, nameIdErr] = $(params.nameId).optional.string().$;
+ if (nameIdErr) return rej('invalid nameId param');
+
+ if (appId === undefined && nameId === undefined) {
+ return rej('appId or nameId is required');
+ }
+
+ // Lookup app
+ const ap = appId !== undefined
+ ? await App.findOne({ _id: appId })
+ : await App.findOne({ nameIdLower: nameId.toLowerCase() });
+
+ if (ap === null) {
+ return rej('app not found');
+ }
+
+ // Send response
+ res(await pack(ap, user, {
+ includeSecret: isSecure && ap.userId.equals(user._id)
+ }));
+});