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

Solve Eslint issues with circular definitions #709

Merged
merged 3 commits into from
Mar 18, 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: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = {
],
"workspaces/no-relative-imports": "error",
"@typescript-eslint/no-unused-vars": "off", // or "@typescript-eslint/no-unused-vars": "off",
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": ["error"],
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
"warn",
Expand Down
56 changes: 28 additions & 28 deletions packages/binding-http/src/oauth-token-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,30 @@ interface TokenInformation {
client_id?: string;
}

export default function (method?: Method): EndpointValidator {
if (!method || !method?.name) {
throw new Error("Undefined oauth token validation method");
export abstract class Validator {
abstract validate(tokenRequest: http.IncomingMessage, scopes: Array<string>, clients: RegExp): Promise<boolean>;
}

function extractTokenFromRequest(request: http.IncomingMessage) {
const headerToken = request.headers.authorization;
const url = new URL(request.url, `http://${request.headers.host}`);
const queryToken = url.searchParams.get("access_token");

if (!headerToken && !queryToken) {
throw new Error("Invalid request: only one authentication method is allowed");
}

switch (method.name) {
case "introspection_endpoint":
return new EndpointValidator(method as IntrospectionEndpoint);
default:
throw new Error("Unsupported oauth token validation method " + method.name);
if (queryToken) {
return queryToken;
}
}

export abstract class Validator {
abstract validate(tokenRequest: http.IncomingMessage, scopes: Array<string>, clients: RegExp): Promise<boolean>;
const matches = headerToken.match(/Bearer\s(\S+)/);

if (!matches) {
throw new Error("Invalid request: malformed authorization header");
}

return matches[1];
}

export class EndpointValidator extends Validator {
Expand Down Expand Up @@ -141,24 +150,15 @@ export class EndpointValidator extends Validator {
}
}

function extractTokenFromRequest(request: http.IncomingMessage) {
const headerToken = request.headers.authorization;
const url = new URL(request.url, `http://${request.headers.host}`);
const queryToken = url.searchParams.get("access_token");

if (!headerToken && !queryToken) {
throw new Error("Invalid request: only one authentication method is allowed");
}

if (queryToken) {
return queryToken;
export default function (method?: Method): EndpointValidator {
if (!method || !method?.name) {
throw new Error("Undefined oauth token validation method");
}

const matches = headerToken.match(/Bearer\s(\S+)/);

if (!matches) {
throw new Error("Invalid request: malformed authorization header");
switch (method.name) {
case "introspection_endpoint":
return new EndpointValidator(method as IntrospectionEndpoint);
default:
throw new Error("Unsupported oauth token validation method " + method.name);
}

return matches[1];
}
8 changes: 4 additions & 4 deletions packages/binding-http/test/http-client-oauth-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import { readFileSync } from "fs";
import OAuthServer from "express-oauth-server";
import bodyParser from "body-parser";

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

@suite("HTTP oauth client implementation")
class HttpClientOAuthTest {
private client: HttpClient;
Expand Down Expand Up @@ -153,7 +157,3 @@ class HttpClientOAuthTest {
body.toString("ascii").should.eql("Ok!");
}
}

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
4 changes: 2 additions & 2 deletions packages/binding-http/test/http-server-oauth-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { IntrospectionEndpoint, EndpointValidator } from "../src/oauth-token-val
import Servient, { ExposedThing } from "@node-wot/core";
import fetch from "node-fetch";

class MockServient extends Servient {}

should();
@suite("OAuth server token validation tests")
class OAuthServerTests {
Expand Down Expand Up @@ -128,5 +130,3 @@ class OAuthServerTests {
response.status.should.be.equal(401);
}
}

class MockServient extends Servient {}
51 changes: 24 additions & 27 deletions packages/binding-mbus/src/mbus-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ const configDefaults = {
maxRetries: 5,
};

/**
* MBusTransaction represents a raw M-Bus operation performed on a MBusConnection
*/
class MBusTransaction {
unitId: number;
base: number;
operations: Array<PropertyOperation>; // operations to be completed when this transaction completes
constructor(unitId: number, base: number) {
this.unitId = unitId;
this.base = base;
this.operations = new Array<PropertyOperation>();
}

/**
* Link PropertyOperation with this transaction, so that operations can be
* notified about the result of a transaction.
*
* @param op the PropertyOperation to link with this transaction
*/
inform(op: PropertyOperation) {
this.operations.push(op);
}
}

/**
* MBusConnection represents a client connected to a specific host and port
*/
Expand All @@ -36,9 +60,7 @@ export class MBusConnection {
connecting: boolean;
connected: boolean;
timer: NodeJS.Timer; // connection idle timer
// eslint-disable-next-line no-use-before-define
currentTransaction: MBusTransaction; // transaction currently in progress or null
// eslint-disable-next-line no-use-before-define
queue: Array<MBusTransaction>; // queue of further transactions
config: {
connectionTimeout?: number;
Expand Down Expand Up @@ -233,31 +255,6 @@ export class MBusConnection {
}
}

/**
* MBusTransaction represents a raw M-Bus operation performed on a MBusConnection
*/
class MBusTransaction {
unitId: number;
base: number;
// eslint-disable-next-line no-use-before-define
operations: Array<PropertyOperation>; // operations to be completed when this transaction completes
constructor(unitId: number, base: number) {
this.unitId = unitId;
this.base = base;
this.operations = new Array<PropertyOperation>();
}

/**
* Link PropertyOperation with this transaction, so that operations can be
* notified about the result of a transaction.
*
* @param op the PropertyOperation to link with this transaction
*/
inform(op: PropertyOperation) {
this.operations.push(op);
}
}

/**
* PropertyOperation represents a read or write operation on a property
*/
Expand Down
Loading