astroboy
Version:
Astroboy(阿童木)is a Nodejs SFB(Separation of Front and Back ends) framework, built on koa2.
45 lines • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const crypto = require("crypto");
const randomString_1 = require("./randomString");
class Token {
constructor(options = {}) {
this.options = Object.assign({}, this.defaultOptions, options);
this.saltLength = this.options.saltLength;
this.secretLength = this.options.secretLength;
}
get defaultOptions() {
return {
saltLength: 10,
secretLength: 18,
};
}
secretSync() {
return randomString_1.randomString(this.secretLength);
}
create(secret) {
const salt = randomString_1.randomString(this.saltLength);
return this.tokenize(secret, salt);
}
tokenize(secret, salt) {
const hash = crypto
.createHash('sha1')
.update(secret, 'utf8')
.digest('base64');
return salt + '-' + hash;
}
verify(secret, token) {
if (!secret || !token || typeof secret !== 'string' || typeof token !== 'string') {
return false;
}
const index = token.indexOf('-');
if (index === -1) {
return false;
}
const salt = token.substr(0, index);
const expected = this.tokenize(secret, salt);
return expected === token;
}
}
exports.Token = Token;
//# sourceMappingURL=token.js.map