Skip to content

Instantly share code, notes, and snippets.

@altexy
Created August 8, 2018 12:57
Show Gist options
  • Save altexy/368460d8f13b77e374fc8d344597c258 to your computer and use it in GitHub Desktop.
Save altexy/368460d8f13b77e374fc8d344597c258 to your computer and use it in GitHub Desktop.
Crazy JS spagetty
require('dotenv').config()
var jwt = require('jsonwebtoken');
var logger = require('morgan');
let atob = require('atob');
let btoa = require('btoa');
let fs = require('fs-extra');
let mcache = require('memory-cache');
const client = require('node-rest-client').Client
let consolidation = {}
consolidation.token = ""
consolidation.execute = () => {
let restClient = new client({
mimetypes: {
json: ['application/json', 'application/json; charset=utf-8'],
xml: ['application/xml', 'application/xml; charset=utf-8']
}
})
let ToCustomTime = (minutes, operation) => {
let time = new Date().getTime();
if(operation == "add") return new Date(time + (minutes * 60 * 1000))
else if(operation == "subtract") return new Date(time - (minutes * 60 * 1000))
else return (new Date()).toUTCString();
}
let ToUnixTime = (date) => {
let DateInTime = new Date(date).getTime()
let UnixTime = DateInTime/1000
return UnixTime
}
let token = "",
assertion = "",
issuer = process.env.OAUTH_CLIENT_ID,
subject = process.env.CERTIFICATE_THUMBPRINT,
audience = process.env.CALLBACK_URL,
issuedAt = ToCustomTime(0, "add"),
notBefore = ToCustomTime(5, "subtract"),
expiration = ToCustomTime(5, "add"),
payload = {
iss : issuer,
sub : subject,
aud : audience,
iat : ToUnixTime(issuedAt),
nbf : ToUnixTime(new Date().Add),
exp : ToUnixTime(expiration)
},
key = {
key: fs.readFileSync(__dirname + '/Authentication/JWT/key.pem', 'utf8'),
passphrase: 'p@$$w0rd'
},
callback = (err, decoded) => {
if(err) console.log(err)
else {
assertion = btoa(decoded)
GetTokenFromCache()
}
}
//Get the payload signed with the certificate
jwt.sign(payload, key, {algorithm: 'RS256'}, callback)
const GetToken = (tokenCallback, cacheCallback) => {
args={
data : {
"grant_type": process.env.OAUTH_GRANT_TYPE,
"assertion": assertion,
"clientId": process.env.OAUTH_CLIENT_ID,
"clientsecret": process.env.OAUTH_CLIENT_SECRET
},
headers : {
"Content-Type": "application/json",
"X-EmpowerID-Api-Key": process.env.EMPOWERID_API_KEY
}
}
return tokenCallback(args, cacheCallback)
}
const TokenRequest = (args, callback) =>{
restClient.post(process.env.SERVER_AUTHENTICATION_URL, args, function(data, response){
let result = data.toString(),
responseObj = JSON.parse(result),
accessToken = responseObj.access_token,
expiresIn = responseObj.expires_in
mcache.put('accessToken', accessToken)
mcache.put('accessTokenExpiration', new Date(new Date().getTime() + expiresIn))
return callback()
})
}
const GetTokenFromCache = (callback) => {
const ReturnToken = () => {
let token = mcache.get('accessToken')
if(token != "" || token != null){
consolidation.token = token
return token
}
}
return GetToken(TokenRequest, ReturnToken)
}
GetTokenFromCache()
}
module.exports = consolidation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment