Skip to content

Commit

Permalink
fix(account): improve authorization checks when leaving identity
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed Aug 16, 2021
1 parent 95892e0 commit 306797d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 10 deletions.
16 changes: 15 additions & 1 deletion src/api/procedures/__tests__/leaveIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,21 @@ describe('modifyCaCheckpoint procedure', () => {
const proc = procedureMockUtils.getInstance<LeaveIdentityParams, void>(mockContext);
const boundFunc = getAuthorization.bind(proc);

expect(boundFunc()).toEqual({
let account = entityMockUtils.getAccountInstance({ isEqual: false });

expect(boundFunc({ account })).toEqual({
roles: false,
permissions: {
tokens: [],
transactions: [TxTags.identity.LeaveIdentityAsKey],
portfolios: [],
},
});

account = entityMockUtils.getAccountInstance({ isEqual: true });

expect(boundFunc({ account })).toEqual({
roles: true,
permissions: {
tokens: [],
transactions: [TxTags.identity.LeaveIdentityAsKey],
Expand Down
8 changes: 7 additions & 1 deletion src/api/procedures/__tests__/setCustodian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('setCustodian procedure', () => {
PortfolioCustody: dsMockUtils.createMockPortfolioId({ did: rawDid, kind: rawPortfolioKind }),
});
const rawExpiry = dsMockUtils.createMockMoment(expiry.getTime());
const fakePortfolio = entityMockUtils.getNumberedPortfolioInstance({ uuid: 'otherUuid' });
const fakePortfolio = entityMockUtils.getNumberedPortfolioInstance({ isEqual: false });
const receivedAuthorizations: AuthorizationRequest[] = [
entityMockUtils.getAuthorizationRequestInstance({
target,
Expand All @@ -147,6 +147,12 @@ describe('setCustodian procedure', () => {
getReceived: receivedAuthorizations,
},
},
defaultPortfolioOptions: {
isEqual: false,
},
numberedPortfolioOptions: {
isEqual: false,
},
});

signerToStringStub.withArgs(signer).returns(signer.address);
Expand Down
4 changes: 2 additions & 2 deletions src/api/procedures/setCustodian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export async function prepareSetCustodian(
context.getCurrentIdentity(),
]);

const hasPendingAuth = authorizationRequests.find(authorizationRequest => {
const hasPendingAuth = !!authorizationRequests.find(authorizationRequest => {
const { issuer, data } = authorizationRequest;
const authorizationData = data as { value: NumberedPortfolio | DefaultPortfolio };
return currentIdentity.did === issuer.did && authorizationData.value.uuid === portfolio.uuid;
return currentIdentity.isEqual(issuer) && authorizationData.value.isEqual(portfolio);
});

if (hasPendingAuth) {
Expand Down
3 changes: 3 additions & 0 deletions src/testUtils/mocks/dataSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ interface ContextOptions {
isFrozen?: boolean;
addPair?: Pair;
getAccounts?: Account[];
currentIdentityIsEqual?: boolean;
}

interface KeyringOptions {
Expand Down Expand Up @@ -559,6 +560,7 @@ const defaultContextOptions: ContextOptions = {
publicKey: 'someKey',
},
getAccounts: [],
currentIdentityIsEqual: true,
};
let contextOptions: ContextOptions = defaultContextOptions;
const defaultKeyringOptions: KeyringOptions = {
Expand Down Expand Up @@ -610,6 +612,7 @@ function configureContext(opts: ContextOptions): void {
getSent: sinon.stub().resolves(opts.sentAuthorizations),
},
areSecondaryKeysFrozen: sinon.stub().resolves(opts.areScondaryKeysFrozen),
isEqual: sinon.stub().returns(opts.currentIdentityIsEqual),
};
opts.withSeed
? getCurrentIdentity.resolves(identity)
Expand Down

0 comments on commit 306797d

Please sign in to comment.