var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { DwnErrorCode } from '../../src/core/dwn-error.js';
import { expect } from 'chai';
import { TestDataGenerator } from '../utils/test-data-generator.js';
import { authenticate, validateAuthorizationIntegrity } from '../../src/core/auth.js';
import { DidResolver, RecordsRead } from '../../src/index.js';
import { Did } from '../../src/did/did.js';
import { DidKeyResolver } from '../../src/did/did-key-resolver.js';
import { Jws } from '../../src/utils/jws.js';
import sinon from 'sinon';
describe('Auth', () => {
    if (process.env.TESTSOFF === 'true') {
        return;
    }
    describe('validateAuthorizationIntegrity()', () => {
        it('should throw if given message does not have `authorization` property', () => __awaiter(void 0, void 0, void 0, function* () {
            // create a message without `authorization`
            const recordsRead = yield RecordsRead.create({
                recordId: yield TestDataGenerator.randomCborSha256Cid()
            });
            yield expect(validateAuthorizationIntegrity(recordsRead.message)).to.be.rejectedWith(DwnErrorCode.AuthorizationMissing);
        }));
    });
    describe('authenticate()', () => {
        it('should pass with given JWS', () => __awaiter(void 0, void 0, void 0, function* () {
            var _a, _b, _c;
            const bob = yield DidKeyResolver.generate();
            console.log('bob', bob);
            expect(() => Did.validate(bob.did)).to.not.throw();
            const didKeyResolver = new DidKeyResolver();
            const sig = Jws.createSignatureInput(bob);
            console.log('sig', sig);
            const networkAvailable = false;
            // stub network call if network is not available
            if (!networkAvailable) {
                sinon.stub(globalThis, 'fetch').resolves({
                    status: 200,
                    json: () => __awaiter(void 0, void 0, void 0, function* () {
                        return Promise.resolve({
                            didDocument: { id: bob.did },
                            didDocumentMetadata: { canonicalId: bob.did }
                        });
                    })
                });
            }
            const resolutionDocument = yield didKeyResolver.resolve(bob.did);
            console.log('resolutionDocument', JSON.stringify(resolutionDocument, null, 2));
            // generate authentication message
            // this message contains the user lookup, roles, perms of the app using it
            const { message } = yield TestDataGenerator.generateRecordsWrite({ author: bob });
            const recordsRead = yield RecordsRead.create({
                recordId: message.recordId,
                authorizationSignatureInput: sig
            });
            yield expect(validateAuthorizationIntegrity(recordsRead.message)).to.not.be.rejectedWith(DwnErrorCode.AuthorizationMissing);
            expect((_a = resolutionDocument.didDocument) === null || _a === void 0 ? void 0 : _a.id).to.equal(bob.did);
            const verificationMethod = (_b = resolutionDocument.didDocument) === null || _b === void 0 ? void 0 : _b.verificationMethod[0];
            expect((_c = verificationMethod.publicKeyJwk) === null || _c === void 0 ? void 0 : _c.x).to.equal(bob.keyPair.publicJwk.x);
            const jws = undefined;
            yield expect(authenticate(jws, new DidResolver)).to.be.rejectedWith(DwnErrorCode.AuthenticateJwsMissing);
        }));
        it('should throw if given JWS is `undefined`', () => __awaiter(void 0, void 0, void 0, function* () {
            const jws = undefined;
            yield expect(authenticate(jws, new DidResolver)).to.be.rejectedWith(DwnErrorCode.AuthenticateJwsMissing);
        }));
    });
});
//# sourceMappingURL=auth.spec.js.map