Skip to content

Commit

Permalink
fix: integrate UserCookieStorage into UserStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfaron committed Feb 17, 2024
1 parent ff18fc7 commit 0133c3c
Show file tree
Hide file tree
Showing 28 changed files with 798 additions and 881 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"CONTENT_ROOT": "projects/aas-server/build",
"WEB_ROOT": "projects/aas-portal/dist",
"ASSETS": "projects/aas-server/src/assets",
// "USER_STORAGE": "mongodb://localhost:27017/aasportal-users",
// "TEMPLATE_STORAGE": "http://localhost:8080/templates",
"USER_STORAGE": "mongodb://localhost:27017/aasportal-users",
"TEMPLATE_STORAGE": "http://localhost:8080/templates",
// "AAS_INDEX": "mysql://localhost:3306",
"ENDPOINTS": "[\"file:///endpoints/samples?name=Samples\"]",
}
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cSpell.words": [
"aaslist",
"AASPORTAL",
"aasv",
"aasv3",
"angewandten",
Expand Down
4 changes: 2 additions & 2 deletions projects/aas-server/src/app/aas-index/mysql/mysql-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ export class MySqlIndex extends AASIndex {

private async initialize(): Promise<Connection> {
const url = new URL(this.variable.AAS_INDEX!);
const username = isEmpty(url.username) ? this.variable.USERNAME : url.username;
const password = isEmpty(url.password) ? this.variable.PASSWORD : url.password;
const username = isEmpty(url.username) ? this.variable.AAS_SERVER_USERNAME : url.username;
const password = isEmpty(url.password) ? this.variable.AAS_SERVER_PASSWORD : url.password;
const connection = await mysql.createConnection({
host: url.hostname,
port: Number(url.port),
Expand Down
2 changes: 0 additions & 2 deletions projects/aas-server/src/app/aas-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import 'reflect-metadata';
import { container } from 'tsyringe';
import { CookieStorageFactory } from './auth/cookie-storage-factory.js';
import { UserStorageFactory } from './auth/user-storage-factory.js';
import { LoggerFactory } from './logging/logger-factory.js';
import { FileLogger } from './logging/file-logger.js';
Expand All @@ -19,7 +18,6 @@ import { AASIndexFactory } from './aas-index/aas-index-factory.js';
container.registerInstance('USERS_DIR', './users');
container.registerSingleton('Logger', FileLogger);
container.register('AASIndex', { useFactory: c => new AASIndexFactory(c).create() });
container.register('CookieStorage', { useFactory: c => new CookieStorageFactory(c).create() });
container.register('UserStorage', { useFactory: c => new UserStorageFactory(c).create() });
container.register('winston.Logger', { useFactory: () => new LoggerFactory().create() });

Expand Down
10 changes: 4 additions & 6 deletions projects/aas-server/src/app/auth/auth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Mailer } from '../mailer.js';
import { ERRORS } from '../errors.js';
import { UserData } from './user-data.js';
import { UserStorage } from './user-storage.js';
import { CookieStorage } from './cookie-storage.js';
import { Variable } from '../variable.js';
import {
Credentials,
Expand All @@ -38,7 +37,6 @@ export class AuthService {
public constructor(
@inject(Mailer) private readonly mailer: Mailer,
@inject('UserStorage') private readonly userStorage: UserStorage,
@inject('CookieStorage') private readonly cookieStorage: CookieStorage,
@inject(Variable) private readonly variable: Variable,
) {
if (variable.JWT_PUBLIC_KEY) {
Expand Down Expand Up @@ -164,19 +162,19 @@ export class AuthService {
}

public getCookieAsync(id: string, name: string): Promise<Cookie | undefined> {
return this.cookieStorage.getAsync(id, name);
return this.userStorage.getCookieAsync(id, name);
}

public getCookiesAsync(id: string): Promise<Cookie[]> {
return this.cookieStorage.getAllAsync(id);
return this.userStorage.getCookiesAsync(id);
}

public setCookieAsync(id: string, name: string, data: string): Promise<void> {
return this.cookieStorage.setAsync(id, name, data);
return this.userStorage.setCookieAsync(id, name, data);
}

public deleteCookieAsync(id: string, name: string): Promise<void> {
return this.cookieStorage.deleteAsync(id, name);
return this.userStorage.deleteCookieAsync(id, name);
}

public hasUserAsync(id: string): Promise<boolean> {
Expand Down
41 changes: 0 additions & 41 deletions projects/aas-server/src/app/auth/cookie-storage-factory.ts

This file was deleted.

41 changes: 0 additions & 41 deletions projects/aas-server/src/app/auth/cookie-storage.ts

This file was deleted.

98 changes: 0 additions & 98 deletions projects/aas-server/src/app/auth/locale-cookie-storage.ts

This file was deleted.

77 changes: 76 additions & 1 deletion projects/aas-server/src/app/auth/locale-user-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
import { inject, injectable } from 'tsyringe';
import path from 'path';
import fs from 'fs';
import { Cookie } from 'common';
import { UserStorage } from './user-storage.js';
import { UserData } from './user-data.js';
import { Logger } from '../logging/logger.js';

@injectable()
export class LocaleUserStorage extends UserStorage {
private readonly usersDirectory: string;

public constructor(@inject('USERS_DIR') usersDirectory: string) {
public constructor(
@inject('Logger') private readonly logger: Logger,
@inject('USERS_DIR') usersDirectory: string,
) {
super();

this.usersDirectory = path.resolve(usersDirectory);
Expand Down Expand Up @@ -54,6 +59,76 @@ export class LocaleUserStorage extends UserStorage {
return false;
}

public async checkCookieAsync(userId: string, name: string): Promise<boolean> {
const file = this.getCookiesFile(userId);
if (fs.existsSync(file)) {
const cookies = await this.readCookies(file);
return cookies.some(cookie => cookie.name === name);
}

return false;
}

public async getCookieAsync(userId: string, name: string): Promise<Cookie | undefined> {
const file = this.getCookiesFile(userId);
if (fs.existsSync(file)) {
const cookies = await this.readCookies(file);
return cookies.find(cookie => cookie.name === name);
}

return undefined;
}

public async getCookiesAsync(userId: string): Promise<Cookie[]> {
const file = this.getCookiesFile(userId);
if (fs.existsSync(file)) {
const cookies = await this.readCookies(file);
return cookies;
}

return [];
}

public async setCookieAsync(userId: string, name: string, data: string): Promise<void> {
const file = this.getCookiesFile(userId);
const cookies = fs.existsSync(file) ? await this.readCookies(file) : [];
const index = cookies.findIndex(cookie => cookie.name === name);
if (index < 0) {
cookies.push({ name, data });
} else {
cookies[index].data = data;
}

await fs.promises.writeFile(file, JSON.stringify(cookies));
}

public async deleteCookieAsync(userId: string, name: string): Promise<void> {
const file = this.getCookiesFile(userId);
const cookies = fs.existsSync(file) ? await this.readCookies(file) : [];
const index = cookies.findIndex(cookie => cookie.name === name);
if (index >= 0) {
cookies.splice(index, 1);
if (cookies.length > 0) {
await fs.promises.writeFile(file, JSON.stringify(cookies));
} else {
await fs.promises.unlink(file);
}
}
}

private getCookiesFile(userId: string): string {
return path.join(this.usersDirectory, userId, 'cookies.json');
}

private async readCookies(path: string): Promise<Cookie[]> {
try {
return JSON.parse((await fs.promises.readFile(path)).toString()) as Cookie[];
} catch (error) {
this.logger.error(`Reading cookies failed: ${error?.message}`);
return [];
}
}

private async readUserData(path: string): Promise<UserData> {
const data = JSON.parse((await fs.promises.readFile(path)).toString()) as UserData;
data.created = new Date(data.created);
Expand Down
32 changes: 0 additions & 32 deletions projects/aas-server/src/app/auth/mongo-db-connection.ts

This file was deleted.

Loading

0 comments on commit 0133c3c

Please sign in to comment.