@rsksmart/express-did-auth
Version:
Authentication with Verifiable Credentials for Express.js
31 lines • 1.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChallengeVerifier = void 0;
const errors_1 = require("../errors");
const js_sha3_1 = require("js-sha3");
const defaults_1 = require("../defaults");
class ChallengeVerifier {
constructor({ challengeExpirationTime, challengeSecret }) {
this.expirationTime = challengeExpirationTime || defaults_1.CHALLENGE_EXPIRATION_TIME;
this.secret = challengeSecret;
}
get(did) {
if (!did)
throw new Error(errors_1.INVALID_DID);
return this.calculateChallenge(did);
}
verify(did, challenge) {
if (!did)
throw new Error(errors_1.INVALID_DID);
const expected = this.calculateChallenge(did);
if (expected !== challenge)
return false;
return true;
}
calculateChallenge(did) {
const timestamp = Math.floor(Date.now() / this.expirationTime);
return js_sha3_1.keccak256(`${did}-${this.secret}-${timestamp}`);
}
}
exports.ChallengeVerifier = ChallengeVerifier;
//# sourceMappingURL=challenge-verifier.js.map