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

fix credentialing field ordering and related singlesig-vlei-issuance test #254

Merged
merged 23 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
62 changes: 38 additions & 24 deletions examples/integration-scripts/singlesig-vlei-issuance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,18 @@ test('singlesig-vlei-issuance', async function run() {

let qviCredHolder = await getGrantedCredential(qviClient, qviCred.sad.d);

if (!qviCredHolder) {
let qviRetry = 0;
while (!qviCredHolder && qviRetry < 5) {
await sendGrantMessage(gleifClient, gleifAid, qviAid, qviCred);
await sendAdmitMessage(qviClient, qviAid, gleifAid);

qviCredHolder = await retry(async () => {
const cred = await getGrantedCredential(qviClient, qviCred.sad.d);
assert(cred !== undefined);
return cred;
qviRetry += 1;
lenkan marked this conversation as resolved.
Show resolved Hide resolved
if (cred !== undefined) return cred;
});
}
assert(qviCredHolder !== undefined);
assert.equal(qviCredHolder.sad.d, qviCred.sad.d);
assert.equal(qviCredHolder.sad.s, QVI_SCHEMA_SAID);
assert.equal(qviCredHolder.sad.i, gleifAid.prefix);
Expand Down Expand Up @@ -224,16 +226,18 @@ test('singlesig-vlei-issuance', async function run() {

let leCredHolder = await getGrantedCredential(leClient, leCred.sad.d);

if (!leCredHolder) {
let leRetry = 0;
while (!leCredHolder && leRetry < 5) {
await sendGrantMessage(qviClient, qviAid, leAid, leCred);
await sendAdmitMessage(leClient, leAid, qviAid);

leRetry += 1;
leCredHolder = await retry(async () => {
const cred = await getGrantedCredential(leClient, leCred.sad.d);
assert(cred !== undefined);
return cred;

if (cred !== undefined) return cred;
});
}
assert(leCredHolder !== undefined);
assert.equal(leCredHolder.sad.d, leCred.sad.d);
assert.equal(leCredHolder.sad.s, LE_SCHEMA_SAID);
assert.equal(leCredHolder.sad.i, qviAid.prefix);
Expand Down Expand Up @@ -265,16 +269,18 @@ test('singlesig-vlei-issuance', async function run() {

let ecrCredHolder = await getGrantedCredential(roleClient, ecrCred.sad.d);

if (!ecrCredHolder) {
let ecrRetry = 0;
while (!ecrCredHolder && ecrRetry < 5) {
await sendGrantMessage(leClient, leAid, roleAid, ecrCred);
await sendAdmitMessage(roleClient, roleAid, leAid);

ecrCredHolder = await retry(async () => {
const cred = await getGrantedCredential(roleClient, ecrCred.sad.d);
assert(cred !== undefined);
return cred;
ecrRetry += 1;
if (cred !== undefined) return cred;
});
}
assert(ecrCredHolder !== undefined);
assert.equal(ecrCredHolder.sad.d, ecrCred.sad.d);
assert.equal(ecrCredHolder.sad.s, ECR_SCHEMA_SAID);
assert.equal(ecrCredHolder.sad.i, leAid.prefix);
Expand Down Expand Up @@ -309,7 +315,8 @@ test('singlesig-vlei-issuance', async function run() {
ecrAuthCred.sad.d
);

if (!ecrAuthCredHolder) {
let ecrAuthRetry = 0;
while (!ecrAuthCredHolder && ecrAuthRetry < 5) {
await sendGrantMessage(leClient, leAid, qviAid, ecrAuthCred);
await sendAdmitMessage(qviClient, qviAid, leAid);

Expand All @@ -318,10 +325,11 @@ test('singlesig-vlei-issuance', async function run() {
qviClient,
ecrAuthCred.sad.d
);
assert(cred !== undefined);
return cred;
ecrAuthRetry += 1;
if (cred !== undefined) return cred;
});
}
assert(ecrAuthCredHolder !== undefined);
assert.equal(ecrAuthCredHolder.sad.d, ecrAuthCred.sad.d);
assert.equal(ecrAuthCredHolder.sad.s, ECR_AUTH_SCHEMA_SAID);
assert.equal(ecrAuthCredHolder.sad.i, leAid.prefix);
Expand Down Expand Up @@ -355,16 +363,18 @@ test('singlesig-vlei-issuance', async function run() {

let ecrCredHolder2 = await getGrantedCredential(roleClient, ecrCred2.sad.d);

if (!ecrCredHolder2) {
let ecr2Retry = 0;
while (!ecrCredHolder2 && ecr2Retry < 5) {
await sendGrantMessage(qviClient, qviAid, roleAid, ecrCred2);
await sendAdmitMessage(roleClient, roleAid, qviAid);

ecrCredHolder2 = await retry(async () => {
const cred = await getGrantedCredential(roleClient, ecrCred2.sad.d);
assert(cred !== undefined);
return cred;
ecr2Retry += 1;
if (cred !== undefined) return cred;
});
}
assert(ecrCredHolder2 !== undefined);
assert.equal(ecrCredHolder2.sad.d, ecrCred2.sad.d);
assert.equal(ecrCredHolder2.sad.s, ECR_SCHEMA_SAID);
assert.equal(ecrCredHolder2.sad.i, qviAid.prefix);
Expand Down Expand Up @@ -398,7 +408,8 @@ test('singlesig-vlei-issuance', async function run() {
oorAuthCred.sad.d
);

if (!oorAuthCredHolder) {
let oorAuthRetry = 0;
while (!oorAuthCredHolder && oorAuthRetry < 5) {
await sendGrantMessage(leClient, leAid, qviAid, oorAuthCred);
await sendAdmitMessage(qviClient, qviAid, leAid);

Expand All @@ -407,10 +418,11 @@ test('singlesig-vlei-issuance', async function run() {
qviClient,
oorAuthCred.sad.d
);
assert(cred !== undefined);
return cred;
oorAuthRetry += 1;
if (cred !== undefined) return cred;
});
}
assert(oorAuthCredHolder !== undefined);
assert.equal(oorAuthCredHolder.sad.d, oorAuthCred.sad.d);
assert.equal(oorAuthCredHolder.sad.s, OOR_AUTH_SCHEMA_SAID);
assert.equal(oorAuthCredHolder.sad.i, leAid.prefix);
Expand Down Expand Up @@ -443,16 +455,18 @@ test('singlesig-vlei-issuance', async function run() {

let oorCredHolder = await getGrantedCredential(roleClient, oorCred.sad.d);

if (!oorCredHolder) {
let oorRetry = 0;
while (!oorCredHolder && oorRetry < 5) {
await sendGrantMessage(qviClient, qviAid, roleAid, oorCred);
await sendAdmitMessage(roleClient, roleAid, qviAid);

oorCredHolder = await retry(async () => {
const cred = await getGrantedCredential(roleClient, oorCred.sad.d);
assert(cred !== undefined);
return cred;
oorRetry += 1;
if (cred !== undefined) return cred;
});
}
assert(oorCredHolder !== undefined);
assert.equal(oorCredHolder.sad.d, oorCred.sad.d);
assert.equal(oorCredHolder.sad.s, OOR_SCHEMA_SAID);
assert.equal(oorCredHolder.sad.i, qviAid.prefix);
Expand Down Expand Up @@ -506,16 +520,16 @@ async function getOrIssueCredential(
}

const issResult = await issuerClient.credentials().issue(issuerAid.name, {
u: privacy ? new Salter({}).qb64 : undefined,
ri: issuerRegistry.regk,
s: schema,
u: privacy ? new Salter({}).qb64 : undefined,
a: {
i: recipientAid.prefix,
u: privacy ? new Salter({}).qb64 : undefined,
...credData,
},
r: rules,
e: source,
r: rules,
});

await waitOperation(issuerClient, issResult.op);
Expand Down
54 changes: 21 additions & 33 deletions src/keri/app/clienting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,23 @@ export class SignifyClient {
/**
* Fetch a resource from from an external URL with headers signed by an AID
* @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} method HTTP method
* @param {any} data Data to be sent in the body of the resource
* @param {string} aidName Name or alias of the AID to be used for signing
* @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
*/
async signedFetch(
aidName: string,
url: string,
path: string,
method: string,
data: any,
aidName: string
req: RequestInit
): Promise<Response> {
const hab = await this.identifiers().get(aidName);
const keeper = this.manager!.get(hab);
Expand All @@ -254,42 +258,26 @@ export class SignifyClient {
keeper.signers[0].verfer
);

const headers = new Headers();
headers.set('Signify-Resource', hab.prefix);
let headers = req.headers;
if (headers == undefined) {
headers = new Headers();
} else {
headers = new Headers(headers);
}
headers.set('Signify-Resource', hab['prefix']);
headers.set(
'Signify-Timestamp',
new Date().toISOString().replace('Z', '000+00:00')
);

if (data !== null) {
headers.set('Content-Length', data.length);
} else {
headers.set('Content-Length', '0');
}
const signed_headers = authenticator.sign(
headers,
method,
new Headers(headers),
headers.get('method')!,
path.split('?')[0]
);
let _body = null;
if (method != 'GET') {
if (data instanceof FormData) {
_body = data;
// do not set the content type, let the browser do it
// headers.set('Content-Type', 'multipart/form-data')
} else {
_body = JSON.stringify(data);
headers.set('Content-Type', 'application/json');
}
} else {
headers.set('Content-Type', 'application/json');
}
req.headers = signed_headers;

return await fetch(url + path, {
method: method,
body: _body,
headers: signed_headers,
});
return await fetch(url + path, req);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/keri/app/credentialing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,13 @@ export class Credentials {
const [, acdc] = Saider.saidify({
v: versify(Ident.ACDC, undefined, Serials.JSON, 0),
d: '',
u: args.u,
i: args.i ?? hab.prefix,
...args,
ri: args.ri,
s: args.s,
a: subject,
e: args.e,
r: args.r,
});

const [, iss] = Saider.saidify({
Expand Down
12 changes: 9 additions & 3 deletions test/app/clienting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,18 @@ describe('SignifyClient', () => {
'EGFi9pCcRaLK8dPh5S7JP9Em62fBMiR1l4gW1ZazuuAO'
);

let heads = new Headers();
heads.set('Content-Type', 'application/json');
let reqInit = {
headers: heads,
method: 'POST',
body: JSON.stringify({ foo: true }),
};
resp = await client.signedFetch(
'aid1',
'http://example.com',
'/test',
'POST',
{ foo: true },
'aid1'
reqInit
);
lastCall = fetchMock.mock.calls[fetchMock.mock.calls.length - 1]!;
assert.equal(lastCall[0]!, 'http://example.com/test');
Expand Down
Loading