Skip to content

Commit 50918e1

Browse files
authored
Merge pull request #194 from mbret/develop
feat: added cloudfare header
2 parents 7322819 + 79f6aa4 commit 50918e1

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

Diff for: packages/api/src/functions/signin/handler.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
88
import schema from "./schema"
99
import { getAuth } from "firebase-admin/auth"
10-
import { getAdminNano, getOrCreateUserFromEmail } from "@libs/couch/dbHelpers"
10+
import {
11+
getDangerousAdminNano,
12+
getOrCreateUserFromEmail
13+
} from "@libs/couch/dbHelpers"
1114
import { generateToken } from "@libs/auth"
1215
import { ObokuErrorCode } from "@oboku/shared"
1316
import { createHttpError } from "@libs/httpErrors"
@@ -18,8 +21,8 @@ import { getFirebaseApp } from "@libs/firebase/app"
1821
const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
1922
event
2023
) => {
21-
const [jwtPrivateKey = ``] = await getParametersValue({
22-
Names: ["jwt-private-key"],
24+
const [jwtPrivateKey = ``, xAccessSecret = ``] = await getParametersValue({
25+
Names: ["jwt-private-key", "x-access-secret"],
2326
WithDecryption: true
2427
})
2528

@@ -41,7 +44,10 @@ const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
4144
})
4245
}
4346

44-
const adminNano = await getAdminNano({ privateKey: jwtPrivateKey })
47+
const adminNano = await getDangerousAdminNano({
48+
privateKey: jwtPrivateKey,
49+
xAccessSecret
50+
})
4551

4652
const user = await getOrCreateUserFromEmail(adminNano, email)
4753

Diff for: packages/api/src/libs/couch/dbHelpers.ts

+11-20
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,16 @@ export const getNanoDbForUser = async (name: string, privateKey: string) => {
359359
return db.use(`userdb-${hexEncodedUserId}`)
360360
}
361361

362-
export const getNano = async ({ jwtToken }: { jwtToken?: string } = {}) => {
362+
export const getNano = async ({
363+
jwtToken,
364+
xAccessSecret
365+
}: { jwtToken?: string; xAccessSecret?: string } = {}) => {
363366
return createNano({
364367
url: COUCH_DB_URL,
365368
requestDefaults: {
366369
headers: {
367370
"content-type": "application/json",
371+
"x-access-secret": xAccessSecret,
368372
accept: "application/json",
369373
...(jwtToken && {
370374
Authorization: `Bearer ${jwtToken}`
@@ -378,24 +382,11 @@ export const getNano = async ({ jwtToken }: { jwtToken?: string } = {}) => {
378382
* WARNING: be very careful when using nano as admin since you will have full power.
379383
* As you know with great power comes great responsibilities
380384
*/
381-
export const getAdminNano = async (options: {
382-
sub?: string
383-
privateKey: string
384-
}) => {
385+
export const getDangerousAdminNano = async (
386+
options: {
387+
sub?: string
388+
privateKey: string
389+
} & Omit<NonNullable<Parameters<typeof getNano>[0]>, "jwtToken">
390+
) => {
385391
return getNano({ jwtToken: await generateAdminToken(options) })
386392
}
387-
388-
export const auth = async (username: string, userpass: string) => {
389-
const db = await getNano()
390-
391-
try {
392-
const response = await db.auth(username, userpass)
393-
if (!response.ok || !response.name) {
394-
return null
395-
}
396-
return response
397-
} catch (e) {
398-
if ((e as any)?.statusCode === 401) return null
399-
throw e
400-
}
401-
}

Diff for: packages/api/src/libs/ssm.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const ssm = new SSMClient({ region: "us-east-1" })
99

1010
type ParameterName =
1111
| `jwt-private-key`
12+
| `x-access-secret`
1213
| `GOOGLE_CLIENT_SECRET`
1314
| `GOOGLE_API_KEY`
1415
| `GOOGLE_CLIENT_ID`

0 commit comments

Comments
 (0)