Add token verification with development basic header

This commit is contained in:
Niklas Fondberg
2021-07-06 10:31:04 +02:00
parent c92482777d
commit 4b7133a9ab
2 changed files with 177 additions and 2 deletions
+11 -2
View File
@@ -1,7 +1,7 @@
import dotenv from 'dotenv';
dotenv.config();
import { ApolloServer } from 'apollo-server';
import { ApolloServer, AuthenticationError } from 'apollo-server';
import knexStringcase from 'knex-stringcase';
import { dbConfig } from './config';
import { typeDefs } from './schema';
@@ -10,6 +10,7 @@ import { OrderAPI } from './datasources/order-api';
import { ProductAPI } from './datasources/product-api';
import { DesignerAPI } from './datasources/designer-api';
import { Api2DataSource } from './datasources/api2-datasource';
import { verifyToken } from './cognito/cognito-client';
console.log(`dbConfig`, dbConfig);
@@ -27,7 +28,15 @@ const dataSources = () => ({
});
const context = async ({ req }) => {
return { data: { foo: 'bar' } };
const authHeader = req.headers.authorization || '';
if (authHeader.startsWith('Bearer ')) {
const token = authHeader.substring(7, authHeader.length);
const res = await verifyToken({ token });
return { auth: res };
} else if (authHeader == 'Basic ZGV2OmRldg==') {
return { auth: 'during development' };
}
throw new AuthenticationError('Not authorized');
};
async function main() {