Skip to content

Commit

Permalink
Refactor/create abstraction for authenticator (#38)
Browse files Browse the repository at this point in the history
* refactor: create abstraction for Authenticator

* chore: organize imports

* refactor: use subset of config for RESTClients

* chore: bump transport version

* chore: fix lint error
  • Loading branch information
tnotheis committed Feb 28, 2024
1 parent 13d2b9e commit 32efc4f
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/runtime/package.json
Expand Up @@ -68,7 +68,7 @@
"@nmshd/consumption": "3.9.5",
"@nmshd/content": "2.8.7",
"@nmshd/crypto": "2.0.6",
"@nmshd/transport": "2.3.1",
"@nmshd/transport": "2.3.2",
"ajv": "^8.12.0",
"ajv-errors": "^3.0.0",
"ajv-formats": "^2.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/transport/package.json
@@ -1,6 +1,6 @@
{
"name": "@nmshd/transport",
"version": "2.3.1",
"version": "2.3.2",
"description": "The transport library handles backbone communication and content encryption.",
"homepage": "https://enmeshed.eu",
"repository": {
Expand Down
30 changes: 24 additions & 6 deletions packages/transport/src/core/backbone/Authenticator.ts
Expand Up @@ -2,15 +2,17 @@ import { ILogger } from "@js-soft/logging-abstractions";
import { AccountController } from "../../modules";
import { CoreDate } from "../types/CoreDate";
import { AuthClient } from "./AuthClient";
import { IRESTClientConfig } from "./RESTClient";
import { CredentialsBasic } from "./RESTClientAuthenticate";

export class Authenticator {
export abstract class AbstractAuthenticator {
private request?: Promise<void>;
private expiry?: CoreDate;
private token?: string;

private readonly authClient: AuthClient;
public constructor(private readonly accountController: AccountController) {
this.authClient = new AuthClient(accountController.config);
public constructor(private readonly config: IRESTClientConfig) {
this.authClient = new AuthClient(config);
}

public async getToken(): Promise<string> {
Expand Down Expand Up @@ -58,11 +60,11 @@ export class Authenticator {
}

private async authenticateInternal() {
const deviceCredentials = await this.accountController.activeDevice.getCredentials();
const deviceCredentials = await this.getCredentials();
const params = {
grantType: "password",
clientId: this.accountController.config.platformClientId,
clientSecret: this.accountController.config.platformClientSecret,
clientId: this.config.platformClientId,
clientSecret: this.config.platformClientSecret,
username: deviceCredentials.username,
password: deviceCredentials.password
};
Expand All @@ -71,4 +73,20 @@ export class Authenticator {
this.token = result.value.token;
this.expiry = result.value.expiry;
}

protected abstract getCredentials(): Promise<CredentialsBasic>;
}

export class Authenticator extends AbstractAuthenticator {
public constructor(private readonly accountController: AccountController) {
super(accountController.config);
}

public async getCredentials(): Promise<CredentialsBasic> {
const activeDevice = await this.accountController.activeDevice.getCredentials();
return {
username: activeDevice.username,
password: activeDevice.password
};
}
}
17 changes: 15 additions & 2 deletions packages/transport/src/core/backbone/RESTClient.ts
Expand Up @@ -2,8 +2,9 @@ import { ILogger } from "@js-soft/logging-abstractions";
import { CoreBuffer } from "@nmshd/crypto";
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import formDataLib from "form-data";
import { AgentOptions } from "http";
import { AgentOptions as HTTPSAgentOptions } from "https";
import _ from "lodash";
import { IConfig } from "../../core";
import { TransportLoggerFactory } from "../TransportLoggerFactory";
import { CoreId } from "../types";
import { ClientResult } from "./ClientResult";
Expand Down Expand Up @@ -32,6 +33,18 @@ export enum RESTClientLogDirective {
LogAll
}

export interface IRESTClientConfig {
platformClientId: string;
platformClientSecret: string;
platformTimeout: number;
platformMaxRedirects: number;
platformAdditionalHeaders?: Record<string, string>;
httpAgent: AgentOptions;
httpsAgent: HTTPSAgentOptions;
debug: boolean;
baseUrl: string;
}

export class RESTClient {
protected _logger: ILogger;
protected _logDirective = RESTClientLogDirective.LogAll;
Expand All @@ -50,7 +63,7 @@ export class RESTClient {
}

public constructor(
protected readonly config: IConfig,
protected readonly config: IRESTClientConfig,
protected requestConfig: AxiosRequestConfig = {}
) {
const defaults: AxiosRequestConfig = {
Expand Down
@@ -1,11 +1,10 @@
import { AxiosRequestConfig } from "axios";
import _ from "lodash";
import { IConfig } from "../Transport";
import { Authenticator } from "./Authenticator";
import { ClientResult } from "./ClientResult";
import { Paginator, PaginatorPercentageCallback } from "./Paginator";
import { RESTClient } from "./RESTClient";
import { RequestError } from "./RequestError";
import { IRESTClientConfig, RESTClient } from "./RESTClient";

export interface CredentialsBasic {
username: string;
Expand All @@ -14,7 +13,7 @@ export interface CredentialsBasic {

export class RESTClientAuthenticate extends RESTClient {
public constructor(
config: IConfig,
config: IRESTClientConfig,
private readonly authenticator: Authenticator,
requestConfig: AxiosRequestConfig = {}
) {
Expand Down
16 changes: 8 additions & 8 deletions packages/transport/src/modules/accounts/AccountController.ts
Expand Up @@ -3,39 +3,39 @@ import { ILogger } from "@js-soft/logging-abstractions";
import { log } from "@js-soft/ts-utils";
import { CryptoSecretKey } from "@nmshd/crypto";
import { ControllerName, CoreAddress, CoreDate, CoreErrors, CoreId, IConfig, Transport, TransportError } from "../../core";
import { Authenticator } from "../../core/backbone/Authenticator";
import { CoreCrypto } from "../../core/CoreCrypto";
import { DbCollectionName } from "../../core/DbCollectionName";
import { DependencyOverrides } from "../../core/DependencyOverrides";
import { TransportLoggerFactory } from "../../core/TransportLoggerFactory";
import { Authenticator } from "../../core/backbone/Authenticator";
import { PasswordGenerator } from "../../util";
import { CertificateController } from "../certificates/CertificateController";
import { CertificateIssuer } from "../certificates/CertificateIssuer";
import { CertificateValidator } from "../certificates/CertificateValidator";
import { ChallengeController } from "../challenges/ChallengeController";
import { DeviceController } from "../devices/DeviceController";
import { DeviceSecretType } from "../devices/DeviceSecretController";
import { DevicesController } from "../devices/DevicesController";
import { BackbonePutDevicesPushNotificationRequest, DeviceAuthClient } from "../devices/backbone/DeviceAuthClient";
import { DeviceClient } from "../devices/backbone/DeviceClient";
import { DeviceController } from "../devices/DeviceController";
import { DevicesController } from "../devices/DevicesController";
import { DeviceSecretType } from "../devices/DeviceSecretController";
import { Device, DeviceInfo, DeviceType } from "../devices/local/Device";
import { DeviceSecretCredentials } from "../devices/local/DeviceSecretCredentials";
import { DeviceSharedSecret } from "../devices/transmission/DeviceSharedSecret";
import { FileController } from "../files/FileController";
import { MessageController } from "../messages/MessageController";
import { RelationshipTemplateController } from "../relationshipTemplates/RelationshipTemplateController";
import { RelationshipSecretController } from "../relationships/RelationshipSecretController";
import { RelationshipsController } from "../relationships/RelationshipsController";
import { RelationshipSecretController } from "../relationships/RelationshipSecretController";
import { RelationshipTemplateController } from "../relationshipTemplates/RelationshipTemplateController";
import { SecretController } from "../secrets/SecretController";
import { ChangedItems } from "../sync/ChangedItems";
import { SyncProgressCallback, SyncProgressReporter } from "../sync/SyncCallback";
import { SyncController } from "../sync/SyncController";
import { SynchronizedCollection } from "../sync/SynchronizedCollection";
import { TokenController } from "../tokens/TokenController";
import { IdentityController } from "./IdentityController";
import { IdentityUtil } from "./IdentityUtil";
import { IdentityClient } from "./backbone/IdentityClient";
import { Identity } from "./data/Identity";
import { IdentityController } from "./IdentityController";
import { IdentityUtil } from "./IdentityUtil";

export class AccountController {
private readonly _authenticator: Authenticator;
Expand Down
4 changes: 2 additions & 2 deletions packages/transport/src/modules/sync/backbone/SyncClient.ts
@@ -1,4 +1,4 @@
import { IConfig, Paginator, PaginatorPercentageCallback, RESTClientAuthenticate } from "../../../core";
import { IRESTClientConfig, Paginator, PaginatorPercentageCallback, RESTClientAuthenticate } from "../../../core";
import { Authenticator } from "../../../core/backbone/Authenticator";
import { ClientResult } from "../../../core/backbone/ClientResult";
import { BackboneDatawalletModification } from "./BackboneDatawalletModification";
Expand Down Expand Up @@ -36,7 +36,7 @@ export interface ISyncClient {
}

export class SyncClient extends RESTClientAuthenticate implements ISyncClient {
public constructor(config: IConfig, authenticator: Authenticator) {
public constructor(config: IRESTClientConfig & { supportedDatawalletVersion: number }, authenticator: Authenticator) {
super(config, authenticator, {
headers: {
"x-supported-datawallet-version": config.supportedDatawalletVersion.toString() // eslint-disable-line @typescript-eslint/naming-convention
Expand Down

0 comments on commit 32efc4f

Please sign in to comment.