Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions helpers/token-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const getCurrentTime = () => Math.floor(new Date() / 1000);
* @param apiKey - TokBox API Key
* @param apiSecret - TokBox API Secret
*/
const createTokenTokBox = (apiKey, apiSecret) => {
const createOpenTokToken = (apiKey, apiSecret) => {
const currentTime = getCurrentTime();
return jwt.sign({
iss: apiKey,
Expand All @@ -21,11 +21,11 @@ const createTokenTokBox = (apiKey, apiSecret) => {
};

/**
* Generates a new token for Nexmo users
* @param applicationId - Nexmo Application ID
* Generates a new token for Vonage users
* @param applicationId - Vonage Application ID
* @param privateKey - Buffer containing the private key
*/
const createTokenNexmo = (applicationId, privateKey) => {
const createVonageToken = (applicationId, privateKey) => {
if (!(privateKey instanceof Buffer)) {
throw new Error("You must set up your private key file.");
}
Expand All @@ -39,6 +39,6 @@ const createTokenNexmo = (applicationId, privateKey) => {
};

module.exports = {
createTokenTokBox,
createTokenNexmo,
createOpenTokToken,
createVonageToken,
};
42 changes: 39 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ require('dotenv').config();

const express = require('express');
const fs = require('fs');
const { createTokenTokBox, createTokenNexmo } = require('./helpers/token-generator');
const { createOpenTokToken, createVonageToken } = require('./helpers/token-generator');
const axios = require('axios');

const API_KEY = process.env.REACT_APP_API_KEY;
const API_SECRET = process.env.API_SECRET;
Expand Down Expand Up @@ -42,17 +43,52 @@ const app = express();
*/
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', CLIENT_URL);
res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-OPENTOK-AUTH');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
/**
* /graphql - Proxy GraphQL requests to the Insights API
*/
app.use(express.json());
app.post('/graphql', async (req, res) => {
try {
const token = isTokBoxApiKey
? createOpenTokToken(API_KEY, API_SECRET)
: createVonageToken(API_KEY, privateKey);
const headerKey = isTokBoxApiKey ? 'X-OPENTOK-AUTH' : 'Authorization';
const headerValue = isTokBoxApiKey ? token : `Bearer ${token}`;
const response = await axios.post(
process.env.REACT_APP_INSIGHTS_URL + '/graphql',
req.body,
{
headers: {
[headerKey]: headerValue,
'Content-Type': 'application/json',
},
}
);
res.status(response.status).json(response.data);
} catch (error) {
if (error.response) {
res.status(error.response.status).json(error.response.data);
} else {
res.status(500).json({ error: error.toString() });
}
}
});

/**
* /token - Get a jwt with the configured variables
* @returns {JSON}
*/
app.get('/token', (req, res) => {
const token = isTokBoxApiKey ?
createTokenTokBox(API_KEY, API_SECRET) :
createTokenNexmo(API_KEY, privateKey);
createOpenTokToken(API_KEY, API_SECRET) :
createVonageToken(API_KEY, privateKey);
res.send(JSON.stringify({
API_KEY,
token,
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { ApolloProvider } from 'react-apollo';
import App from './App';

const SERVER_URL = process.env.REACT_APP_SERVER_URL;
const INSIGHTS_URL = process.env.REACT_APP_INSIGHTS_URL;
const API_KEY = process.env.REACT_APP_API_KEY;

const isTokBoxApiKey = /^-?\d+$/.test(API_KEY);
Expand All @@ -26,7 +25,7 @@ const authMiddleware = setContext(() =>

const client = new ApolloClient({
link: authMiddleware.concat(new HttpLink({
uri: urlJoin(INSIGHTS_URL, '/graphql')
uri: urlJoin(SERVER_URL, '/graphql')
})),
cache: new InMemoryCache(),
});
Expand Down