Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signedFetch more generic/configurable aligning it with fetch api #259

Merged
merged 10 commits into from
May 16, 2024
4 changes: 2 additions & 2 deletions examples/integration-scripts/singlesig-vlei-issuance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ const OOR_AUTH_RULES = LE_RULES;

const CRED_RETRY_DEFAULTS = {
maxSleep: 1000,
minSleep: 10,
minSleep: 1000,
2byrds marked this conversation as resolved.
Show resolved Hide resolved
maxRetries: 5,
timeout: 10000,
timeout: 30000,
};

interface Aid {
Expand Down
14 changes: 6 additions & 8 deletions src/keri/app/clienting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,21 @@ export class SignifyClient {
* Fetch a resource from from an external URL with headers signed by an AID
2byrds marked this conversation as resolved.
Show resolved Hide resolved
* @async
* @param {string} aidName Name or alias of the AID to be used for signing
* @param {string} url URL of the resource
* @param {string} path Path to the resource
* @param {string} url URL of the requested resource
* @param {RequestInit} req Request options should include:
* - method: HTTP method
* - data Data to be sent in the body of the resource.
* If the data is a CESR JSON string then you should also set contentType to 'application/json+cesr'
* If the data is a FormData object then you should not set the contentType and the browser will set it to 'multipart/form-data'
* If the data is an object then you should use JSON.stringify to convert it to a string and set the contentType to 'application/json'
* - contentType Content type of the request.
* @returns {Promise<Response>} A promise to the result of the fetch
* @returns {Promise<Request>} A promise to the result of the fetch
2byrds marked this conversation as resolved.
Show resolved Hide resolved
*/
async signedFetch(
async createSignedRequest(
aidName: string,
url: string,
path: string,
req: RequestInit
): Promise<Response> {
): Promise<Request> {
const hab = await this.identifiers().get(aidName);
const keeper = this.manager!.get(hab);

Expand All @@ -269,11 +267,11 @@ export class SignifyClient {
const signed_headers = authenticator.sign(
new Headers(headers),
req.method ?? 'GET',
path.split('?')[0]
new URL(url).pathname
);
req.headers = signed_headers;

return await fetch(url + path, req);
return new Request(url,req);
}

/**
Expand Down
26 changes: 11 additions & 15 deletions test/app/clienting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import { Groups } from '../../src/keri/app/grouping';
import { Notifications } from '../../src/keri/app/notifying';

import { Authenticater } from '../../src/keri/core/authing';
import { Cigar } from '../../src/keri/core/cigar';
import { HEADER_SIG_INPUT, HEADER_SIG_TIME } from '../../src/keri/core/httping';
import { SaltyKeeper } from '../../src/keri/core/keeping';
import { Salter, Tier } from '../../src/keri/core/salter';
import libsodium from 'libsodium-wrappers-sumo';
import fetchMock from 'jest-fetch-mock';
Expand Down Expand Up @@ -364,23 +362,21 @@ describe('SignifyClient', () => {

let heads = new Headers();
heads.set('Content-Type', 'application/json');
let reqInit = {
let treqInit = {
headers: heads,
method: 'POST',
body: JSON.stringify({ foo: true }),
};
resp = await client.signedFetch(
'aid1',
'http://example.com',
'/test',
reqInit
);
let turl = 'http://example.com/test';
let treq = await client.createSignedRequest('aid1', turl, treqInit);
let tres = await fetch(treq);
lastCall = fetchMock.mock.calls[fetchMock.mock.calls.length - 1]!;
assert.equal(lastCall[0]!, 'http://example.com/test');
assert.equal(lastCall[1]!.method, 'POST');
lastBody = JSON.parse(lastCall[1]!.body!);
let resReq = (lastCall[0] as Request)
assert.equal(resReq.url, 'http://example.com/test');
assert.equal(resReq.method, 'POST');
lastBody = await resReq.json();
assert.deepEqual(lastBody.foo, true);
lastHeaders = new Headers(lastCall[1]!.headers!);
lastHeaders = new Headers(resReq.headers);
assert.equal(
lastHeaders.get('signify-resource'),
'ELUvZ8aJEHAQE-0nsevyYTP98rBbGJUrTj5an-pCmwrK'
Expand All @@ -403,7 +399,7 @@ describe('SignifyClient', () => {
);

let aid = await client.identifiers().get('aid1');
const keeper = client.manager!.get(aid) as SaltyKeeper;
const keeper = client.manager!.get(aid);
const signer = keeper.signers[0];
const created = lastHeaders
.get(HEADER_SIG_INPUT)
Expand All @@ -415,7 +411,7 @@ describe('SignifyClient', () => {

if (data) {
const raw = new TextEncoder().encode(data);
const sig = signer.sign(raw, null) as Cigar;
const sig = signer.sign(raw);
assert.equal(
sig.qb64,
lastHeaders
Expand Down
Loading