"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("mocha");
const chai_1 = require("chai");
const lodash_1 = require("lodash");
const shared_1 = require("../../shared");
const core_utils_1 = require("../../../../shared/core-utils");
const activitypub_1 = require("../../../helpers/activitypub");
const peertube_crypto_1 = require("../../../helpers/peertube-crypto");
describe('Test activity pub helpers', function () {
    describe('When checking the Linked Signature', function () {
        it('Should fail with an invalid Mastodon signature', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const body = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/create-bad-signature.json'));
                const publicKey = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/public-key.json')).publicKey;
                const fromActor = { publicKey, url: 'http://localhost:9002/accounts/peertube' };
                const result = yield (0, peertube_crypto_1.isJsonLDSignatureVerified)(fromActor, body);
                (0, chai_1.expect)(result).to.be.false;
            });
        });
        it('Should fail with an invalid public key', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const body = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/create.json'));
                const publicKey = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/bad-public-key.json')).publicKey;
                const fromActor = { publicKey, url: 'http://localhost:9002/accounts/peertube' };
                const result = yield (0, peertube_crypto_1.isJsonLDSignatureVerified)(fromActor, body);
                (0, chai_1.expect)(result).to.be.false;
            });
        });
        it('Should succeed with a valid Mastodon signature', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const body = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/create.json'));
                const publicKey = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/public-key.json')).publicKey;
                const fromActor = { publicKey, url: 'http://localhost:9002/accounts/peertube' };
                const result = yield (0, peertube_crypto_1.isJsonLDSignatureVerified)(fromActor, body);
                (0, chai_1.expect)(result).to.be.true;
            });
        });
        it('Should fail with an invalid PeerTube signature', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const keys = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/peertube/invalid-keys.json'));
                const body = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/peertube/announce-without-context.json'));
                const actorSignature = { url: 'http://localhost:9002/accounts/peertube', privateKey: keys.privateKey };
                const signedBody = yield (0, activitypub_1.buildSignedActivity)(actorSignature, body);
                const fromActor = { publicKey: keys.publicKey, url: 'http://localhost:9002/accounts/peertube' };
                const result = yield (0, peertube_crypto_1.isJsonLDSignatureVerified)(fromActor, signedBody);
                (0, chai_1.expect)(result).to.be.false;
            });
        });
        it('Should succeed with a valid PeerTube signature', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const keys = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/peertube/keys.json'));
                const body = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/peertube/announce-without-context.json'));
                const actorSignature = { url: 'http://localhost:9002/accounts/peertube', privateKey: keys.privateKey };
                const signedBody = yield (0, activitypub_1.buildSignedActivity)(actorSignature, body);
                const fromActor = { publicKey: keys.publicKey, url: 'http://localhost:9002/accounts/peertube' };
                const result = yield (0, peertube_crypto_1.isJsonLDSignatureVerified)(fromActor, signedBody);
                (0, chai_1.expect)(result).to.be.true;
            });
        });
    });
    describe('When checking HTTP signature', function () {
        it('Should fail with an invalid http signature', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const req = (0, shared_1.buildRequestStub)();
                req.method = 'POST';
                req.url = '/accounts/ronan/inbox';
                const mastodonObject = (0, lodash_1.cloneDeep)(require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/bad-http-signature.json')));
                req.body = mastodonObject.body;
                req.headers = mastodonObject.headers;
                const parsed = (0, peertube_crypto_1.parseHTTPSignature)(req, 3600 * 1000 * 365 * 10);
                const publicKey = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/public-key.json')).publicKey;
                const actor = { publicKey };
                const verified = (0, peertube_crypto_1.isHTTPSignatureVerified)(parsed, actor);
                (0, chai_1.expect)(verified).to.be.false;
            });
        });
        it('Should fail with an invalid public key', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const req = (0, shared_1.buildRequestStub)();
                req.method = 'POST';
                req.url = '/accounts/ronan/inbox';
                const mastodonObject = (0, lodash_1.cloneDeep)(require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/http-signature.json')));
                req.body = mastodonObject.body;
                req.headers = mastodonObject.headers;
                const parsed = (0, peertube_crypto_1.parseHTTPSignature)(req, 3600 * 1000 * 365 * 10);
                const publicKey = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/bad-public-key.json')).publicKey;
                const actor = { publicKey };
                const verified = (0, peertube_crypto_1.isHTTPSignatureVerified)(parsed, actor);
                (0, chai_1.expect)(verified).to.be.false;
            });
        });
        it('Should fail because of clock skew', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const req = (0, shared_1.buildRequestStub)();
                req.method = 'POST';
                req.url = '/accounts/ronan/inbox';
                const mastodonObject = (0, lodash_1.cloneDeep)(require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/http-signature.json')));
                req.body = mastodonObject.body;
                req.headers = mastodonObject.headers;
                let errored = false;
                try {
                    (0, peertube_crypto_1.parseHTTPSignature)(req);
                }
                catch (_a) {
                    errored = true;
                }
                (0, chai_1.expect)(errored).to.be.true;
            });
        });
        it('Should with a scheme', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const req = (0, shared_1.buildRequestStub)();
                req.method = 'POST';
                req.url = '/accounts/ronan/inbox';
                const mastodonObject = (0, lodash_1.cloneDeep)(require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/http-signature.json')));
                req.body = mastodonObject.body;
                req.headers = mastodonObject.headers;
                req.headers = 'Signature ' + mastodonObject.headers;
                let errored = false;
                try {
                    (0, peertube_crypto_1.parseHTTPSignature)(req, 3600 * 1000 * 365 * 10);
                }
                catch (_a) {
                    errored = true;
                }
                (0, chai_1.expect)(errored).to.be.true;
            });
        });
        it('Should succeed with a valid signature', function () {
            return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
                const req = (0, shared_1.buildRequestStub)();
                req.method = 'POST';
                req.url = '/accounts/ronan/inbox';
                const mastodonObject = (0, lodash_1.cloneDeep)(require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/http-signature.json')));
                req.body = mastodonObject.body;
                req.headers = mastodonObject.headers;
                const parsed = (0, peertube_crypto_1.parseHTTPSignature)(req, 3600 * 1000 * 365 * 10);
                const publicKey = require((0, core_utils_1.buildAbsoluteFixturePath)('./ap-json/mastodon/public-key.json')).publicKey;
                const actor = { publicKey };
                const verified = (0, peertube_crypto_1.isHTTPSignatureVerified)(parsed, actor);
                (0, chai_1.expect)(verified).to.be.true;
            });
        });
    });
});
//# sourceMappingURL=helpers.js.map