import config from 'config';
import jwt from 'jsonwebtoken';

const decode = jwt.decode;
const JsonWebTokenError = jwt.JsonWebTokenError;
const TokenExpiredError = jwt.TokenExpiredError;

/**
 * The algorithm to use for generating the JWT signature. The key length must
 * match the length of the hash function used.
 *
 * @see https://tools.ietf.org/html/rfc4868#section-2.1.1
 * @see https://github.com/auth0/node-jsonwebtoken#algorithms-supported
 */
const ALGO = config.get('jwt.algorithm');
const SECRET = Buffer.from(config.get('jwt.secret'), 'base64');

const verifyDefaults = {
  alorithms: ['HS512'],
};

const options = {
  algorithm: ALGO,
  expiresIn: '6h',
  notBefore: 0,
};

/**
 * Creates a JWT
 *
 * @throws JsonWebTokenError
 *
 * @param {Object} body - the body of the JWT
 * @returns {String} the signed JWT
 */
const create = function(body) {

  body = body || {};

  try {
    return jwt.sign(body, SECRET, options);
  } catch (caught) {
    let error = new JsonWebTokenError('Failed while signing JWT');
    Object.assign(error, caught, {name: 'JsonWebTokenError'});
    throw error;
  }
};


/**
 * Verifies if the JWT is valid and has not expired.
 * If its valid its returned otherwise an error is thrown
 *
 * @throws TokenExpiredError
 * @throws JsonWebTokenError
 *
 * @param {String} token - the JWT to be decoded
 * @param {Object} options - see docs for jsonwebtoken
 */
const verify = function(token, options) {
  options = Object.assign({}, verifyDefaults, options);
  return jwt.verify(token, SECRET, options);
};

export default {create, verify, decode, JsonWebTokenError, TokenExpiredError};

@@ -55,16 +55,23 @@
 oken';%0A%0A
+export 
 const de
@@ -89,16 +89,23 @@
 decode;%0A
+export 
 const Js
@@ -145,16 +145,23 @@
 nError;%0A
+export 
 const To
@@ -850,22 +850,14 @@
  */%0A
-const create =
+export
  fun
@@ -861,16 +861,23 @@
 function
+ create
 (body) %7B
@@ -1125,17 +1125,16 @@
 r;%0A  %7D%0A%7D
-;
 %0A%0A%0A/**%0A 
@@ -1419,22 +1419,14 @@
  */%0A
-const verify =
+export
  fun
@@ -1430,16 +1430,23 @@
 function
+ verify
 (token, 
@@ -1557,18 +1557,17 @@
 tions);%0A
+
 %7D
-;
 %0A%0Aexport

add named exports to lib/jwt