@webgap/token
Version:
WebGAP Tokenizer module. Handles JWT encoding / decoding features.
75 lines (66 loc) • 1.91 kB
JavaScript
/**
* (C) Copyright 2014 WebGAP (http://www.webgap.eu/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created by: ManuelMartins
* Created on: 23-05-2014
*
*/
;
var AbstractAlgorithm = require('./abstract.js');
var util = require('util');
/**
* Token utilities
*
* @param {Object} options
* @param {string} options.secret
* @constructor
*/
function HS512Algorithm(options) {
HS512Algorithm.super_.apply(this, arguments);
var self = this;
options = options || {};
// algorithm
self.ALGORITHM = 'HS512';
if (!options.secret) {
console.error('Parameter \'options.secret\' is missing.');
throw new Error('Parameter \'options.secret\' is missing.');
}
self.secret = options.secret;
}
/* inherit AbstractAlgorithm Module */
util.inherits(HS512Algorithm, AbstractAlgorithm);
/**
* {@inheritDoc}
*/
HS512Algorithm.prototype.encode = function (data) {
return this.jwt.encode(this.generateJWTObject(data), this.secret, this.ALGORITHM);
};
/**
* {@inheritDoc}
*/
HS512Algorithm.prototype.decode = function (token, complete) {
var decodedToken = null;
try {
decodedToken = this.jwt.decode(token, this.secret);
if (!complete) {
decodedToken = decodedToken.data;
}
} catch (error) {
console.warn("Could not decode token! " + error);
decodedToken = false;
}
return decodedToken;
};
module.exports = HS512Algorithm;