blockstack
Version:
The Blockstack Javascript library for authentication, identity, and storage.
285 lines (264 loc) • 9.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.doSignaturesMatchPublicKeys = doSignaturesMatchPublicKeys;
exports.doPublicKeysMatchIssuer = doPublicKeysMatchIssuer;
exports.doPublicKeysMatchUsername = doPublicKeysMatchUsername;
exports.isIssuanceDateValid = isIssuanceDateValid;
exports.isExpirationDateValid = isExpirationDateValid;
exports.isManifestUriValid = isManifestUriValid;
exports.isRedirectUriValid = isRedirectUriValid;
exports.verifyAuthRequest = verifyAuthRequest;
exports.verifyAuthRequestAndLoadManifest = verifyAuthRequestAndLoadManifest;
exports.verifyAuthResponse = verifyAuthResponse;
var _jsontokens = require('jsontokens');
var _index = require('../index');
/**
* Checks if the ES256k signature on passed `token` match the claimed public key
* in the payload key `public_keys`.
*
* @param {String} token encoded and signed authentication token
* @return {Boolean} Returns `true` if the signature matches the claimed public key
* @throws {Error} if `token` contains multiple public keys
* @private
*/
function doSignaturesMatchPublicKeys(token) {
var payload = (0, _jsontokens.decodeToken)(token).payload;
var publicKeys = payload.public_keys;
if (publicKeys.length === 1) {
var publicKey = publicKeys[0];
try {
var tokenVerifier = new _jsontokens.TokenVerifier('ES256k', publicKey);
var signatureVerified = tokenVerifier.verify(token);
if (signatureVerified) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
} else {
throw new Error('Multiple public keys are not supported');
}
}
/**
* Makes sure that the identity address portion of
* the decentralized identifier passed in the issuer `iss`
* key of the token matches the public key
*
* @param {String} token encoded and signed authentication token
* @return {Boolean} if the identity address and public keys match
* @throws {Error} if ` token` has multiple public keys
* @private
*/
function doPublicKeysMatchIssuer(token) {
var payload = (0, _jsontokens.decodeToken)(token).payload;
var publicKeys = payload.public_keys;
var addressFromIssuer = (0, _index.getAddressFromDID)(payload.iss);
if (publicKeys.length === 1) {
var addressFromPublicKeys = (0, _index.publicKeyToAddress)(publicKeys[0]);
if (addressFromPublicKeys === addressFromIssuer) {
return true;
}
} else {
throw new Error('Multiple public keys are not supported');
}
return false;
}
/**
* Looks up the identity address that owns the claimed username
* in `token` using the lookup endpoint provided in `nameLookupURL`
* to determine if the username is owned by the identity address
* that matches the claimed public key
*
* @param {String} token encoded and signed authentication token
* @param {String} nameLookupURL a URL to the name lookup endpoint of the Blockstack Core API
* @return {Promise<Boolean>} returns a `Promise` that resolves to
* `true` if the username is owned by the public key, otherwise the
* `Promise` resolves to `false`
* @private
*/
function doPublicKeysMatchUsername(token, nameLookupURL) {
return new Promise(function (resolve) {
var payload = (0, _jsontokens.decodeToken)(token).payload;
if (!payload.username) {
resolve(true);
return;
}
if (payload.username === null) {
resolve(true);
return;
}
if (nameLookupURL === null) {
resolve(false);
return;
}
var username = payload.username;
var url = nameLookupURL.replace(/\/$/, '') + '/' + username;
try {
fetch(url).then(function (response) {
return response.text();
}).then(function (responseText) {
return JSON.parse(responseText);
}).then(function (responseJSON) {
if (responseJSON.hasOwnProperty('address')) {
var nameOwningAddress = responseJSON.address;
var addressFromIssuer = (0, _index.getAddressFromDID)(payload.iss);
if (nameOwningAddress === addressFromIssuer) {
resolve(true);
} else {
resolve(false);
}
} else {
resolve(false);
}
}).catch(function () {
resolve(false);
});
} catch (e) {
resolve(false);
}
});
}
/**
* Checks if the if the token issuance time and date is after the
* current time and date.
*
* @param {String} token encoded and signed authentication token
* @return {Boolean} `true` if the token was issued after the current time,
* otherwise returns `false`
* @private
*/
function isIssuanceDateValid(token) {
var payload = (0, _jsontokens.decodeToken)(token).payload;
if (payload.iat) {
if (typeof payload.iat !== 'number') {
return false;
}
var issuedAt = new Date(payload.iat * 1000); // JWT times are in seconds
if (new Date().getTime() < issuedAt.getTime()) {
return false;
} else {
return true;
}
} else {
return true;
}
}
/**
* Checks if the expiration date of the `token` is before the current time
* @param {String} token encoded and signed authentication token
* @return {Boolean} `true` if the `token` has not yet expired, `false`
* if the `token` has expired
*
* @private
*/
function isExpirationDateValid(token) {
var payload = (0, _jsontokens.decodeToken)(token).payload;
if (payload.exp) {
if (typeof payload.exp !== 'number') {
return false;
}
var expiresAt = new Date(payload.exp * 1000); // JWT times are in seconds
if (new Date().getTime() > expiresAt.getTime()) {
return false;
} else {
return true;
}
} else {
return true;
}
}
/**
* Makes sure the `manifest_uri` is a same origin absolute URL.
* @param {String} token encoded and signed authentication token
* @return {Boolean} `true` if valid, otherwise `false`
* @private
*/
function isManifestUriValid(token) {
var payload = (0, _jsontokens.decodeToken)(token).payload;
return (0, _index.isSameOriginAbsoluteUrl)(payload.domain_name, payload.manifest_uri);
}
/**
* Makes sure the `redirect_uri` is a same origin absolute URL.
* @param {String} token encoded and signed authentication token
* @return {Boolean} `true` if valid, otherwise `false`
* @private
*/
function isRedirectUriValid(token) {
var payload = (0, _jsontokens.decodeToken)(token).payload;
return (0, _index.isSameOriginAbsoluteUrl)(payload.domain_name, payload.redirect_uri);
}
/**
* Verify authentication request is valid. This function performs a number
* of checks on the authentication request token:
* * Checks that `token` has a valid issuance date & is not expired
* * Checks that `token` has a valid signature that matches the public key it claims
* * Checks that both the manifest and redirect URLs are absolute and conform to
* the same origin policy
*
* @param {String} token encoded and signed authentication request token
* @return {Promise} that resolves to true if the auth request
* is valid and false if it does not. It rejects with a String if the
* token is not signed
* @private
*/
function verifyAuthRequest(token) {
return new Promise(function (resolve, reject) {
if ((0, _jsontokens.decodeToken)(token).header.alg === 'none') {
reject('Token must be signed in order to be verified');
}
Promise.all([isExpirationDateValid(token), isIssuanceDateValid(token), doSignaturesMatchPublicKeys(token), doPublicKeysMatchIssuer(token), isManifestUriValid(token), isRedirectUriValid(token)]).then(function (values) {
if (values.every(Boolean)) {
resolve(true);
} else {
resolve(false);
}
});
});
}
/**
* Verify the authentication request is valid and
* fetch the app manifest file if valid. Otherwise, reject the promise.
* @param {String} token encoded and signed authentication request token
* @return {Promise} that resolves to the app manifest file in JSON format
* or rejects if the auth request or app manifest file is invalid
* @private
*/
function verifyAuthRequestAndLoadManifest(token) {
return new Promise(function (resolve, reject) {
return verifyAuthRequest(token).then(function (valid) {
if (valid) {
return (0, _index.fetchAppManifest)(token).then(function (appManifest) {
resolve(appManifest);
}).catch(function (err) {
reject(err);
});
} else {
reject();
return Promise.reject();
}
});
});
}
/**
* Verify the authentication response is valid
* @param {String} token the authentication response token
* @param {String} nameLookupURL the url use to verify owner of a username
* @return {Promise} that resolves to true if auth response
* is valid and false if it does not
* @private
*/
function verifyAuthResponse(token, nameLookupURL) {
return new Promise(function (resolve) {
Promise.all([isExpirationDateValid(token), isIssuanceDateValid(token), doSignaturesMatchPublicKeys(token), doPublicKeysMatchIssuer(token), doPublicKeysMatchUsername(token, nameLookupURL)]).then(function (values) {
if (values.every(Boolean)) {
resolve(true);
} else {
resolve(false);
}
});
});
}