sails-service-cipher

Service for Sails framework with Cipher features

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
sails-service-cipher
503.2.18 years ago9 years agoMinified + gzip package size for sails-service-cipher in KB

Readme

sails-service-cipher
Build Status Coverage
Downloads Downloads npm version License
semantic-release Commitizen friendly dependencies dev dependencies
Service for Sails framework with Cipher features.

List of supported ciphers

  • JSON Web Token

Getting Started

Install this module.
npm install sails-service-cipher

Then require it in your service and create cipher instance.
// api/services/CipherService.js
import CipherService from 'sails-service-cipher';

export default CipherService('jwt');

// api/controllers/CipherController.js
export default {
  cipher: function(req, res) {
    res.ok(CipherService.encodeSync('DATA_HERE'));
  }
};

API

Each of Cipher instances has 4 methods:

encode(data, config)

data - Can be any type of data that you need to encode.
config - Additional configuration that will mix up to pre-defined config.
Returns Promise.

encodeSync(data, config)

data - Can be any type of data that you need to encode.
config - Additional configuration that will mix up to pre-defined config.
Works in sync mode, so it returns encoded data.

decode(data, config)

data - Can be only data that you receive after encode or encodeSync. Cipher will try to decode this to original state.
config - Additional configuration that will mix up to pre-defined config.
Returns Promise.

decodeSync(data, config)

data - Can be only data that you receive after encode or encodeSync. Cipher will try to decode this to original state.
config - Additional configuration that will mix up to pre-defined config.
Works in sync mode, so it returns decoded data.

Examples

JwtCipher

let jwt = CipherService('jwt', {
  secretKey: '<SECRET_KEY>', // Secret key for signing token
  algorithm: 'HS512', // Algorithm for signing
  expiresInMinutes: 60 * 24 // When this token will be expired
});

jwt.encode('SOME_DATA').then(console.log.bind(console)); // Encode SOME_DATA and print to console
jwt.decode('SOME_JWT_TOKEN').then(console.log.bind(console)); // Decode some token and print to console
console.log(jwt.encodeSync({foo: 'bar'})); // Encode object in sync mode and print to console JWT
console.log(jwt.decodeSync('SOME_JWT_TOKEN')); // Decode JWT and print to console result

How to add another cipher?

  • First of all you need to inherits from BaseCipher.

  • Your class should have 4 methods described above in API section.

License

The MIT License (MIT)
Copyright (c) 2015 Eugene Obrezkov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.