const fs = require('fs')
const crypto = require('crypto')
const jwt = require('jsonwebtoken')
const config = JSON.parse(
fs.readFileSync('PATH_TO_FILE')
)
let run = async () => {
// In node we don't need to manually decrypt the
// key, as the JWT library can handle this for us
letkey= {
key: config.boxAppSettings.appAuth.privateKey,
passphrase: config.boxAppSettings.appAuth.passphrase
}
// We will need the authenticationUrl again later,
// so it is handy to define here
letclaims= {
'iss': config.boxAppSettings.clientID,
'sub': config.enterpriseID,
'box_sub_type': 'enterprise',
'aud': authenticationUrl,
// This is an identifier that helps protect against
// replay attacks
'jti': crypto.randomBytes(64).toString('hex'),
// We give the assertion a lifetime of 45 seconds
// before it expires
'exp': Math.floor(Date.now() /1000) +45
}
letkeyId=config.boxAppSettings.appAuth.publicKeyID
// Rather than constructing the JWT assertion manually, we are
// using the jsonwebtoken library.
letassertion= jwt.sign(claims, key, {
// The API support "RS256", "RS384", and "RS512" encryption
'algorithm': 'RS512',
'keyid': keyId,
})
console.log(assertion)
}
run()