Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 8a2e171

Browse files
awjh-ibmnklincoln
authored andcommitted
Business network now can check against regitry for identityIssue (#3401)
Signed-off-by: awjh-ibm <andrew.hurt1@ibm.com>
1 parent 7c5b65e commit 8a2e171

8 files changed

Lines changed: 100 additions & 1 deletion

File tree

packages/composer-client/lib/businessnetworkconnection.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,28 @@ class BusinessNetworkConnection extends EventEmitter {
803803
})
804804
.then((exists) => {
805805
if (exists) {
806-
return this.connection.createIdentity(this.securityContext, identityName, options);
806+
if (this.connection.registryCheckRequired()) {
807+
return this.getIdentityRegistry()
808+
.then((registry) => {
809+
return registry.getAll();
810+
})
811+
.then((ids) => {
812+
ids = ids.map((el) => {
813+
return el.name;
814+
});
815+
816+
if (ids.includes(identityName)) {
817+
throw new Error(`Identity with name ${identityName} already exists in ${this.getBusinessNetwork().getName()}`);
818+
}
819+
});
820+
}
807821
} else {
808822
throw new Error(`Participant '${participant.getFullyQualifiedIdentifier()}' does not exist `);
809823
}
810824
})
825+
.then(() => {
826+
return this.connection.createIdentity(this.securityContext, identityName, options);
827+
})
811828
.then((identity) => {
812829
return this.submitTransaction(transaction)
813830
.then(() => {

packages/composer-client/test/businessnetworkconnection.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ describe('BusinessNetworkConnection', () => {
6868
mockConnection = sinon.createStubInstance(Connection);
6969
mockSecurityContext.getConnection.returns(mockConnection);
7070
mockBusinessNetworkDefinition = sinon.createStubInstance(BusinessNetworkDefinition);
71+
mockBusinessNetworkDefinition.getName.returns('super-doge-network');
7172
businessNetworkConnection = new BusinessNetworkConnection();
7273
businessNetworkConnection.businessNetwork = mockBusinessNetworkDefinition;
7374
modelManager = new ModelManager();
@@ -1146,6 +1147,45 @@ describe('BusinessNetworkConnection', () => {
11461147
});
11471148
});
11481149

1150+
it('should throw an error if the identity exists in the registry and connection is requires registry check', () => {
1151+
mockConnection.registryCheckRequired.returns(true);
1152+
1153+
let identityRegistry = sinon.createStubInstance(IdentityRegistry);
1154+
identityRegistry.getAll.resolves([{name: 'dogeid1'}]);
1155+
sandbox.stub(IdentityRegistry, 'getIdentityRegistry').resolves(identityRegistry);
1156+
1157+
return businessNetworkConnection.issueIdentity('org.acme.sample.SampleParticipant#dogeid1', 'dogeid1')
1158+
.catch((error) => {
1159+
error.should.match(/Identity with name dogeid1 already exists in super-doge-network/);
1160+
});
1161+
});
1162+
1163+
it('should submit a request to the chaincode if the identity does not exist in the registry and connection is WebConnection', () => {
1164+
mockConnection.registryCheckRequired.returns(true);
1165+
1166+
let identityRegistry = sinon.createStubInstance(IdentityRegistry);
1167+
identityRegistry.getAll.resolves([{name: 'dogeid0'}]);
1168+
sandbox.stub(IdentityRegistry, 'getIdentityRegistry').resolves(identityRegistry);
1169+
1170+
sandbox.stub(businessNetworkConnection, 'submitTransaction').resolves();
1171+
1172+
return businessNetworkConnection.issueIdentity('org.acme.sample.SampleParticipant#dogeid1', 'dogeid1')
1173+
.then((result) => {
1174+
sinon.assert.calledOnce(mockConnection.createIdentity);
1175+
sinon.assert.calledWith(mockConnection.createIdentity, mockSecurityContext, 'dogeid1');
1176+
sinon.assert.calledOnce(businessNetworkConnection.submitTransaction);
1177+
const tx = businessNetworkConnection.submitTransaction.args[0][0];
1178+
tx.instanceOf('org.hyperledger.composer.system.IssueIdentity').should.be.true;
1179+
tx.participant.isRelationship().should.be.true;
1180+
tx.participant.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#dogeid1');
1181+
tx.identityName.should.equal('dogeid1');
1182+
result.should.deep.equal({
1183+
userID : 'dogeid1',
1184+
userSecret : 'suchsecret'
1185+
});
1186+
});
1187+
});
1188+
11491189
it('should submit a request to the chaincode for a fully qualified identifier', () => {
11501190
sandbox.stub(businessNetworkConnection, 'submitTransaction').resolves();
11511191
return businessNetworkConnection.issueIdentity('org.acme.sample.SampleParticipant#dogeid1', 'dogeid1')

packages/composer-common/lib/connection.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,14 @@ class Connection extends EventEmitter {
588588
throw new Error('abstract function called');
589589
}
590590

591+
/**
592+
* Return whether a registry check is required before executing createIdentity to prevent duplicates.
593+
* @return {boolean} false.
594+
*/
595+
registryCheckRequired() {
596+
return false;
597+
}
598+
591599
/**
592600
* Create a new identity for the specified user ID.
593601
* @param {SecurityContext} securityContext The participant's security context.

packages/composer-common/test/connection.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,12 @@ describe('Connection', () => {
576576

577577
});
578578

579+
describe('#registryCheckRequired', () => {
580+
it('should return false', () => {
581+
connection.registryCheckRequired().should.deep.equal(false);
582+
});
583+
});
584+
579585
describe('#createIdentity', () => {
580586

581587
it('should call _createIdentity and handle no error', () => {

packages/composer-connector-embedded/lib/embeddedconnection.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ class EmbeddedConnection extends Connection {
373373
});
374374
}
375375

376+
/**
377+
* Return whether a registry check is required before executing createIdentity to prevent duplicates.
378+
* @return {boolean} true.
379+
*/
380+
registryCheckRequired() {
381+
return true;
382+
}
383+
376384
/**
377385
* Create a new identity for the specified name.
378386
* @param {SecurityContext} securityContext The participant's security context.

packages/composer-connector-embedded/test/embeddedconnection.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,12 @@ describe('EmbeddedConnection', () => {
440440

441441
});
442442

443+
describe('#registryCheckRequired', () => {
444+
it('should return true', () => {
445+
connection.registryCheckRequired().should.deep.equal(true);
446+
});
447+
});
448+
443449
describe('#createIdentity', () => {
444450

445451
let mockIdentitiesDataCollection;

packages/composer-connector-web/lib/webconnection.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,14 @@ class WebConnection extends Connection {
350350
});
351351
}
352352

353+
/**
354+
* Return whether a registry check is required before executing createIdentity to prevent duplicates.
355+
* @return {boolean} true.
356+
*/
357+
registryCheckRequired() {
358+
return true;
359+
}
360+
353361
/**
354362
* Create a new identity for the specified name.
355363
* @param {SecurityContext} securityContext The participant's security context.

packages/composer-connector-web/test/webconnection.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,12 @@ describe('WebConnection', () => {
464464

465465
});
466466

467+
describe('#registryCheckRequired', () => {
468+
it('should return true', () => {
469+
connection.registryCheckRequired().should.deep.equal(true);
470+
});
471+
});
472+
467473
describe('#createIdentity', () => {
468474

469475
let mockIdentitiesDataCollection;

0 commit comments

Comments
 (0)