diff --git a/package-lock.json b/package-lock.json index f6b951d32..2bf4179ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "0.0.0-development", "license": "MIT", "dependencies": { - "body-parser": "^1.19.0", "cors": "^2.8.5", "express": "^4.17.1", "ws": "^7.2.3", diff --git a/package.json b/package.json index d9986a5a6..667d7dafb 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "semantic-release": "semantic-release" }, "dependencies": { - "body-parser": "^1.19.0", "cors": "^2.8.5", "express": "^4.17.1", "ws": "^7.2.3", diff --git a/src/api/README.md b/src/api/README.md index 60deaa7be..b67d54967 100644 --- a/src/api/README.md +++ b/src/api/README.md @@ -13,9 +13,3 @@ Endpoints: This group of methods uses `:key` option from config: * GET `/:key/id` - return a new user id. required `:key` from config. * GET `/:key/peers` - return an array of all connected users. required `:key` from config. **IMPORTANT:** You should set `allow_discovery` to `true` in config to enable this method. It disabled by default. - -This group of methods uses `:key` option from config, `:userId` and `:userToken` parameters from user. -* POST `/:key/:userId/:userToken/offer` -* POST `/:key/:userId/:userToken/candidate` -* POST `/:key/:userId/:userToken/answer` -* POST `/:key/:userId/:userToken/leave` \ No newline at end of file diff --git a/src/api/index.ts b/src/api/index.ts index 257bf2475..babb7a40d 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,25 +1,16 @@ -import bodyParser from "body-parser"; import cors from "cors"; import express from "express"; import publicContent from "../../app.json"; import { IConfig } from "../config"; -import { IMessageHandler } from "../messageHandler"; import { IRealm } from "../models/realm"; -import { AuthMiddleware } from "./middleware/auth"; -import CallsApi from "./v1/calls"; import PublicApi from "./v1/public"; -export const Api = ({ config, realm, messageHandler }: { +export const Api = ({ config, realm }: { config: IConfig; realm: IRealm; - messageHandler: IMessageHandler; }): express.Router => { - const authMiddleware = new AuthMiddleware(config, realm); - const app = express.Router(); - const jsonParser = bodyParser.json(); - app.use(cors()); app.get("/", (_, res) => { @@ -27,7 +18,6 @@ export const Api = ({ config, realm, messageHandler }: { }); app.use("/:key", PublicApi({ config, realm })); - app.use("/:key/:id/:token", authMiddleware.handle, jsonParser, CallsApi({ realm, messageHandler })); return app; }; diff --git a/src/api/middleware/auth/index.ts b/src/api/middleware/auth/index.ts deleted file mode 100644 index 476e9e9ee..000000000 --- a/src/api/middleware/auth/index.ts +++ /dev/null @@ -1,35 +0,0 @@ -import express from "express"; -import { IConfig } from "../../../config"; -import { Errors } from "../../../enums"; -import { IRealm } from "../../../models/realm"; -import { IMiddleware } from "../middleware"; - -export class AuthMiddleware implements IMiddleware { - - constructor(private readonly config: IConfig, private readonly realm: IRealm) { } - - public handle = (req: express.Request, res: express.Response, next: express.NextFunction) => { - const { id, token, key } = req.params; - - if (key !== this.config.key) { - return res.status(401).send(Errors.INVALID_KEY); - } - - if (!id) { - return res.sendStatus(401); - } - - const client = this.realm.getClientById(id); - - if (!client) { - return res.sendStatus(401); - } - - if (client.getToken() && token !== client.getToken()) { - return res.status(401).send(Errors.INVALID_TOKEN); - } - - next(); - }; - -} diff --git a/src/api/middleware/middleware.ts b/src/api/middleware/middleware.ts deleted file mode 100644 index 9c28f9d4b..000000000 --- a/src/api/middleware/middleware.ts +++ /dev/null @@ -1,5 +0,0 @@ -import express from "express"; - -export interface IMiddleware { - handle(req: express.Request, res: express.Response, next: express.NextFunction): any; -} diff --git a/src/api/v1/calls/index.ts b/src/api/v1/calls/index.ts deleted file mode 100644 index 3535e991f..000000000 --- a/src/api/v1/calls/index.ts +++ /dev/null @@ -1,40 +0,0 @@ -import express from "express"; -import { IMessageHandler } from "../../../messageHandler"; -import { IMessage } from "../../../models/message"; -import { IRealm } from "../../../models/realm"; - -export default ({ realm, messageHandler }: { realm: IRealm; messageHandler: IMessageHandler; }): express.Router => { - const app = express.Router(); - - const handle = (req: express.Request, res: express.Response, next: express.NextFunction): any => { - const { id } = req.params; - - if (!id) return next(); - - const client = realm.getClientById(id); - - if (!client) { - throw new Error(`client not found:${id}`); - } - - const { type, dst, payload } = req.body; - - const message: IMessage = { - type, - src: id, - dst, - payload - }; - - messageHandler.handle(client, message); - - res.sendStatus(200); - }; - - app.post("/offer", handle); - app.post("/candidate", handle); - app.post("/answer", handle); - app.post("/leave", handle); - - return app; -}; diff --git a/src/instance.ts b/src/instance.ts index 0b8f522d1..cb13bb562 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -21,7 +21,7 @@ export const createInstance = ({ app, server, options }: { const realm: IRealm = new Realm(); const messageHandler = new MessageHandler(realm); - const api = Api({ config, realm, messageHandler }); + const api = Api({ config, realm }); const messagesExpire: IMessagesExpire = new MessagesExpire({ realm, config, messageHandler }); const checkBrokenConnections = new CheckBrokenConnections({ realm,