UsersModule moved to old repos
This commit is contained in:
1 parent
b291894227
commit
121af47510
65 files changed
No files matched your search
@@ -0,0 +1,26 @@
|
||||
|
||||
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
|
||||
const checkJwt = jwt({
|
||||
// Dynamically provide a signing key
|
||||
// based on the kid in the header and
|
||||
// the signing keys provided by the JWKS endpoint.
|
||||
secret: jwksRsa.expressJwtSecret({
|
||||
cache: true,
|
||||
rateLimit: true,
|
||||
jwksRequestsPerMinute: 1,
|
||||
jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`
|
||||
}),
|
||||
|
||||
// Validate the audience and the issuer.
|
||||
credentialsRequired: false,
|
||||
audience: process.env.AUTH0_AUDIENCE,
|
||||
issuer: process.env.AUTH0_ISSUER,
|
||||
algorithms: [`RS256`]
|
||||
})
|
||||
|
||||
module.exports = checkJwt
|
||||
@@ -0,0 +1,81 @@
|
||||
const MODULE_NAME = 'token-decoder'
|
||||
|
||||
/*
|
||||
JSON Web Token (JWT) is a compact, URL-safe means of representing
|
||||
claims to be transferred between two parties. The claims in a JWT
|
||||
are encoded as a JSON object that is used as the payload of a JSON
|
||||
Web Signature (JWS) structure or as the plaintext of a JSON Web
|
||||
Encryption (JWE) structure, enabling the claims to be digitally
|
||||
signed or integrity protected with a Message Authentication Code
|
||||
(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 decoder = webTokenLibrary({
|
||||
cache: true,
|
||||
rateLimit: true,
|
||||
jwksRequestsPerMinute: 1,
|
||||
jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`
|
||||
})
|
||||
|
||||
function decodeToken (token, callBackFunction) {
|
||||
try {
|
||||
if (process.env.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
|
||||
|
||||
if (
|
||||
header === null ||
|
||||
header.kid === null ||
|
||||
payload === null
|
||||
) {
|
||||
if (process.env.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> Invalid Token. ') }
|
||||
if (process.env.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> token = ' + token) }
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
|
||||
decoder.getSigningKey(header.kid, onKeyReady)
|
||||
|
||||
function onKeyReady (err, key) {
|
||||
if (process.env.INFO_LOG === true) { console.log('[INFO] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> Entering function.') }
|
||||
|
||||
if (err) {
|
||||
if (process.env.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> Error getting the key. ') }
|
||||
if (process.env.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> key = ' + key) }
|
||||
if (process.env.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)
|
||||
|
||||
function onVerified (err, decoded) {
|
||||
if (process.env.INFO_LOG === true) { console.log('[INFO] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> onVerified -> Entering function.') }
|
||||
|
||||
if (err) {
|
||||
if (process.env.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> onVerified -> Error verifying. ') }
|
||||
if (process.env.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> onKeyReady -> onVerified -> decoded = ' + decoded) }
|
||||
if (process.env.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 (process.env.ERROR_LOG === true) { console.log('[ERROR] ' + MODULE_NAME + ' -> decodeToken -> err = ' + err) }
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = decodeToken
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"errors": [
|
||||
{
|
||||
"code": 401,
|
||||
"message": "Wait, you're not supposed to be here, go through the gate as everyone else"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
/* Callbacks default responses. */
|
||||
|
||||
global.DEFAULT_OK_RESPONSE = {
|
||||
result: 'Ok',
|
||||
message: 'Operation Succeeded'
|
||||
}
|
||||
|
||||
global.DEFAULT_FAIL_RESPONSE = {
|
||||
result: 'Fail',
|
||||
message: 'Operation Failed'
|
||||
}
|
||||
|
||||
global.DEFAULT_RETRY_RESPONSE = {
|
||||
result: 'Retry',
|
||||
message: 'Retry Later'
|
||||
}
|
||||
|
||||
global.CUSTOM_OK_RESPONSE = {
|
||||
result: 'Ok, but check Message',
|
||||
message: 'Custom Message'
|
||||
}
|
||||
|
||||
global.CUSTOM_FAIL_RESPONSE = {
|
||||
result: 'Fail Because',
|
||||
message: 'Custom Message'
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
|
||||
/* This module imports */
|
||||
|
||||
import 'dotenv/config'
|
||||
import './globals'
|
||||
import logger from './logger'
|
||||
import express from 'express'
|
||||
import graphqlHTTP from 'express-graphql'
|
||||
import schema from './schema/schema'
|
||||
import mongoose from 'mongoose'
|
||||
import checkJwt from './auth/jwt'
|
||||
import wrongPreshared from './errors/notAllowed.json'
|
||||
import cors from 'cors'
|
||||
|
||||
require('dotenv').config()
|
||||
|
||||
const app = express()
|
||||
|
||||
// Checking preshared key
|
||||
app.post('/graphql', (req, res, next) => {
|
||||
if (req.headers.preshared === process.env.USERS_API_PRESHARED) {
|
||||
next()
|
||||
} else {
|
||||
logger.error('Wrong Pre-Shared Received', req.headers.preshared)
|
||||
res.send(wrongPreshared)
|
||||
}
|
||||
})
|
||||
|
||||
// Allow crosss-origin requests
|
||||
app.use(cors())
|
||||
|
||||
// Database Connection
|
||||
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true })
|
||||
|
||||
mongoose.connection.once('open', () => {
|
||||
logger.info('Connected to the DB.')
|
||||
})
|
||||
|
||||
/* Here we bind all requests to this endpoint to be procecced by the GraphQL Library. */
|
||||
|
||||
app.get('/graphql', graphqlHTTP({
|
||||
schema,
|
||||
graphiql: true
|
||||
}))
|
||||
|
||||
app.post('/graphql', graphqlHTTP(req => {
|
||||
return {
|
||||
schema,
|
||||
graphiql: false,
|
||||
context: {
|
||||
user: req.user,
|
||||
userId: req.headers.userid
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
app.post('/graphql', checkJwt, (err, req, res, next) => {
|
||||
if (err) return res.status(401).send(`[Authenticate Token Error] ${err.message}`)
|
||||
next()
|
||||
})
|
||||
|
||||
app.listen(4000, () => {
|
||||
logger.info('Now listening for requests on port 4000')
|
||||
})
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { createLogger, format, transports } from 'winston'
|
||||
const { combine, timestamp, simple } = format
|
||||
import { inspect } from 'util'
|
||||
|
||||
const options = {
|
||||
file: {
|
||||
level: 'info',
|
||||
filename: '../logs/app.log',
|
||||
handleExceptions: true,
|
||||
json: true,
|
||||
maxsize: 5242880, // 5MB
|
||||
maxFiles: 5,
|
||||
colorize: false,
|
||||
timestamp: true
|
||||
},
|
||||
console: {
|
||||
level: 'debug',
|
||||
handleExceptions: true,
|
||||
json: false,
|
||||
colorize: true,
|
||||
timestamp: true
|
||||
}
|
||||
}
|
||||
|
||||
const logger = createLogger({
|
||||
level: 'info',
|
||||
format: format.combine(
|
||||
format.colorize(),
|
||||
format.timestamp({ format: 'YY/MM/DD HH:mm' }),
|
||||
format.printf(
|
||||
info => `${info.timestamp} - ${info.level}: ${JSON.parse(JSON.stringify(inspect(info.message)))}`
|
||||
)
|
||||
),
|
||||
transports: [
|
||||
new transports.File(options.file),
|
||||
new transports.Console(options.console)
|
||||
],
|
||||
exitOnError: false // do not exit on handled exceptions
|
||||
})
|
||||
|
||||
export default logger
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
/* This is the database schema definition for the User Collection in Mongo DB */
|
||||
|
||||
const mongoose = require('mongoose')
|
||||
const Schema = mongoose.Schema
|
||||
|
||||
const userSchema = new Schema({
|
||||
authId: String,
|
||||
referrerId: String,
|
||||
creationDate: Number,
|
||||
alias: String,
|
||||
firstName: String,
|
||||
middleName: String,
|
||||
lastName: String,
|
||||
email: String,
|
||||
emailVerified: Boolean,
|
||||
bio: String,
|
||||
isDeveloper: Boolean,
|
||||
isTrader: Boolean,
|
||||
isDataAnalyst: Boolean,
|
||||
roleId: String,
|
||||
avatarHandle: String,
|
||||
avatarChangeDate: String,
|
||||
sessionToken: String
|
||||
})
|
||||
|
||||
global.UserSchema = global.UserSchema || mongoose.model('User', userSchema)
|
||||
module.exports = global.UserSchema
|
||||
@@ -0,0 +1,468 @@
|
||||
const MODULE_NAME = 'schema'
|
||||
|
||||
import logger from '../logger'
|
||||
import _ from 'lodash'
|
||||
import User from '../models/user'
|
||||
import tokenDecoder from '../auth/token-decoder'
|
||||
|
||||
const graphql = require('graphql')
|
||||
|
||||
const {
|
||||
GraphQLObjectType,
|
||||
GraphQLString,
|
||||
GraphQLSchema,
|
||||
GraphQLID,
|
||||
GraphQLInt,
|
||||
GraphQLList,
|
||||
GraphQLNonNull
|
||||
} = graphql // Destructuring this variables from inside the package.
|
||||
|
||||
let roles = [
|
||||
{id: '1', name: 'Not Defined'},
|
||||
{id: '2', name: 'Developer'},
|
||||
{id: '3', name: 'Trader'},
|
||||
{id: '4', name: 'Data Analyst'}
|
||||
]
|
||||
|
||||
// Types
|
||||
|
||||
const UserType = new GraphQLObjectType({
|
||||
name: 'User',
|
||||
fields: () => ({
|
||||
id: { type: GraphQLID},
|
||||
authId: { type: GraphQLString},
|
||||
referrerId: {type: GraphQLString},
|
||||
alias: {type: GraphQLString},
|
||||
firstName: {type: GraphQLString},
|
||||
middleName: {type: GraphQLString},
|
||||
lastName: {type: GraphQLString},
|
||||
bio: {type: GraphQLString},
|
||||
email: {type: GraphQLString},
|
||||
emailVerified: {type: GraphQLInt},
|
||||
isDeveloper: {type: GraphQLInt},
|
||||
isTrader: {type: GraphQLInt},
|
||||
isDataAnalyst: {type: GraphQLInt},
|
||||
avatarHandle: {type: GraphQLString},
|
||||
avatarChangeDate: {type: GraphQLString},
|
||||
sessionToken: {type: GraphQLString},
|
||||
role: {
|
||||
type: RoleType,
|
||||
resolve (parent, args) {
|
||||
return _.find(roles, {id: parent.roleId})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const DescendentType = new GraphQLObjectType({
|
||||
name: 'Descendent',
|
||||
fields: () => ({
|
||||
id: { type: GraphQLID},
|
||||
referrerId: {type: GraphQLString},
|
||||
alias: {type: GraphQLString},
|
||||
firstName: {type: GraphQLString},
|
||||
middleName: {type: GraphQLString},
|
||||
lastName: {type: GraphQLString},
|
||||
descendents: {
|
||||
type: new GraphQLList(DescendentType),
|
||||
resolve (parent, args) {
|
||||
return User.find({referrerId: parent.id})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const RoleType = new GraphQLObjectType({
|
||||
name: 'Role',
|
||||
fields: () => ({
|
||||
id: { type: GraphQLID},
|
||||
name: {type: GraphQLString},
|
||||
users: {
|
||||
type: new GraphQLList(UserType),
|
||||
resolve (parent, args) {
|
||||
return User.find({roleId: parent.id})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// RootQueries
|
||||
|
||||
const RootQuery = new GraphQLObjectType({
|
||||
name: 'RootQueryType',
|
||||
fields: {
|
||||
user: {
|
||||
type: UserType,
|
||||
args: {id: {type: GraphQLID}},
|
||||
resolve (parent, args) {
|
||||
// Code to get data from data source.
|
||||
return User.findById(args.id)
|
||||
}
|
||||
},
|
||||
userByAuthId: {
|
||||
type: UserType,
|
||||
args: {authId: {type: GraphQLString}},
|
||||
resolve (parent, args) {
|
||||
// Code to get data from data source.
|
||||
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> RootQuery -> userByAuthId -> resolve -> Entering function.') }
|
||||
|
||||
/* In order to be able to wait for asyc calls to the database, we need to return a promise to GraphQL. */
|
||||
|
||||
const promiseToGraphQL = new Promise((resolve, reject) => {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> RootQuery -> userByAuthId -> resolve -> Promise -> Entering function.') }
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> RootQuery -> userByAuthId -> resolve -> Promise -> args.authId = ' + args.authId) }
|
||||
|
||||
findUserByAuthId(args.authId, onUserFound)
|
||||
|
||||
function onUserFound (err, responseToGraphQL) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> RootQuery -> userByAuthId -> resolve -> Promise -> onUserFound -> Entering function.') }
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> RootQuery -> userByAuthId -> resolve -> Promise -> onUserFound -> responseToGraphQL = ' + JSON.stringify(responseToGraphQL)) }
|
||||
|
||||
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
|
||||
if (process.env.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> RootQuery -> userByAuthId -> resolve -> Promise -> onUserFound -> err.message = ' + err.message) }
|
||||
reject(responseToGraphQL)
|
||||
} else {
|
||||
resolve(responseToGraphQL)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return promiseToGraphQL
|
||||
}
|
||||
},
|
||||
role: {
|
||||
type: RoleType,
|
||||
args: {id: {type: GraphQLID}},
|
||||
resolve (parent, args) {
|
||||
// Code to get data from data source.
|
||||
return _.find(roles, {id: args.id})
|
||||
}
|
||||
},
|
||||
users: {
|
||||
type: new GraphQLList(UserType),
|
||||
resolve (parent, args) {
|
||||
return User.find({})
|
||||
}
|
||||
},
|
||||
roles: {
|
||||
type: new GraphQLList(RoleType),
|
||||
resolve (parent, args) {
|
||||
return roles
|
||||
}
|
||||
},
|
||||
usersSearch: {
|
||||
type: new GraphQLList(UserType),
|
||||
args: {alias: {type: GraphQLString}, firstName: {type: GraphQLString}, middleName: {type: GraphQLString}, lastName: {type: GraphQLString}},
|
||||
resolve (parent, args) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> RootQuery -> usersSearch -> resolve -> Entering function.') }
|
||||
|
||||
let mongoQuery = { $or: [] }
|
||||
|
||||
if (args.alias !== null && args.alias !== '') { mongoQuery.$or.push({alias: args.alias}) }
|
||||
if (args.firstName !== null && args.firstName !== '') { mongoQuery.$or.push({firstName: args.firstName}) }
|
||||
if (args.middleName !== null && args.middleName !== '') { mongoQuery.$or.push({middleName: args.middleName}) }
|
||||
if (args.lastName !== null && args.lastName !== '') { mongoQuery.$or.push({lastName: args.lastName}) }
|
||||
|
||||
if (mongoQuery.$or.length === 0) { mongoQuery = {} }
|
||||
|
||||
return User.find(mongoQuery)
|
||||
}
|
||||
},
|
||||
descendents: {
|
||||
type: new GraphQLList(DescendentType),
|
||||
args: {id: {type: GraphQLString}},
|
||||
resolve (parent, args) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> RootQuery -> descendents -> resolve -> Entering function.') }
|
||||
|
||||
return User.find({referrerId: args.id})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Mutations
|
||||
|
||||
const Mutation = new GraphQLObjectType({
|
||||
name: 'Mutation',
|
||||
fields: {
|
||||
authenticate: {
|
||||
type: UserType,
|
||||
args: {
|
||||
idToken: {type: new GraphQLNonNull(GraphQLString)}
|
||||
},
|
||||
resolve (parent, args) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> Mutation -> authenticate -> resolve -> Entering function.') }
|
||||
|
||||
/* In order to be able to wait for asyc calls to the database, and authorization authority, we need to return a promise to GraphQL. */
|
||||
|
||||
const promiseToGraphQL = new Promise((resolve, reject) => {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> Mutation -> authenticate -> resolve -> Promise -> Entering function.') }
|
||||
authenticate(args.idToken, onAuthenticated)
|
||||
|
||||
function onAuthenticated (err, responseToGraphQL) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> Mutation -> authenticate -> resolve -> Promise -> onAuthenticated -> Entering function.') }
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> Mutation -> authenticate -> resolve -> Promise -> onAuthenticated -> responseToGraphQL = ' + JSON.stringify(responseToGraphQL)) }
|
||||
|
||||
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
|
||||
if (process.env.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> Mutation -> authenticate -> resolve -> Promise -> onAuthenticated -> err.message = ' + err.message) }
|
||||
reject(responseToGraphQL)
|
||||
} else {
|
||||
resolve(responseToGraphQL)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return promiseToGraphQL
|
||||
}
|
||||
},
|
||||
updateUser: {
|
||||
type: UserType,
|
||||
args: {
|
||||
firstName: {type: GraphQLString},
|
||||
middleName: {type: GraphQLString},
|
||||
lastName: {type: GraphQLString},
|
||||
bio: {type: GraphQLString},
|
||||
isDeveloper: {type: GraphQLInt},
|
||||
isTrader: {type: GraphQLInt},
|
||||
isDataAnalyst: {type: GraphQLInt},
|
||||
roleId: {type: new GraphQLNonNull(GraphQLString)}
|
||||
},
|
||||
resolve (parent, args, context) {
|
||||
let key = {
|
||||
_id: context.userId
|
||||
}
|
||||
|
||||
let updatedUser = {
|
||||
firstName: args.firstName,
|
||||
middleName: args.middleName,
|
||||
lastName: args.lastName,
|
||||
bio: args.bio,
|
||||
isDeveloper: args.isDeveloper,
|
||||
isTrader: args.isTrader,
|
||||
isDataAnalyst: args.isDataAnalyst,
|
||||
roleId: args.roleId
|
||||
}
|
||||
|
||||
return User.update(key, updatedUser)
|
||||
}
|
||||
},
|
||||
updateUserReferrer: {
|
||||
type: UserType,
|
||||
args: {
|
||||
referrerId: {type: GraphQLString}
|
||||
},
|
||||
resolve (parent, args, context) {
|
||||
let key = {
|
||||
_id: context.userId,
|
||||
referrerId: null
|
||||
}
|
||||
|
||||
let updatedUser = {
|
||||
referrerId: args.referrerId
|
||||
}
|
||||
|
||||
return User.update(key, updatedUser)
|
||||
}
|
||||
},
|
||||
updateSessionToken: {
|
||||
type: UserType,
|
||||
args: {
|
||||
userId: {type: GraphQLString},
|
||||
sessionToken: {type: GraphQLString}
|
||||
},
|
||||
resolve (parent, args) {
|
||||
let key = {
|
||||
_id: decodeURI(args.userId)
|
||||
}
|
||||
|
||||
let updatedUser = {
|
||||
sessionToken: args.sessionToken
|
||||
}
|
||||
|
||||
return User.update(key, updatedUser)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
function findUserByAuthId (authId, callBackFunction) {
|
||||
try {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> Entering function.') }
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> authId = ' + authId) }
|
||||
|
||||
if (authId === null || authId === undefined) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> User requested not specified.') }
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> args.authId = ' + authId) }
|
||||
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: 'Bad Request' })
|
||||
return
|
||||
}
|
||||
|
||||
User.findOne({authId: authId}, onUserReceived)
|
||||
|
||||
function onUserReceived (err, user) {
|
||||
if (err) {
|
||||
if (ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> findUserByAuthId -> onUserReceived -> Database Error.') }
|
||||
if (ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> findUserByAuthId -> onUserReceived -> err = ' + err) }
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err })
|
||||
} else {
|
||||
if (user === null) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> onUserReceived -> User not found at Database.') }
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> onUserReceived -> args.authId = ' + authId) }
|
||||
|
||||
let customResponse = {
|
||||
result: global.CUSTOM_OK_RESPONSE.result,
|
||||
message: 'User Not Found'
|
||||
}
|
||||
|
||||
callBackFunction(customResponse, { error: customResponse.message })
|
||||
return
|
||||
}
|
||||
|
||||
if (user.authId === authId && user.authId !== undefined) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> onUserReceived -> User found at Database.') }
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> onUserReceived -> args.authId = ' + authId) }
|
||||
|
||||
callBackFunction(global.DEFAULT_OK_RESPONSE, user)
|
||||
return
|
||||
} else {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> onUserReceived -> User found at Database is not the user requested.') }
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> onUserReceived -> args.authId = ' + authId) }
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> findUserByAuthId -> onUserReceived -> user.authId = ' + user.authId) }
|
||||
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err })
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (process.env.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> findUserByAuthId -> err.message = ' + err.message) }
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err })
|
||||
}
|
||||
}
|
||||
|
||||
function authenticate (encodedToken, callBackFunction) {
|
||||
try {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> authenticate -> Entering function.') }
|
||||
|
||||
let authId = ''
|
||||
let alias = ''
|
||||
let email = ''
|
||||
let emailVerified = false
|
||||
|
||||
tokenDecoder(encodedToken, onValidated)
|
||||
|
||||
function onValidated (err, decodedToken) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> authenticate -> onValidated -> Entering function.') }
|
||||
|
||||
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
|
||||
if (process.env.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> authenticate -> onValidated -> err.message = ' + err.message) }
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err })
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Ok, the user was correctly authenticated. Next we need to know if this logged in user
|
||||
has already been added to this module's database or not yet.
|
||||
|
||||
*/
|
||||
|
||||
authId = decodedToken.sub
|
||||
alias = decodedToken.nickname
|
||||
email = decodedToken.email
|
||||
emailVerified = decodedToken.email_verified
|
||||
|
||||
findUserByAuthId(authId, onUserFound)
|
||||
|
||||
function onUserFound (err, user) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> Entering function.') }
|
||||
|
||||
if (err.result === global.DEFAULT_FAIL_RESPONSE.result) {
|
||||
if (process.env.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> err.message = ' + err.message) }
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err })
|
||||
return
|
||||
}
|
||||
|
||||
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> User already exists at database') }
|
||||
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> Existing User: ' + JSON.stringify(user)) }
|
||||
|
||||
callBackFunction(global.DEFAULT_OK_RESPONSE, { authId: authId, alias: user.alias })
|
||||
return
|
||||
}
|
||||
|
||||
if (
|
||||
err.result === global.CUSTOM_OK_RESPONSE.result &&
|
||||
err.message === 'User Not Found'
|
||||
) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> User does not exist at database') }
|
||||
|
||||
/*
|
||||
|
||||
The authenticated user is NOT at our module database. We need to add him.
|
||||
We will take from the authentication provider the basic information it knows about the logged in users
|
||||
and save it as an initial set of data, which can later be modified.
|
||||
|
||||
*/
|
||||
|
||||
/* The user can sign up with many different possible social accounts. Currently this module only supports Github only. */
|
||||
|
||||
let authArray = authId.split('|')
|
||||
let socialAccountProvider = authArray[0]
|
||||
|
||||
if (
|
||||
socialAccountProvider !== 'github' &&
|
||||
socialAccountProvider !== 'auth0'
|
||||
) {
|
||||
if (process.env.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> Social Account Provider not Supoorted. ') }
|
||||
if (process.env.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> socialAccountProvider = ' + socialAccountProvider) }
|
||||
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err })
|
||||
return
|
||||
}
|
||||
|
||||
let localDate = new Date()
|
||||
let creationDate = localDate.valueOf()
|
||||
|
||||
let newUser = new User({
|
||||
alias: alias,
|
||||
authId: authId,
|
||||
creationDate: creationDate,
|
||||
email: email,
|
||||
emailVerified: emailVerified,
|
||||
roleId: '1'
|
||||
})
|
||||
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> ' + alias + ' being added to the database') }
|
||||
|
||||
newUser.save(onSaved)
|
||||
|
||||
function onSaved (err, savedUser) {
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> onSaved -> Entering function.') }
|
||||
|
||||
if (err) {
|
||||
if (process.env.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> onSaved -> err = ' + err) }
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err })
|
||||
return
|
||||
}
|
||||
|
||||
if (process.env.INFO_LOG === true) { logger.debug('[INFO] ' + MODULE_NAME + ' -> authenticate -> onValidated -> onUserFound -> onSaved -> Saved User: ' + JSON.stringify(savedUser)) }
|
||||
|
||||
callBackFunction(global.DEFAULT_OK_RESPONSE, { authId, alias })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (process.env.ERROR_LOG === true) { logger.error('[ERROR] ' + MODULE_NAME + ' -> authenticate -> err.message = ' + err.message) }
|
||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err })
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new GraphQLSchema({
|
||||
query: RootQuery,
|
||||
mutation: Mutation
|
||||
})
|
||||
@@ -0,0 +1,9 @@
|
||||
const getItem = async name => window.localStorage.getItem(name)
|
||||
const setItem = async (name, value) => window.localStorage.setItem(name, value)
|
||||
const removeItem = async name => window.localStorage.removeItem(name)
|
||||
|
||||
export {
|
||||
getItem,
|
||||
setItem,
|
||||
removeItem
|
||||
}
|
||||
Reference in new issue
Block a user