Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ministryofjustice/fb-jwt-client-node

Package Overview
Dependencies
Maintainers
12
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ministryofjustice/fb-jwt-client-node - npm Package Compare versions

Comparing version 0.0.30 to 0.0.33

140

lib/fb-jwt-client.js

@@ -17,37 +17,66 @@ const got = require('got')

if (response.status) {
responseLabels.status = response.status
}
if (response.status) responseLabels.status = response.status
if (response.statusCode) {
responseLabels.status_code = response.statusCode
}
/*
* Are the fields present?
*/
if (
Reflect.has(response, 'statusCode') &&
Reflect.has(response, 'statusMessage')) {
const statusCode = Reflect.get(response, 'statusCode')
const statusMessage = Reflect.get(response, 'statusMessage')
/*
* Are the values null?
*/
if (statusCode) responseLabels.status_code = statusCode
if (statusMessage) responseLabels.status_message = statusMessage
} else {
/*
* Alternatively, explicitly test for an Error
*/
if (response instanceof Error) {
if (Reflect.has(response, 'body')) {
const {
body: {
code,
name: message
}
} = response
if (response.statusMessage) {
responseLabels.status_message = response.statusMessage
}
if (code) responseLabels.error_code = code
if (message) responseLabels.error_message = message
} else {
const code = getErrorStatusCode(Reflect.get(response, 'code') || Reflect.get(response, 'name'))
const message = getErrorStatusMessage(code, Reflect.get(response, 'message'))
if (response.name) {
responseLabels.error_name = response.name
responseLabels.error_code = code
responseLabels.error_message = message
}
}
}
if (response.message) {
responseLabels.error_message = response.message
}
return responseLabels
}
const getErrorStatusCode = (message) => {
let statusCode = 500
if (message === 'ENOTFOUND') {
// no dns resolution
statusCode = 502
} else if (message === 'ECONNREFUSED') {
// connection rejected
statusCode = 503
/*
* Known errors. This can, of course, be extended
*/
const getErrorStatusCode = (key) => {
switch (key) {
case 'HTTPError':
return 404
case 'ENOTFOUND':
return 502
case 'ECONNREFUSED':
return 503
default:
return 500
}
return statusCode
}
/*
* This can, of course, be extended, too
*/
const getErrorStatusMessage = (key, message) => key === 404 ? 'Not Found' : message
/**

@@ -82,2 +111,3 @@ * Creates client using JSON Web Tokens

}
if (!serviceSecret) {

@@ -107,2 +137,3 @@ this.throwRequestError('ENOSERVICESECRET', 'No service secret passed to client')

}
this.requestMetrics = {

@@ -198,9 +229,8 @@ startTimer

*
* @param {string} [ivSeed]
* Initialization Vector
*
* @return {string}
*
**/
encryptUserIdAndToken (userId, userToken, ivSeed = userId + userToken) {
encryptUserIdAndToken (userId, userToken) {
const ivSeed = userId + userToken
return this.encrypt(this.serviceSecret, {userId, userToken}, ivSeed)

@@ -212,3 +242,3 @@ }

*
* @param {string} encryptedData
* @param {string} data
* Encrypted user ID and token

@@ -219,4 +249,4 @@ *

**/
decryptUserIdAndToken (encryptedData) {
return this.decrypt(this.serviceSecret, encryptedData)
decryptUserIdAndToken (data) {
return this.decrypt(this.serviceSecret, data)
}

@@ -275,10 +305,10 @@

if (hasData) {
if (isGET) {
if (isGET) {
if (hasData) {
requestOptions.searchParams = {
payload: Buffer.from(JSON.stringify(data)).toString('Base64')
}
} else {
requestOptions.body = data
}
} else {
requestOptions.json = data
}

@@ -326,3 +356,3 @@

logger.error(logObject, logMessage)
if (logger) logger.error(logObject, logMessage)
}

@@ -378,5 +408,2 @@

let requestMetricsEnd
let retryCounter = 1
function logError (type, error) {

@@ -386,2 +413,5 @@ client.logError(type, error, Object.assign({}, labels, {name: `jwt_${type.toLowerCase()}_request_error`}), logger)

let requestMetricsEnd
let retryCounter = 1
const gotOptions = got.mergeOptions(got.defaults.options, {

@@ -398,3 +428,3 @@ hooks: {

logError('client', error)
if (logger) logError('client', error)

@@ -414,22 +444,6 @@ if (requestMetricsEnd) requestMetricsEnd(getResponseLabels(error))

(response) => {
if (response.statusCode >= 400) {
const {
statusCode,
statusMessage,
body,
retryCount
} = response
if (response.statusCode >= 400 && logger) logError('client', response)
const error = {
statusCode,
statusMessage,
body,
retryCount
}
logError('client', error)
}
if (requestMetricsEnd) requestMetricsEnd(getResponseLabels(response))
response.body = response.body || {}
response.body = response.body || '{}'
return response

@@ -439,3 +453,3 @@ }

}
}, sendOptions, requestOptions)
}, sendOptions, requestOptions, {method})

@@ -454,7 +468,5 @@ const apiMetricsEnd = this.apiMetrics.startTimer(labels)

if (response.statusCode < 300) {
if (response.body && /^\s*$/.test(response.body)) {
if (requestMetricsEnd) requestMetricsEnd(getResponseLabels(response))
return {}
}
apiMetricsEnd(getResponseLabels(e))
if (response.body && /^\s*$/.test(response.body)) return {}
return response.body

@@ -467,3 +479,3 @@ }

this.handleRequestError(e)
client.handleRequestError(e)
}

@@ -470,0 +482,0 @@ }

{
"name": "@ministryofjustice/fb-jwt-client-node",
"version": "0.0.30",
"version": "0.0.33",
"description": "Form Builder JSON Web Token Client (Node)",

@@ -20,3 +20,3 @@ "main": "lib/fb-jwt-client.js",

"aes256": "^1.0.4",
"got": "^10.0.1",
"got": "10.0.1",
"jsonwebtoken": "^8.5.1",

@@ -31,5 +31,5 @@ "path-to-regexp": "^6.1.0"

"nock": "^11.6.0",
"proxyquire": "^2.1.3",
"sinon": "^7.5.0",
"sinon-chai": "^3.3.0"
"sinon-chai": "^3.3.0",
"proxyquire": "^2.1.3"
},

@@ -36,0 +36,0 @@ "_moduleAliases": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc