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

Refactor refresh tokens #305

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 18 additions & 16 deletions src/classes/PermanentFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type {
Attributes,
FileEntry,
} from 'ssh2';
import type { AuthTokenManager } from './AuthTokenManager';

const isRootPath = (requestedPath: string): boolean => (
requestedPath === '/'
Expand Down Expand Up @@ -79,10 +80,10 @@ export class PermanentFileSystem {

private archivesCache?: Archive[];

private readonly authToken;
private readonly authTokenManager;

public constructor(authToken: string) {
this.authToken = authToken;
public constructor(authTokenManager: AuthTokenManager) {
this.authTokenManager = authTokenManager;
}

private static loadRootFileEntries(): FileEntry[] {
Expand Down Expand Up @@ -191,7 +192,7 @@ export class PermanentFileSystem {
const childName = path.basename(requestedPath);
const parentFolder = await this.loadFolder(parentPath);
return createFolder(
this.getClientConfiguration(),
await this.getClientConfiguration(),
{
name: childName,
},
Expand All @@ -201,7 +202,7 @@ export class PermanentFileSystem {

public async deleteDirectory(requestedPath: string): Promise<void> {
const account = await getAuthenticatedAccount(
this.getClientConfiguration(),
await this.getClientConfiguration(),
);
if (!account.isSftpDeletionEnabled) {
throw new OperationNotAllowedError('You must enable SFTP deletion directly in your account settings.');
Expand All @@ -220,7 +221,7 @@ export class PermanentFileSystem {
const folder = await this.loadFolder(requestedPath);

await deleteFolder(
this.getClientConfiguration(),
await this.getClientConfiguration(),
folder.id,
);
}
Expand All @@ -242,14 +243,14 @@ export class PermanentFileSystem {
fileSystemCompatibleName: archiveRecordName,
};
const s3Url = await uploadFile(
this.getClientConfiguration(),
await this.getClientConfiguration(),
dataStream,
fileFragment,
archiveRecordfragment,
parentFolder,
);
await createArchiveRecord(
this.getClientConfiguration(),
await this.getClientConfiguration(),
s3Url,
fileFragment,
archiveRecordfragment,
Expand All @@ -259,7 +260,7 @@ export class PermanentFileSystem {

public async deleteFile(requestedPath: string): Promise<void> {
const account = await getAuthenticatedAccount(
this.getClientConfiguration(),
await this.getClientConfiguration(),
);
if (!account.isSftpDeletionEnabled) {
throw new OperationNotAllowedError('You must enable SFTP deletion directly in your account settings.');
Expand All @@ -274,7 +275,7 @@ export class PermanentFileSystem {
);

await deleteArchiveRecord(
this.getClientConfiguration(),
await this.getClientConfiguration(),
archiveRecord.id,
);
}
Expand Down Expand Up @@ -338,7 +339,7 @@ export class PermanentFileSystem {
childName,
);
const populatedArchiveRecord = await getArchiveRecord(
this.getClientConfiguration(),
await this.getClientConfiguration(),
archiveRecord.id,
archiveId,
);
Expand Down Expand Up @@ -380,17 +381,18 @@ export class PermanentFileSystem {
return this.loadArchiveRecords(archiveRecordPaths);
}

private getClientConfiguration(): ClientConfiguration {
private async getClientConfiguration(): Promise<ClientConfiguration> {
const authToken = await this.authTokenManager.getAuthToken();
return {
bearerToken: this.authToken,
bearerToken: authToken,
baseUrl: process.env.PERMANENT_API_BASE_PATH,
};
}

private async loadArchives(): Promise<Archive[]> {
if (!this.archivesCache) {
this.archivesCache = await getArchives(
this.getClientConfiguration(),
await this.getClientConfiguration(),
);
}
return this.archivesCache;
Expand All @@ -417,7 +419,7 @@ export class PermanentFileSystem {
return cachedArchiveFolders;
}
const archiveFolders = await getArchiveFolders(
this.getClientConfiguration(),
await this.getClientConfiguration(),
archiveId,
);
this.archiveFoldersCache.set(archiveId, archiveFolders);
Expand Down Expand Up @@ -506,7 +508,7 @@ export class PermanentFileSystem {
childName,
);
const populatedTargetFolder = await getFolder(
this.getClientConfiguration(),
await this.getClientConfiguration(),
targetFolder.id,
archiveId,
);
Expand Down
5 changes: 3 additions & 2 deletions src/classes/PermanentFileSystemManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger } from '../logger';
import { PermanentFileSystem } from './PermanentFileSystem';
import type { AuthTokenManager } from './AuthTokenManager';

export class PermanentFileSystemManager {
private readonly permanentFileSystems = new Map<string, PermanentFileSystem>();
Expand All @@ -8,15 +9,15 @@ export class PermanentFileSystemManager {

public getCurrentPermanentFileSystemForUser(
user: string,
authToken: string,
authTokenManager: AuthTokenManager,
): PermanentFileSystem {
logger.silly('Get permanent file system for user', { user });
this.resetDeletionTimeout(user);
const existingFileSystem = this.permanentFileSystems.get(user);
if (existingFileSystem !== undefined) {
return existingFileSystem;
}
const permanentFileSystem = new PermanentFileSystem(authToken);
const permanentFileSystem = new PermanentFileSystem(authTokenManager);
this.permanentFileSystems.set(
user,
permanentFileSystem,
Expand Down
Loading