blockstack-auth
Version:
Blockstack Auth Library
102 lines (86 loc) • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AuthResponse = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _keyEncoder = require('key-encoder');
var _keyEncoder2 = _interopRequireDefault(_keyEncoder);
var _jsontokens = require('jsontokens');
var _ellipticCurve = require('elliptic-curve');
var _nodeUuid = require('node-uuid');
var _nodeUuid2 = _interopRequireDefault(_nodeUuid);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var AuthResponse = exports.AuthResponse = function () {
function AuthResponse(privateKey) {
_classCallCheck(this, AuthResponse);
this.privateKey = privateKey;
this.keyEncoder = new _keyEncoder2.default('secp256k1');
this.publicKey = _ellipticCurve.secp256k1.getPublicKey(privateKey);
this.tokenSigner = new _jsontokens.TokenSigner('ES256k', privateKey);
this.issuer = { publicKey: this.publicKey };
}
_createClass(AuthResponse, [{
key: 'satisfyProvisions',
value: function satisfyProvisions(provisions, username, privateData) {
var _this = this;
provisions.forEach(function (provision) {
switch (provision.action) {
case 'disclose':
if (provision.scope === 'username' && username) {
provision.data = username;
}
break;
case 'sign':
if (provision.data) {
var signature = _ellipticCurve.secp256k1.signMessage(provision.data, _this.privateKey);
provision.signature = signature;
}
break;
case 'write':
break;
default:
break;
}
});
this.provisions = provisions;
}
}, {
key: 'setIssuer',
value: function setIssuer(username, publicKeychain, chainPath) {
if (username && publicKeychain && chainPath) {
this.issuer = {
publicKey: this.publicKey,
username: username,
publicKeychain: publicKeychain,
chainPath: chainPath
};
} else if (username) {
this.issuer = {
publicKey: this.publicKey,
username: username
};
} else if (username || publicKeychain || chainPath) {
throw 'Either all or none of the following must be provided: username, publicKeychain, chainPath';
} else {
throw 'Cannot set issuer without the following: username, publicKeychain, chainPath';
}
}
}, {
key: 'payload',
value: function payload() {
return {
issuer: this.issuer,
issuedAt: new Date().getTime(),
provisions: this.provisions
};
}
}, {
key: 'sign',
value: function sign() {
return this.tokenSigner.sign(this.payload());
}
}]);
return AuthResponse;
}();