From 9fea88c0879b9ed2a65903c4f6b6fa131442690d Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 15 Nov 2018 07:58:01 +0100 Subject: [PATCH] Removed the Auth0 env file from the server --- server/app.js | 1 - server/auth/middleware/jwt.js | 16 +++---- server/auth/token-decoder.js | 84 +++++++++++++++-------------------- 3 files changed, 45 insertions(+), 56 deletions(-) diff --git a/server/app.js b/server/app.js index 8a0286318..f057c495b 100644 --- a/server/app.js +++ b/server/app.js @@ -9,7 +9,6 @@ const express = require('express') const graphqlHTTP = require('express-graphql') const schema = require('./schema/schema') const mongoose = require('mongoose') -const mongodbConfig = require('./models/MongoDB') const checkJwt = require('./auth/middleware/jwt') const cors = require('cors') diff --git a/server/auth/middleware/jwt.js b/server/auth/middleware/jwt.js index 9b8ccdcb1..f9c94b734 100644 --- a/server/auth/middleware/jwt.js +++ b/server/auth/middleware/jwt.js @@ -1,6 +1,6 @@ -const jwt = require('express-jwt'); -const jwksRsa = require('jwks-rsa'); -const AUTH_CONFIG = require('../Auth0'); +const jwt = require('express-jwt') +const jwksRsa = require('jwks-rsa') + // Authentication middleware. When used, the // if the access token exists, it be verified against // the Auth0 JSON Web Key Set @@ -12,14 +12,14 @@ const checkJwt = jwt({ cache: true, rateLimit: true, jwksRequestsPerMinute: 1, - jwksUri: `https://${AUTH_CONFIG.domain}/.well-known/jwks.json` + jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json` }), // Validate the audience and the issuer. credentialsRequired: false, - audience: AUTH_CONFIG.api_audience, - issuer: AUTH_CONFIG.issuer, + audience: process.env.AUTH0_AUDIENCE, + issuer: process.env.AUTH0_ISSUER, algorithms: [`RS256`] -}); +}) -module.exports = checkJwt ; +module.exports = checkJwt diff --git a/server/auth/token-decoder.js b/server/auth/token-decoder.js index ea10fb110..c31691520 100644 --- a/server/auth/token-decoder.js +++ b/server/auth/token-decoder.js @@ -1,4 +1,4 @@ -const MODULE_NAME = "token-decoder"; +const MODULE_NAME = 'token-decoder' /* JSON Web Token (JWT) is a compact, URL-safe means of representing @@ -10,82 +10,72 @@ const MODULE_NAME = "token-decoder"; (MAC) and/or encrypted. */ -const JSONWebToken = require('jsonwebtoken'); // A library to deal with JSON Web Tokens. -const webTokenLibrary = require('jwks-rsa'); // A library to retrieve RSA signing keys from a JWKS (JSON Web Key Set) endpoint. - -const AUTH_CONFIG = require('./Auth0'); // Config where we have our secrets. +const JSONWebToken = require('jsonwebtoken') // A library to deal with JSON Web Tokens. +const webTokenLibrary = require('jwks-rsa') // A library to retrieve RSA signing keys from a JWKS (JSON Web Key Set) endpoint. const decoder = webTokenLibrary({ cache: true, rateLimit: true, jwksRequestsPerMinute: 1, - jwksUri: `https://${AUTH_CONFIG.domain}/.well-known/jwks.json` + jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json` }) function decodeToken (token, callBackFunction) { + try { + if (global.INFO_LOG === true) { console.log('[INFO] ' + MODULE_NAME + ' -> decodeToken -> Entering function.') } - try{ - - if (global.INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> decodeToken -> Entering function."); } - - const decoded = JSONWebToken.decode(token, { complete: true }); - const header = decoded.header; - const payload = decoded.payload; + const decoded = JSONWebToken.decode(token, { complete: true }) + const header = decoded.header + const payload = decoded.payload if ( header === null || header.kid === null || payload === null ) { - if (global.ERROR_LOG === true) { console.log ("[ERROR] " + MODULE_NAME + " -> decodeToken -> Invalid Token. "); } - if (global.ERROR_LOG === true) { console.log ("[ERROR] " + MODULE_NAME + " -> decodeToken -> token = " + token); } - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + if (global.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> Invalid Token. ') } + if (global.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> token = ' + token) } + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return } - decoder.getSigningKey(header.kid, onKeyReady); + decoder.getSigningKey(header.kid, onKeyReady) - function onKeyReady(err, key) { - - if (global.INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> decodeToken -> onKeyReady -> Entering function."); } + function onKeyReady (err, key) { + if (global.INFO_LOG === true) { console.log('[INFO] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> Entering function.') } if (err) { - - if (global.ERROR_LOG === true) { console.log ("[ERROR] " + MODULE_NAME + " -> decodeToken -> onKeyReady -> Error getting the key. "); } - if (global.ERROR_LOG === true) { console.log ("[ERROR] " + MODULE_NAME + " -> decodeToken -> onKeyReady -> key = " + key); } - if (global.ERROR_LOG === true) { console.log ("[ERROR] " + MODULE_NAME + " -> decodeToken -> onKeyReady -> err = " + err); } - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; + if (global.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> Error getting the key. ') } + if (global.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> key = ' + key) } + if (global.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> err = ' + err) } + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return } JSONWebToken.verify( token, key.publicKey, { algorithms: ['RS256'] }, - onVerified); + onVerified) - function onVerified(err, decoded) { + function onVerified (err, decoded) { + if (global.INFO_LOG === true) { console.log('[INFO] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> onVerified -> Entering function.') } - if (global.INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> decodeToken -> onKeyReady -> onVerified -> Entering function."); } - - if (err) { - - if (global.ERROR_LOG === true) { console.log ("[ERROR] " + MODULE_NAME + " -> decodeToken -> onKeyReady -> onVerified -> Error verifying. "); } - if (global.ERROR_LOG === true) { console.log ("[ERROR] " + MODULE_NAME + " -> decodeToken -> onKeyReady -> onVerified -> decoded = " + decoded); } - if (global.ERROR_LOG === true) { console.log ("[ERROR] " + MODULE_NAME + " -> decodeToken -> onKeyReady -> onVerified -> err = " + err); } - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - callBackFunction(global.DEFAULT_OK_RESPONSE, decoded); + if (err) { + if (global.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> onVerified -> Error verifying. ') } + if (global.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> onVerified -> decoded = ' + decoded) } + if (global.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> onVerified -> err = ' + err) } + callBackFunction(global.DEFAULT_FAIL_RESPONSE) + return } + + callBackFunction(global.DEFAULT_OK_RESPONSE, decoded) + } } - } catch(err) { - - if (global.ERROR_LOG === true) { console.log("[ERROR] " + MODULE_NAME + " -> decodeToken -> err = " + err); } - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - + } catch (err) { + if (global.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> err = ' + err) } + callBackFunction(global.DEFAULT_FAIL_RESPONSE) } } -module.exports = decodeToken; +module.exports = decodeToken