@ministryofjustice/fb-jwt-client-node
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -20,2 +20,5 @@ const request = require('request-promise-native') | ||
* | ||
* @param {string} serviceSecret | ||
* Service secret | ||
* | ||
* @param {string} serviceToken | ||
@@ -35,6 +38,9 @@ * Service token | ||
**/ | ||
constructor (serviceToken, serviceSlug, microserviceUrl, errorClass) { | ||
constructor (serviceSecret, serviceToken, serviceSlug, microserviceUrl, errorClass) { | ||
if (errorClass) { | ||
this.ErrorClass = errorClass | ||
} | ||
if (!serviceSecret) { | ||
this.throwRequestError('ENOSERVICESECRET', 'No service secret passed to client') | ||
} | ||
if (!serviceToken) { | ||
@@ -50,2 +56,3 @@ this.throwRequestError('ENOSERVICETOKEN', 'No service token passed to client') | ||
this.serviceSecret = serviceSecret | ||
this.serviceToken = serviceToken | ||
@@ -119,2 +126,36 @@ this.serviceUrl = microserviceUrl | ||
/** | ||
* Encrypt user ID and token using service secret | ||
* | ||
* Guaranteed to return the same value | ||
* | ||
* @param {string} userId | ||
* User ID | ||
* | ||
* @param {string} userToken | ||
* User token | ||
* | ||
* @return {string} | ||
* | ||
**/ | ||
encryptUserIdAndToken (userId, userToken) { | ||
const serviceSecret = this.serviceSecret | ||
const ivSeed = userId + userToken | ||
return this.encrypt(serviceSecret, {userId, userToken}, ivSeed) | ||
} | ||
/** | ||
* Decrypt user ID and token using service secret | ||
* | ||
* @param {string} encryptedData | ||
* Encrypted user ID and token | ||
* | ||
* @return {object} | ||
* | ||
**/ | ||
decryptUserIdAndToken (encryptedData) { | ||
const serviceSecret = this.serviceSecret | ||
return this.decrypt(serviceSecret, encryptedData) | ||
} | ||
/** | ||
* Create user-specific endpoint | ||
@@ -121,0 +162,0 @@ * |
@@ -14,2 +14,3 @@ const test = require('tape') | ||
const serviceSlug = 'testServiceSlug' | ||
const serviceSecret = 'testServiceSecret' | ||
const serviceToken = 'testServiceToken' | ||
@@ -55,12 +56,16 @@ const microserviceUrl = 'https://microservice' | ||
test('When instantiating client without a service secret', t => { | ||
testInstantiation(t, [], 'ENOSERVICESECRET', 'No service secret passed to client') | ||
}) | ||
test('When instantiating client without a service token', t => { | ||
testInstantiation(t, [], 'ENOSERVICETOKEN', 'No service token passed to client') | ||
testInstantiation(t, [serviceSecret], 'ENOSERVICETOKEN', 'No service token passed to client') | ||
}) | ||
test('When instantiating client without a service slug', t => { | ||
testInstantiation(t, [serviceToken], 'ENOSERVICESLUG', 'No service slug passed to client') | ||
testInstantiation(t, [serviceSecret, serviceToken], 'ENOSERVICESLUG', 'No service slug passed to client') | ||
}) | ||
test('When instantiating client without a service url', t => { | ||
testInstantiation(t, [serviceToken, serviceSlug], 'ENOMICROSERVICEURL', 'No microservice url passed to client') | ||
testInstantiation(t, [serviceSecret, serviceToken, serviceSlug], 'ENOMICROSERVICEURL', 'No microservice url passed to client') | ||
}) | ||
@@ -73,3 +78,3 @@ | ||
try { | ||
const jwtClient = new FBJWTClient(null, microserviceUrl, serviceSlug, MyError) // eslint-disable-line no-unused-vars | ||
const jwtClient = new FBJWTClient(null, serviceToken, serviceSlug, microserviceUrl, MyError) // eslint-disable-line no-unused-vars | ||
} catch (e) { | ||
@@ -81,3 +86,3 @@ t.equal(e.name, 'MyError', 'it should use the error class passed') | ||
// Set up a client to test the methods | ||
const jwtClient = new FBJWTClient(serviceToken, serviceSlug, microserviceUrl) | ||
const jwtClient = new FBJWTClient(serviceSecret, serviceToken, serviceSlug, microserviceUrl) | ||
@@ -84,0 +89,0 @@ // Endpoint URLs |
{ | ||
"name": "@ministryofjustice/fb-jwt-client-node", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"description": "Form Builder JSON Web Token Client (Node)", | ||
"main": "index.js", | ||
"main": "lib/fb-jwt-client.js", | ||
"repository": { | ||
@@ -7,0 +7,0 @@ "type": "git", |
@@ -22,5 +22,9 @@ # Form Builder JSON Web Token client (Node) | ||
// initialise client | ||
const jwtClient = new FBJWTClient(serviceToken, serviceSlug, microserviceUrl, [errorClass]) | ||
const jwtClient = new FBJWTClient(serviceSecret, serviceToken, serviceSlug, microserviceUrl, [errorClass]) | ||
``` | ||
#### `serviceSecret` | ||
Constructor will throw an error if no service secret is passed | ||
#### `serviceToken` | ||
@@ -47,4 +51,4 @@ | ||
class FBMyClient extends FBJWTClient { | ||
constructor (serviceToken, serviceSlug, microserviceUrl, myVar) { | ||
super(serviceToken, serviceSlug, microserviceUrl) | ||
constructor (serviceSecret, serviceToken, serviceSlug, microserviceUrl, myVar) { | ||
super(serviceSecret, serviceToken, serviceSlug, microserviceUrl) | ||
// do something with additional constructor argument | ||
@@ -55,3 +59,3 @@ this.myVar = myVar | ||
const myClient = new FBMyClient('service token', 'myservice', 'http://myservice', 'my var') | ||
const myClient = new FBMyClient('service_secret', 'service_token', 'myservice', 'http://myservice', 'my var') | ||
``` | ||
@@ -62,6 +66,6 @@ | ||
class FBAnotherClient extends FBJWTClient { | ||
constructor (serviceToken, serviceSlug, microserviceUrl) { | ||
constructor (serviceSecret, serviceToken, serviceSlug, microserviceUrl) { | ||
// create custom error class | ||
class FBAnotherClientError extends FBJWTClient.prototype.ErrorClass {} | ||
super(serviceToken, serviceSlug, microserviceUrl, FBAnotherClientError) | ||
super(serviceSecret, serviceToken, serviceSlug, microserviceUrl, FBAnotherClientError) | ||
} | ||
@@ -96,3 +100,11 @@ } | ||
Decrypt data | ||
- encryptUserIdAndToken | ||
Encrypt user ID and token using service secret | ||
- decryptUserIdAndToken | ||
Decrypt user ID and token using service secret | ||
- handleRequestError | ||
@@ -110,24 +122,4 @@ | ||
### Loading and initialising client with secret and associated methods | ||
``` javascript | ||
// load client class | ||
const {FBJWTClientWithSecret} = require('@ministryofjustice/fb-jwt-client-node') | ||
// initialise client | ||
const jwtClientWithSecret = new FBJWTClientWithSecret(serviceSecret, serviceToken, serviceSlug, microserviceUrl, [errorClass]) | ||
``` | ||
#### Methods | ||
- encryptUserIdAndToken | ||
Encrypt user ID and token using service secret | ||
- decryptUserIdAndToken | ||
Decrypt user ID and token using service secret | ||
## Further details | ||
See documentation in code for further details and `fb-user-datastore-client-node` and `fb-submitter-client-node` for examples. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31051
9
789
119