This commit is contained in:
Luis Fernando Molina committed 2018-10-28 07:07:17 +01:00
1 parent a69130f05e
commit 3a0e28f2b5
3 files changed
+42 -51

No files matched your search

+3 -3
View File
@@ -9,6 +9,7 @@ const schema = require('./schema/schema')
const mongoose = require('mongoose')
const mongodbConfig = require('./models/MongoDB')
const checkJwt = require('./auth/middleware/jwt')
const logger = require('../utils/logger')
const cors = require('cors')
@@ -20,7 +21,7 @@ app.use(cors())
// Connect to the database
mongoose.connect(mongodbConfig.connectionString, { useNewUrlParser: true })
mongoose.connection.once('open', () => {
console.log('Connected to Mongo')
logger.info('Connected to Mongo')
})
/* Here we bind all requests to this endpoint to be procecced by the GraphQL Library. */
@@ -42,11 +43,10 @@ app.post('/graphql', graphqlHTTP(req => {
}))
app.post('/graphql', checkJwt, (err, req, res, next) => {
console.log(req)
if (err) return res.status(401).send(`[Authenticate Token Error] ${err.message}`)
next()
})
app.listen(4000, () => {
console.log('Now listening for requests on port 4000')
logger.info('Now listening for requests on port 4000')
})
+38 -45
View File
@@ -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,10 +10,11 @@ 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 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 AUTH_CONFIG = require('./Auth0') // Config where we have our secrets.
const logger = require('../utils/logger')
const decoder = webTokenLibrary({
cache: true,
@@ -23,69 +24,61 @@ const decoder = webTokenLibrary({
})
function decodeToken (token, callBackFunction) {
try {
if (global.INFO_LOG === true) { logger.info('[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) { logger.error('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> Invalid Token. ') }
if (global.ERROR_LOG === true) { logger.error('[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) { logger.info('[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) { logger.error('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> Error getting the key. ') }
if (global.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> key = ' + key) }
if (global.ERROR_LOG === true) { logger.error('[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) { logger.info('[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) { logger.error('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> onVerified -> Error verifying. ') }
if (global.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> onVerified -> decoded = ' + decoded) }
if (global.ERROR_LOG === true) { logger.error('[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) { logger.error('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> err = ' + err) }
callBackFunction(global.DEFAULT_FAIL_RESPONSE)
}
}
module.exports = decodeToken;
module.exports = decodeToken
+1 -3
View File
@@ -271,12 +271,10 @@ const Mutation = new GraphQLObjectType({
sessionToken: {type: GraphQLString}
},
resolve (parent, args) {
console.log('Request Received', args)
let key = {
authId: decodeURI(args.authId)
}
console.log('Key', key)
console.log('decode', decodeURI(args.authId))
let updatedUser = {
sessionToken: args.sessionToken
}