Skip to content

Commit bde331e

Browse files
committed
Handle auth for remote specifications
1 parent 17b20b2 commit bde331e

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/app/api/remotes/[encryptedRemoteConfig]/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ const RemoteSpecAuthSchema = z.object({
1313
username: z.string(),
1414
password: z.string(),
1515
});
16-
type RemoteSpecAuth = z.infer<typeof RemoteSpecAuthSchema>;
16+
export type RemoteSpecAuth = z.infer<typeof RemoteSpecAuthSchema>;
1717

1818
const RemoteConfigSchema = z.object({
1919
url: z.string().url(),
2020
auth: RemoteSpecAuthSchema.optional(),
2121
});
22-
type RemoteConfig = z.infer<typeof RemoteConfigSchema>;
22+
export type RemoteConfig = z.infer<typeof RemoteConfigSchema>;
2323

2424
export async function GET(req: NextRequest, { params }: { params: RemoteSpecificationParams }) {
2525
const isAuthenticated = await session.getIsAuthenticated()

src/features/projects/data/GitHubProjectDataSource.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@ import {
99
GitHubRepository,
1010
GitHubRepositoryRef
1111
} from "../domain"
12+
import { RemoteConfig } from "@/app/api/remotes/[encryptedRemoteConfig]/route"
13+
import RsaEncryptionService from "@/common/encryption/EncryptionService"
14+
import { env } from "@/common"
1215

1316
export default class GitHubProjectDataSource implements IProjectDataSource {
1417
private readonly repositoryDataSource: IGitHubRepositoryDataSource
1518
private readonly repositoryNameSuffix: string
19+
private readonly encryptionService: RsaEncryptionService
1620

1721
constructor(config: {
1822
repositoryDataSource: IGitHubRepositoryDataSource
1923
repositoryNameSuffix: string
2024
}) {
2125
this.repositoryDataSource = config.repositoryDataSource
2226
this.repositoryNameSuffix = config.repositoryNameSuffix
27+
this.encryptionService = new RsaEncryptionService({
28+
publicKey: Buffer.from(env.getOrThrow("ENCRYPTION_PUBLIC_KEY_BASE_64"), "base64").toString("utf-8"),
29+
privateKey: Buffer.from(env.getOrThrow("ENCRYPTION_PRIVATE_KEY_BASE_64"), "base64").toString("utf-8")
30+
})
2331
}
2432

2533
async getProjects(): Promise<Project[]> {
@@ -167,11 +175,34 @@ export default class GitHubProjectDataSource implements IProjectDataSource {
167175
const existingVersionIdCount = versionIds.filter(e => e == baseVersionId).length
168176
const versionId = baseVersionId + (existingVersionIdCount > 0 ? existingVersionIdCount : "")
169177
const specifications = remoteVersion.specifications.map(e => {
170-
return {
171-
id: this.makeURLSafeID((e.id || e.name).toLowerCase()),
172-
name: e.name,
173-
url: "/api/remotes/EKNYViMOSUnJggD4c4UwEoOGkGKZIPjWtijfeoYqgrkRP%2FIwXa770oxwRsVTdVFzmbjWuartdrUhUjkq7EyT4m3NBQOph0UaRTQhFgxm4Q5v2KJ%2BkhJ6TTKwiEgEdS%2BdOvTzAzXtk80T4amaNdeET9JVGJo0y8G47qtUIZCWmyzxamTnOJYOhkj4NcH9XlyafghwUV%2FO%2FAShlzwscFPy%2BlDFuhl8jmYV4fvClI2%2F4iFyew%2Bg5LGvNXPTS8wEZcz9GBKrcgSE6ScK3oeAGesKPyYblkKiU7dAxi%2FlRs9jiKQId%2BEFLWFcDgNw8aLDyZjKMT2u4gF9dfLx4iRQhaJ7KQ%3D%3D"
174-
}
178+
if (e.auth) {
179+
// decrypt username and password
180+
const username = this.encryptionService.decrypt(e.auth.encryptedUsername)
181+
const password = this.encryptionService.decrypt(e.auth.encryptedPassword)
182+
// construct remote config
183+
const remoteConfig: RemoteConfig = {
184+
url: e.url,
185+
auth: {
186+
type: e.auth.type,
187+
username,
188+
password
189+
}
190+
}
191+
// encrypt and encode remote config
192+
const encryptedRemoteConfig = encodeURIComponent(this.encryptionService.encrypt(JSON.stringify(remoteConfig)))
193+
194+
return {
195+
id: this.makeURLSafeID((e.id || e.name).toLowerCase()),
196+
name: e.name,
197+
url: `/api/remotes/${encryptedRemoteConfig}`
198+
}
199+
} else {
200+
return {
201+
id: this.makeURLSafeID((e.id || e.name).toLowerCase()),
202+
name: e.name,
203+
url: `/api/proxy?url=${encodeURIComponent(e.url)}`
204+
}
205+
}
175206
})
176207
versions.push({
177208
id: versionId,

src/features/projects/domain/IProjectConfig.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { z } from "zod"
33
export const ProjectConfigRemoteSpecificationSchema = z.object({
44
id: z.coerce.string().optional(),
55
name: z.coerce.string(),
6-
url: z.string()
6+
url: z.string(),
7+
auth: z.object({
8+
type: z.string(),
9+
encryptedUsername: z.string(),
10+
encryptedPassword: z.string()
11+
}).optional(),
712
})
813

914
export const ProjectConfigRemoteVersionSchema = z.object({

0 commit comments

Comments
 (0)