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

feat: ESSR tunneling with KERIA API #304

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/integration-scripts/test-setup-single-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ describe('test-setup-single-client', () => {
'http://127.0.0.1:5642/oobi/BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha/controller?name=Wan&tag=witness',
'http://127.0.0.1:5643/oobi/BLskRTInXnMxWaGqcpSyMgo0nYbalW99cGZESrz3zapM/controller?name=Wes&tag=witness',
'http://127.0.0.1:5644/oobi/BIKKuvBwpmDVA4Ds-EpL5bt9OqPzWPja2LigFYZN2YfX/controller?name=Wil&tag=witness',
'http://127.0.0.1:5645/oobi/BM35JN8XeJSEfpxopjn5jr7tAHCE5749f0OobhMLCorE/controller?name=Wit&tag=witness',
'http://127.0.0.1:5646/oobi/BIj15u5V11bkbtAxMA7gcNJZcax-7TgaBMLsQnMHpYHP/controller?name=Wub&tag=witness',
'http://127.0.0.1:5647/oobi/BF2rZTW79z4IXocYRQnjjsOuvFUQv-ptCf8Yltd7PfsM/controller?name=Wyz&tag=witness',
],
});
break;
Expand Down
103 changes: 50 additions & 53 deletions src/keri/app/clienting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Authenticater } from '../core/authing';
import { HEADER_SIG_TIME } from '../core/httping';
import { Authenticator } from '../core/authing';
import { HEADER_SIG_SENDER, HEADER_SIG_TIME } from '../core/httping';
import { ExternalModule, KeyManager } from '../core/keeping';
import { Tier } from '../core/salter';

Expand Down Expand Up @@ -37,7 +37,7 @@ export class SignifyClient {
public bran: string;
public pidx: number;
public agent: Agent | null;
public authn: Authenticater | null;
public authn: Authenticator | null;
public manager: KeyManager | null;
public tier: Tier;
public bootUrl: string;
Expand Down Expand Up @@ -151,7 +151,7 @@ export class SignifyClient {
this.controller.salter,
this.exteralModules
);
this.authn = new Authenticater(
this.authn = new Authenticator(
this.controller.signer,
this.agent.verfer!
);
Expand All @@ -172,63 +172,60 @@ export class SignifyClient {
data: any,
extraHeaders?: Headers
): Promise<Response> {
const headers = new Headers();
let signed_headers = new Headers();
const final_headers = new Headers();

headers.set('Signify-Resource', this.controller.pre);
headers.set(
HEADER_SIG_TIME,
new Date().toISOString().replace('Z', '000+00:00')
);
headers.set('Content-Type', 'application/json');

const _body = method == 'GET' ? null : JSON.stringify(data);

if (this.authn) {
signed_headers = this.authn.sign(
headers,
method,
path.split('?')[0]
);
} else {
throw new Error('client need to call connect first');
if (!this.authn) {
throw new Error('Client needs to call connect first');
}

signed_headers.forEach((value, key) => {
final_headers.set(key, value);
});
if (extraHeaders !== undefined) {
const headers = new Headers();
headers.set(HEADER_SIG_SENDER, this.controller.pre);

if (extraHeaders) {
extraHeaders.forEach((value, key) => {
final_headers.append(key, value);
headers.append(key, value);
});
}
const res = await fetch(this.url + path, {
method: method,
body: _body,
headers: final_headers,
});
if (!res.ok) {
const error = await res.text();
const message = `HTTP ${method} ${path} - ${res.status} ${res.statusText} - ${error}`;
throw new Error(message);
}
const isSameAgent =
this.agent?.pre === res.headers.get('signify-resource');
if (!isSameAgent) {
throw new Error('message from a different remote agent');

const body = method == 'GET' ? null : JSON.stringify(data);
if (body) {
headers.set('Content-Type', 'application/json');
headers.set('Content-Length', body.length.toString());
}

const verification = this.authn.verify(
res.headers,
const request = new Request(this.url + path, {
method,
path.split('?')[0]
body,
headers,
});

const wrappedRequest = await this.authn.wrap(
request,
this.url,
this.controller.pre,
this.agent!.pre
);
const wrappedResponse = await fetch(wrappedRequest);

// Any other error will be wrapped in an ESSR response
if (wrappedResponse.status === 401) {
throw new Error(
`HTTP ${method} ${path} - ${wrappedResponse.status} ${wrappedResponse.statusText}`
);
}

const response = await this.authn.unwrap(
wrappedResponse,
this.agent!.pre,
this.controller.pre
);
if (verification) {
return res;
} else {
throw new Error('response verification failed');

if (!response.ok) {
const error = await response.text();
throw new Error(
`HTTP ${method} ${path} - ${response.status} ${response.statusText} - ${error}`
);
}

return response;
}

/**
Expand All @@ -253,13 +250,13 @@ export class SignifyClient {
const hab = await this.identifiers().get(aidName);
const keeper = this.manager!.get(hab);

const authenticator = new Authenticater(
const authenticator = new Authenticator(
keeper.signers[0],
keeper.signers[0].verfer
);

const headers = new Headers(req.headers);
headers.set('Signify-Resource', hab['prefix']);
headers.set(HEADER_SIG_SENDER, hab['prefix']);
headers.set(
HEADER_SIG_TIME,
new Date().toISOString().replace('Z', '000+00:00')
Expand Down
Loading
Loading