Skip to content

Commit 5fcecee

Browse files
ENGG-3146: Authorization support in local sync (#137)
1 parent a7b24df commit 5fcecee

File tree

6 files changed

+132
-3
lines changed

6 files changed

+132
-3
lines changed

src/renderer/actions/local-sync/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { homedir } from "os";
22

33
export const CONFIG_FILE = "requestly.json";
4+
export const COLLECTION_AUTH_FILE = "auth.json";
45
export const DESCRIPTION_FILE = "README.md";
56
export const COLLECTION_VARIABLES_FILE = "vars.json";
67
export const ENVIRONMENT_VARIABLES_FILE = "env.json";

src/renderer/actions/local-sync/fs-manager.rpc-service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,10 @@ export class FsManagerRPCService extends RPCServiceOverIPC {
112112
"updateCollectionDescription",
113113
this.fsManager.updateCollectionDescription.bind(this.fsManager)
114114
);
115+
116+
this.exposeMethodOverIPC(
117+
"updateCollectionAuthData",
118+
this.fsManager.updateCollectionAuthData.bind(this.fsManager)
119+
);
115120
}
116121
}

src/renderer/actions/local-sync/fs-manager.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
removeUndefinedFromRoot,
1515
} from "./common-utils";
1616
import {
17+
COLLECTION_AUTH_FILE,
1718
COLLECTION_VARIABLES_FILE,
1819
CONFIG_FILE,
1920
DESCRIPTION_FILE,
@@ -37,10 +38,12 @@ import {
3738
} from "./fs-utils";
3839
import {
3940
ApiRecord,
41+
Auth,
4042
Config,
4143
Description,
4244
EnvironmentRecord,
4345
Variables,
46+
AuthType,
4447
} from "./schemas";
4548
import {
4649
API,
@@ -530,6 +533,48 @@ export class FsManager {
530533
}
531534
}
532535

536+
async updateCollectionAuthData(
537+
id: string,
538+
authData: Static<typeof Auth>
539+
): Promise<FileSystemResult<Static<typeof Auth>>> {
540+
try {
541+
const authFileResource = this.createResource({
542+
id: getIdFromPath(appendPath(id, COLLECTION_AUTH_FILE)),
543+
type: "file",
544+
});
545+
546+
if (authData.currentAuthType === AuthType.NO_AUTH) {
547+
const deleteResult = await deleteFsResource(authFileResource);
548+
if (deleteResult.type === "error") {
549+
return deleteResult;
550+
}
551+
return {
552+
type: "success",
553+
content: {
554+
authConfigStore: {},
555+
currentAuthType: AuthType.NO_AUTH,
556+
},
557+
};
558+
}
559+
const writeResult = await writeContent(authFileResource, authData, Auth);
560+
if (writeResult.type === "error") {
561+
return writeResult;
562+
}
563+
return {
564+
type: "success",
565+
content: authData,
566+
};
567+
} catch (e: any) {
568+
return {
569+
type: "error",
570+
error: {
571+
message: e.message || "An unexpected error has occured!",
572+
path: e.path || "Unknown path",
573+
},
574+
};
575+
}
576+
}
577+
533578
async moveCollection(
534579
id: string,
535580
newParentId: string

src/renderer/actions/local-sync/fs-utils.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
parseContent,
2121
} from "./common-utils";
2222
import {
23+
COLLECTION_AUTH_FILE,
2324
COLLECTION_VARIABLES_FILE,
2425
CONFIG_FILE,
2526
DESCRIPTION_FILE,
@@ -38,6 +39,7 @@ import {
3839
EnvironmentVariableType,
3940
GlobalConfig,
4041
Description,
42+
Auth,
4143
} from "./schemas";
4244
import { Stats } from "node:fs";
4345

@@ -483,7 +485,7 @@ async function getCollectionVariables(
483485
} as FileSystemResult<Static<typeof Variables>>;
484486
}
485487

486-
async function getDescription(
488+
async function getCollectionDescription(
487489
rootPath: string,
488490
folder: FolderResource
489491
): Promise<FileSystemResult<string>> {
@@ -511,6 +513,33 @@ async function getDescription(
511513
};
512514
}
513515

516+
async function getCollectionAuthData(
517+
rootPath: string,
518+
folder: FolderResource
519+
): Promise<FileSystemResult<Static<typeof Auth>>> {
520+
const authPath = appendPath(folder.path, COLLECTION_AUTH_FILE);
521+
const authFileExists = await fsp
522+
.lstat(authPath)
523+
.then((stats) => stats.isFile())
524+
.catch(() => false);
525+
526+
if (authFileExists) {
527+
return parseFile({
528+
resource: createFsResource({
529+
rootPath,
530+
path: authPath,
531+
type: "file",
532+
}),
533+
validator: Auth,
534+
});
535+
}
536+
537+
return {
538+
type: "success",
539+
content: {},
540+
} as FileSystemResult<Static<typeof Auth>>;
541+
}
542+
514543
export async function parseFolderToCollection(
515544
rootPath: string,
516545
folder: FolderResource
@@ -523,12 +552,24 @@ export async function parseFolderToCollection(
523552
return collectionVariablesResult;
524553
}
525554

526-
const descriptionFileResult = await getDescription(rootPath, folder);
555+
const descriptionFileResult = await getCollectionDescription(
556+
rootPath,
557+
folder
558+
);
527559
if (descriptionFileResult.type === "error") {
528560
return descriptionFileResult;
529561
}
530562

563+
const collectionAuthDataResult = await getCollectionAuthData(
564+
rootPath,
565+
folder
566+
);
567+
if (collectionAuthDataResult.type === "error") {
568+
return collectionAuthDataResult;
569+
}
570+
531571
const collectionVariables = collectionVariablesResult.content;
572+
const collectionAuthData = collectionAuthDataResult.content;
532573
const collectionDescription = descriptionFileResult.content;
533574

534575
const collection: Collection = {
@@ -538,6 +579,7 @@ export async function parseFolderToCollection(
538579
collectionId: getCollectionId(rootPath, folder),
539580
variables: collectionVariables,
540581
description: collectionDescription,
582+
auth: collectionAuthData,
541583
};
542584

543585
const result: FileSystemResult<Collection> = {
@@ -599,6 +641,7 @@ export function sanitizeFsResourceList(
599641
(resource) => resource.path !== appendPath(rootPath, CONFIG_FILE),
600642
(resource) => !resource.path.endsWith(COLLECTION_VARIABLES_FILE),
601643
(resource) => !resource.path.endsWith(DESCRIPTION_FILE),
644+
(resource) => !resource.path.endsWith(COLLECTION_AUTH_FILE),
602645
(resource) => !resource.path.includes(DS_STORE_FILE),
603646
];
604647
if (type === "api") {

src/renderer/actions/local-sync/schemas.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ export enum ApiMethods {
2121
OPTIONS = "OPTIONS",
2222
}
2323

24+
export enum AuthType {
25+
INHERIT = "INHERIT",
26+
NO_AUTH = "NO_AUTH",
27+
API_KEY = "API_KEY",
28+
BEARER_TOKEN = "BEARER_TOKEN",
29+
BASIC_AUTH = "BASIC_AUTH",
30+
}
31+
2432
type RequestBodyContainer = {
2533
text?: string;
2634
form?: KeyValuePair[];
@@ -40,6 +48,30 @@ interface KeyValuePair {
4048
type?: string;
4149
}
4250

51+
export const Auth = Type.Object({
52+
authConfigStore: Type.Object({
53+
API_KEY: Type.Optional(
54+
Type.Object({
55+
addTo: Type.String(),
56+
key: Type.String(),
57+
value: Type.String(),
58+
})
59+
),
60+
BASIC_AUTH: Type.Optional(
61+
Type.Object({
62+
username: Type.String(),
63+
password: Type.String(),
64+
})
65+
),
66+
BEARER_TOKEN: Type.Optional(
67+
Type.Object({
68+
bearer: Type.String(),
69+
})
70+
),
71+
}),
72+
currentAuthType: Type.Enum(AuthType),
73+
});
74+
4375
export const Description = Type.String();
4476

4577
export const ApiRecord = Type.Object({
@@ -71,6 +103,7 @@ export const ApiRecord = Type.Object({
71103
body: Type.Optional(Type.Any()),
72104
bodyContainer: Type.Optional(Type.Any()),
73105
contentType: Type.Optional(Type.Enum(RequestContentType)),
106+
auth: Type.Optional(Auth),
74107
scripts: Type.Optional(
75108
Type.Object({
76109
preRequest: Type.String(),

src/renderer/actions/local-sync/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { EnvironmentVariableType } from "./schemas";
1+
import { Static } from "@sinclair/typebox";
2+
import { Auth, EnvironmentVariableType } from "./schemas";
23

34
export type FileSystemError = { message: string; path: string };
45
export type ContentfulSuccess<T> = T extends void
@@ -36,6 +37,7 @@ export type Collection = {
3637
name: string;
3738
variables?: Record<string, any>;
3839
description?: string;
40+
auth?: Static<typeof Auth>;
3941
};
4042

4143
export type API = {

0 commit comments

Comments
 (0)