Skip to content

Commit

Permalink
[@dhealthdapps/backend] fix(schedulers): validate challenge fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kravchenkodhealth authored and evias committed Jan 4, 2023
1 parent 91ccc5a commit 5fe37d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Injectable } from "@nestjs/common";
import { CronJob } from "cron";
import { SchedulerRegistry } from "@nestjs/schedule";
import { EventEmitter2 } from "@nestjs/event-emitter";
import { ConfigService } from "@nestjs/config";

// internal dependencies
import { AccessTokenRequest } from "../requests/AccessTokenRequest";
Expand Down Expand Up @@ -91,6 +92,8 @@ export class ValidateChallengeScheduler {
*/
protected stopTimeoutAmount = 1800000;

public authRegistries: string[];

/**
* Construct an instance of the scheduler.
*
Expand All @@ -103,6 +106,7 @@ export class ValidateChallengeScheduler {
private readonly schedulerRegistry: SchedulerRegistry,
protected readonly authService: AuthService,
protected readonly emitter: EventEmitter2,
protected readonly configService: ConfigService,
) {
// initialize cronJob with provided params
this.job = new CronJob(
Expand All @@ -125,6 +129,8 @@ export class ValidateChallengeScheduler {
this.logger = new LogService(
`${dappConfig.dappName}/ValidateChallengeScheduler`,
);

this.authRegistries = this.configService.get<string[]>("auth.registries");
}

/**
Expand All @@ -137,7 +143,10 @@ export class ValidateChallengeScheduler {
protected async validate() {
try {
const payload = await this.authService.validateChallenge(
{ challenge: this.challenge } as AccessTokenRequest,
{
challenge: this.challenge,
registry: this.authRegistries[0],
} as AccessTokenRequest,
false,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { ValidateChallengeScheduler } from "../../../../src/common/schedulers/Va
describe("common/ValidateChallengeScheduler", () => {
let validateChallengeScheduler: ValidateChallengeScheduler;
let authService: AuthService;
let configService: ConfigService;
let logger = {
setContext: jest.fn(),
log: jest.fn(),
Expand Down Expand Up @@ -82,6 +83,7 @@ describe("common/ValidateChallengeScheduler", () => {
ValidateChallengeScheduler,
);
authService = module.get<AuthService>(AuthService);
configService = module.get<ConfigService>(ConfigService);

(validateChallengeScheduler as any).logger = logger;
});
Expand All @@ -107,6 +109,7 @@ describe("common/ValidateChallengeScheduler", () => {
(validateChallengeScheduler as any).schedulerRegistry,
{} as AuthService,
{} as EventEmitter2,
configService,
);

expect(mockedFn).toBeCalledTimes(1);
Expand Down Expand Up @@ -185,6 +188,8 @@ describe("common/ValidateChallengeScheduler", () => {
.spyOn(authService, "validateChallenge")
.mockResolvedValue({} as AuthenticationPayload);

(validateChallengeScheduler as any).authRegistries = ["fakeRegistry"];

await (validateChallengeScheduler as any).validate();

expect(mockedValidate).toBeCalled();
Expand All @@ -194,7 +199,9 @@ describe("common/ValidateChallengeScheduler", () => {
(validateChallengeScheduler as any).challenge = "invalidChallenge";
(validateChallengeScheduler as any).authService = {
validateChallenge: jest.fn().mockImplementation(
() => { throw new Error("An error occured") }, // <-- force-throw
() => {
throw new Error("An error occured");
}, // <-- force-throw
),
};

Expand Down Expand Up @@ -225,6 +232,8 @@ describe("common/ValidateChallengeScheduler", () => {
emit: mockedFn,
};

(validateChallengeScheduler as any).authRegistries = ["fakeRegistry"];

await (validateChallengeScheduler as any).validate();

expect(mockedFn).toBeCalled();
Expand Down
2 changes: 2 additions & 0 deletions runtime/dapp-frontend-vue/src/state/store/AuthModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ export const AuthModule = {
const handler = new AuthService();
const authChallenge: string = context.getters["getChallenge"];
const refCode: string = context.getters["getRefCode"];
const registry: string = context.getters["getAuthRegistry"];

// try authenticating the user and requesting an access token
// this will only succeed provided that the end-user attached
// the authentication challenge in a transfer transaction
const response: AccessTokenDTO = await handler.login(
authChallenge,
registry,
refCode ? refCode : undefined
);

Expand Down

0 comments on commit 5fe37d7

Please sign in to comment.