Skip to content

Commit

Permalink
feat: accept no node owner (directory).
Browse files Browse the repository at this point in the history
  • Loading branch information
gdethier committed Apr 12, 2024
1 parent fcf65ac commit 844719a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/authenticator",
"version": "0.5.6-5",
"version": "0.5.6-6",
"repository": {
"type": "git",
"url": "git+https://github.com/logion-network/logion-authenticator.git"
Expand Down
6 changes: 3 additions & 3 deletions src/AuthenticatedUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class AuthenticatedUser implements AccountId {

constructor(
accountId: AccountId,
nodeOwner: ValidAccountId,
nodeOwner: ValidAccountId | undefined,
authorityService: AuthorityService,
errorFactory: ErrorFactory,
) {
Expand All @@ -21,7 +21,7 @@ export class AuthenticatedUser implements AccountId {
}

isNodeOwner(): boolean {
return this.isPolkadot() && this.nodeOwner.equals(this);
return this.isPolkadot() && this.nodeOwner?.equals(this) || false;
}

async isLegalOfficer(): Promise<boolean> {
Expand Down Expand Up @@ -59,7 +59,7 @@ export class AuthenticatedUser implements AccountId {
}

readonly validAccountId: ValidAccountId;
private readonly nodeOwner: ValidAccountId;
private readonly nodeOwner?: ValidAccountId;
private readonly authorityService: AuthorityService;
private readonly errorFactory: ErrorFactory;
}
2 changes: 1 addition & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "./Signature.js";

export interface TokenConfig {
readonly nodeOwner: ValidAccountId;
readonly nodeOwner?: ValidAccountId;
readonly nodePeerId: PeerId;
readonly nodeKey: Buffer;
readonly jwtTimeToLive: Duration;
Expand Down
26 changes: 22 additions & 4 deletions test/AuthenticatedUser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('AuthenticatedUser', () => {
const authenticatedUser = buildAuthenticatedUser({
address: USER_POLKADOT_ADDRESS,
isWellKnownNode: true,
nodeOwner: ALICE,
});
expect(authenticatedUser.is(newValidAccountId(SOME_OTHER_USER))).toBe(false);
})
Expand All @@ -21,6 +22,7 @@ describe('AuthenticatedUser', () => {
const authenticatedUser = buildAuthenticatedUser({
address: USER_POLKADOT_ADDRESS,
isWellKnownNode: true,
nodeOwner: ALICE,
});
expect(authenticatedUser.is(null)).toBe(false);
})
Expand All @@ -29,6 +31,7 @@ describe('AuthenticatedUser', () => {
const authenticatedUser = buildAuthenticatedUser({
address: USER_POLKADOT_ADDRESS,
isWellKnownNode: true,
nodeOwner: ALICE,
});
expect(authenticatedUser.is(undefined)).toBe(false);
})
Expand All @@ -37,6 +40,7 @@ describe('AuthenticatedUser', () => {
const authenticatedUser = buildAuthenticatedUser({
address: USER_POLKADOT_ADDRESS,
isWellKnownNode: true,
nodeOwner: ALICE,
});
expect(authenticatedUser.isOneOf([ newValidAccountId(SOME_OTHER_USER) ])).toBe(false);
})
Expand All @@ -45,6 +49,7 @@ describe('AuthenticatedUser', () => {
const authenticatedUser = buildAuthenticatedUser({
address: USER_POLKADOT_ADDRESS,
isWellKnownNode: true,
nodeOwner: ALICE,
});
expect(authenticatedUser.isOneOf([ newValidAccountId(USER_POLKADOT_ADDRESS) ])).toBe(true);
expect(authenticatedUser.isPolkadot()).toBe(true);
Expand All @@ -54,6 +59,7 @@ describe('AuthenticatedUser', () => {
const authenticatedUser = buildAuthenticatedUser({
address: ALICE.address,
isWellKnownNode: true,
nodeOwner: ALICE,
});
expect(authenticatedUser.isNodeOwner()).toBe(true);
})
Expand All @@ -62,7 +68,8 @@ describe('AuthenticatedUser', () => {
const authenticatedUser = buildAuthenticatedUser({
address: USER_ETHEREUM_ADDRESS,
isWellKnownNode: true,
addressType: "Ethereum"
addressType: "Ethereum",
nodeOwner: ALICE,
});
expect(authenticatedUser.isOneOf([ newValidAccountId(USER_ETHEREUM_ADDRESS, "Ethereum") ])).toBe(true);
expect(authenticatedUser.isPolkadot()).toBe(false);
Expand All @@ -74,7 +81,8 @@ describe('AuthenticatedUser', () => {
const authenticatedUser = buildAuthenticatedUser({
address: USER_ETHEREUM_ADDRESS,
isWellKnownNode: true,
addressType: "Ethereum"
addressType: "Ethereum",
nodeOwner: ALICE,
});
expect(authenticatedUser.validAccountId.type).toEqual("Ethereum");
expect(authenticatedUser.validAccountId.address).toEqual(USER_ETHEREUM_ADDRESS);
Expand All @@ -84,21 +92,31 @@ describe('AuthenticatedUser', () => {
const authenticatedUser = buildAuthenticatedUser({
address: USER_POLKADOT_ADDRESS,
isWellKnownNode: true,
nodeOwner: ALICE,
});
expect(authenticatedUser.validAccountId.type).toEqual("Polkadot");
expect(authenticatedUser.validAccountId.address).toEqual(USER_POLKADOT_ADDRESS);
})

it('handles no node owner', async () => {
const authenticatedUser = buildAuthenticatedUser({
address: ALICE.address,
isWellKnownNode: true,
});
expect(authenticatedUser.isNodeOwner()).toBe(false);
})
})

function buildAuthenticatedUser(args: {
address: string,
isWellKnownNode?: boolean,
addressType?: AccountType,
nodeOwner?: ValidAccountId,
}): AuthenticatedUser {
const { address, isWellKnownNode, addressType } = args;
const { address, isWellKnownNode, addressType, nodeOwner } = args;
return new AuthenticatedUser(
newValidAccountId(address, addressType),
ALICE,
nodeOwner,
mockAuthorityService(false, isWellKnownNode),
{
unauthorized: (message: string) => new Error(message),
Expand Down

0 comments on commit 844719a

Please sign in to comment.