From b61dfa88107cda6afd6e08ea1e0488adce33fb65 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 12 Sep 2020 18:45:00 +0200 Subject: [PATCH] Switch to apollo-server-express to enable middleware for authentication Signed-off-by: trivernis --- package-lock.json | 13 +---------- package.json | 7 +++--- src/datasources/db/cargobikeAPI.ts | 2 +- src/index.ts | 37 ++++++++++++++++++++++++++---- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index 86b5a65..734d53b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -310,6 +310,7 @@ "version": "3.4.0", "resolved": "https://registry.npmjs.org/@types/crc/-/crc-3.4.0.tgz", "integrity": "sha1-I2a+tDmc1zSzPkLHrICVduYX1Io=", + "dev": true, "requires": { "@types/node": "*" } @@ -863,18 +864,6 @@ "zen-observable-ts": "^0.8.21" } }, - "apollo-server": { - "version": "2.17.0", - "resolved": "https://registry.npmjs.org/apollo-server/-/apollo-server-2.17.0.tgz", - "integrity": "sha512-vVMu+VqjmsB6yk5iNTb/AXM6EJGd2uwzrcTAbwqvGI7GqCYDRZlGBwrQCjOU/jT/EPWdNRWks/qhJYiQMeVXSg==", - "requires": { - "apollo-server-core": "^2.17.0", - "apollo-server-express": "^2.17.0", - "express": "^4.0.0", - "graphql-subscriptions": "^1.0.0", - "graphql-tools": "^4.0.0" - } - }, "apollo-server-caching": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/apollo-server-caching/-/apollo-server-caching-0.5.2.tgz", diff --git a/package.json b/package.json index caf276b..5414519 100644 --- a/package.json +++ b/package.json @@ -27,13 +27,14 @@ "gulp-eslint": "^6.0.0", "gulp-nodemon": "^2.5.0", "gulp-typescript": "^6.0.0-alpha.1", - "typescript": "^4.0.2" + "typescript": "^4.0.2", + "@types/crc": "^3.4.0" }, "dependencies": { - "@types/crc": "^3.4.0", - "apollo-server": "^2.17.0", + "apollo-server-express": "^2.17.0", "crc": "^3.8.0", "dotenv": "^8.2.0", + "express": "^4.17.1", "graphql": "^15.3.0", "messagepack": "^1.1.12", "pg": "^8.3.3", diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index 1a3709d..026d7d4 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -26,7 +26,7 @@ export class CargoBikeAPI extends DataSource { bike.id = id bike.description = token bike.name = name - this.connection.manager.save(bike) + await this.connection.manager.save(bike) return { success: true, message: token, diff --git a/src/index.ts b/src/index.ts index e427d51..965f99b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,26 +1,55 @@ -import { ApolloServer } from 'apollo-server' +import { ApolloServer } from 'apollo-server-express' import bikeresolver from './resolvers/cargobikeResolver' import { CargoBikeAPI } from './datasources/db/cargobikeAPI' import typeDefs from './schema/type-defs' import 'reflect-metadata' import { createConnection } from 'typeorm' import { UserServerAPI } from './datasources/userserver/userserviceAPI' +import express from 'express' require('dotenv').config() +async function authenticate (req: any, res: any, next: any) { + if (process.env.NODE_ENV === 'develop') { + next() + } else { + const token = req.headers.authorization?.replace('Bearer ', '') + if (token) { + if (await userAPI.validateToken(token)) { + next() + } else { + res.status(401) + res.send('Unauthorized') + } + } else { + res.status(401) + res.send('Unauthorized') + } + } +} + createConnection().then(async () => { console.log('connected to db') }).catch(error => console.log(error)) +const userAPI = new UserServerAPI(process.env.RPC_HOST) + const server = new ApolloServer({ resolvers: [bikeresolver], typeDefs, dataSources: () => ({ cargoBikeAPI: new CargoBikeAPI(), - userAPI: new UserServerAPI(process.env.RPC_HOST) + userAPI }) }) +const app = express() + +app.post('/graphql', authenticate) +app.get(/\/graphql?&.*query=/, authenticate) +server.applyMiddleware({ app }) + console.log(__dirname) -server.listen() - .then(({ url }) => console.log(`Server ready at ${url} `)) +app.listen(4000, () => { + console.log('Server listening on port 4000') +})