README
A small library to create micro-services with AES-256-CTR encryption on top of micro.
Install
npm i micro-encrypt
Usage
auths
To use this library the first thing you have to do is create a json file where you define all of your authentications/users. The format is key/value.
Every key of the json is an user. We will call it API_KEY
, and the value of that API_KEY
is the API_SECRET
. You can add as much as you want. Use just one API_KEY
for each client you connect with.
Example:
{
"04f8996da763b7a969b211da63548b10": "my_password",
"canbe_an_email@domain.com": "password2",
...
}
/micro
This module is a wrapper of micro with the difference that you have to pass an object
with two mandatory parameters:
auths:
Object
endpoints:
Array
const micro = require('micro-encrypt/micro')
const { create } = require('micro-encrypt/endpoints')
const auths = {
user1213151: 'the_password'
}
const endpoints = []
endpoints.push(create('/hello', () => 'world'))
const server = micro({ auths, endpoints })
server.listen(3000)
/endpoints
to do
/request
This module is just a function that creates a wrapper of request-promise which is also a promise wrapper of request.
const request = require('micro-encrypt/request')
const encryption = { API_KEY, API_SECRET } // Any of those you have defined previously in auths.json
const body = { symbol: 'BTC' }
const address = await request(`${url}/getAddressForDeposit`, { body, encryption })
If you want to use request as standalone you can do it this way:
const request = require('request')
const { encrypt, decrypt } = require('micro-encrypt/encryption')
const body = encrypt({ symbol: 'BTC' }, API_SECRET)
const headers = { authorization: API_KEY }
request(
`${url}/getAddressForDeposit`,
{ body, headers },
(error, response, body) => {
const address = decrypt(body, API_SECRET)
}
)
Error handling
A successful request always gets 2XX as statusCode. Any other statusCode will be an error. And you must handle it with try-catch.
const request = require('micro-encrypt/request')
try {
const address = await request(`${url}/helloWorld`)
} catch(e) {
console.log(e.statusCode) // 404
console.log(e.error) // { "message": "Not Found" }
}
If you want to use request
const request = require('request')
request(`${url}/helloWorld`, (error, response, body) => {
console.log(response.statusCode) // 404
console.log(body) // '{ "message": "Not Found" }'
})
/encryption
to do