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

Improve LLO authentication service and testing. #2

Merged
merged 1 commit into from
Dec 6, 2022
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/rest-api-core",
"version": "0.2.1-2",
"version": "0.2.1-4",
"repository": {
"type": "git",
"url": "git+https://github.com/logion-network/logion-rest-api-core.git"
Expand Down
5 changes: 5 additions & 0 deletions src/AuthenticationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export class AuthenticationService {
return user;
}

async authenticatedUserIsLegalOfficerOnNode(request: Request): Promise<AuthenticatedUser> {
const user = await this.authenticatedUser(request);
return user.requireLegalOfficerOnNode();
}

async authenticatedUserIsOneOf(request: Request, ...addresses: (string | undefined | null)[]): Promise<AuthenticatedUser> {
const user = await this.authenticatedUser(request);
user.require(user => user.isOneOf(addresses), "User has not access to this resource");
Expand Down
35 changes: 34 additions & 1 deletion src/TestApp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import "./inversify.decorate";
import { DateTime } from "luxon";
import { AuthenticatedUser, AuthenticationSystem, Authenticator, SessionManager } from '@logion/authenticator';
import {
AuthenticatedUser,
AuthenticationSystem,
Authenticator,
SessionManager,
AuthorityService
} from '@logion/authenticator';
import express, { Express } from 'express';
import { Dino } from 'dinoloop';
import { Container } from 'inversify';
Expand Down Expand Up @@ -105,6 +111,13 @@ export function mockAuthenticationForUserOrLegalOfficer(isLegalOfficer: boolean,
});
authenticatedUser.setup(instance => instance.isNodeOwner()).returns(isLegalOfficer);
authenticatedUser.setup(instance => instance.isLegalOfficer()).returnsAsync(isLegalOfficer);
authenticatedUser.setup(instance => instance.requireLegalOfficerOnNode).returns(() => {
if (isLegalOfficer) {
return Promise.resolve(authenticatedUser.object())
} else {
throw new UnauthorizedException();
}
})
return mockAuthenticationWithAuthenticatedUser(authenticatedUser.object());
}

Expand All @@ -115,6 +128,7 @@ function mockAuthenticationService(mock: AuthenticationServiceMock): Authenticat
authenticationService.setup(instance => instance.authenticatedUserIs).returns(mock.authenticatedUserIs);
authenticationService.setup(instance => instance.authenticatedUserIsOneOf).returns(mock.authenticatedUserIsOneOf);
authenticationService.setup(instance => instance.authenticatedUser).returns(mock.authenticatedUser);
authenticationService.setup(instance => instance.authenticatedUserIsLegalOfficerOnNode).returns(mock.authenticatedUser);
authenticationService.setup(instance => instance.nodeOwner).returns(mock.nodeOwner);
authenticationService.setup(instance => instance.ensureAuthorizationBearer).returns(mock.ensureAuthorizationBearer);
return authenticationService.object();
Expand All @@ -134,6 +148,13 @@ export function mockAuthenticatedUser(conditionFulfilled: boolean, address?: str
});
authenticatedUser.setup(instance => instance.isNodeOwner).returns(() => conditionFulfilled);
authenticatedUser.setup(instance => instance.isLegalOfficer()).returnsAsync(conditionFulfilled);
authenticatedUser.setup(instance => instance.requireLegalOfficerOnNode).returns(() => {
if (address === ALICE) {
return Promise.resolve(authenticatedUser.object());
} else {
throw new UnauthorizedException();
}
});
return authenticatedUser.object();
}

Expand All @@ -148,9 +169,21 @@ function mockAuthenticationSystem(mock: AuthenticationServiceMock): Authenticati
const authenticator = new Mock<Authenticator>();
authenticator.setup(instance => instance.ensureAuthenticatedUserOrThrow).returns(() => mock.authenticatedUser());

const authorityService: AuthorityService = {
isLegalOfficer(): Promise<boolean> {
return Promise.resolve(true);
},
isLegalOfficerNode(): Promise<boolean> {
return Promise.resolve(true);
},
isLegalOfficerOnNode(): Promise<boolean> {
return Promise.resolve(false);
}
}
return {
sessionManager: sessionManager.object(),
authenticator: authenticator.object(),
authorityService,
};
}

23 changes: 22 additions & 1 deletion test/AuthenticationController.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Authenticator, Session, SessionManager, SessionSignature, SignedSession, Token } from "@logion/authenticator";
import {
Authenticator,
Session,
SessionManager,
SessionSignature,
SignedSession,
Token,
AuthorityService
} from "@logion/authenticator";
import { UnauthorizedException } from "dinoloop";
import { Container } from "inversify";
import { DateTime } from "luxon";
Expand Down Expand Up @@ -160,9 +168,22 @@ function mockDependenciesForAuth(container: Container, verifies: boolean, sessio
const sessionManager = new Mock<SessionManager>();
const authenticator = new Mock<Authenticator>();

const authorityService: AuthorityService = {
isLegalOfficer(): Promise<boolean> {
return Promise.resolve(true);
},
isLegalOfficerNode(): Promise<boolean> {
return Promise.resolve(true);
},
isLegalOfficerOnNode(): Promise<boolean> {
return Promise.resolve(false);
}
}

authenticationService.setup(instance => instance.authenticationSystem()).returnsAsync({
sessionManager: sessionManager.object(),
authenticator: authenticator.object(),
authorityService,
});

const session = new Mock<Session>();
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,8 @@ __metadata:
linkType: hard

"@logion/authenticator@npm:^0.3.3-1":
version: 0.3.3-2
resolution: "@logion/authenticator@npm:0.3.3-2"
version: 0.3.3-4
resolution: "@logion/authenticator@npm:0.3.3-4"
dependencies:
"@ethersproject/transactions": ^5.6.2
"@logion/node-api": ^0.8.0-1
Expand All @@ -855,7 +855,7 @@ __metadata:
luxon: ^3.0.1
peer-id: ^0.16.0
web3-utils: ^1.7.4
checksum: 146ae8160b912565ea3aa41c23c81fd315b346c667fd5d2f1e0993fdae86f8a9be93cd2a48354b413c2a23ede37e3880d703d7205fa049bbb6e8f53dc7f3ddc7
checksum: 1e1e8920cf357f661fa098c7d89bbd00e13a9e440b7f0921ce646d07e587f331d9c225a9d710e78a6e4f66e72a4c0a8f620147bc46bef09c71fbcdc87b28afc6
languageName: node
linkType: hard

Expand Down