Skip to content

Commit

Permalink
Merge pull request #194 from mbret/develop
Browse files Browse the repository at this point in the history
feat: added cloudfare header
  • Loading branch information
mbret authored Dec 3, 2024
2 parents 7322819 + 79f6aa4 commit 50918e1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
14 changes: 10 additions & 4 deletions packages/api/src/functions/signin/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import { ValidatedEventAPIGatewayProxyEvent } from "@libs/api-gateway"
import schema from "./schema"
import { getAuth } from "firebase-admin/auth"
import { getAdminNano, getOrCreateUserFromEmail } from "@libs/couch/dbHelpers"
import {
getDangerousAdminNano,
getOrCreateUserFromEmail
} from "@libs/couch/dbHelpers"
import { generateToken } from "@libs/auth"
import { ObokuErrorCode } from "@oboku/shared"
import { createHttpError } from "@libs/httpErrors"
Expand All @@ -18,8 +21,8 @@ import { getFirebaseApp } from "@libs/firebase/app"
const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
event
) => {
const [jwtPrivateKey = ``] = await getParametersValue({
Names: ["jwt-private-key"],
const [jwtPrivateKey = ``, xAccessSecret = ``] = await getParametersValue({
Names: ["jwt-private-key", "x-access-secret"],
WithDecryption: true
})

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

const adminNano = await getAdminNano({ privateKey: jwtPrivateKey })
const adminNano = await getDangerousAdminNano({
privateKey: jwtPrivateKey,
xAccessSecret
})

const user = await getOrCreateUserFromEmail(adminNano, email)

Expand Down
31 changes: 11 additions & 20 deletions packages/api/src/libs/couch/dbHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,16 @@ export const getNanoDbForUser = async (name: string, privateKey: string) => {
return db.use(`userdb-${hexEncodedUserId}`)
}

export const getNano = async ({ jwtToken }: { jwtToken?: string } = {}) => {
export const getNano = async ({
jwtToken,
xAccessSecret
}: { jwtToken?: string; xAccessSecret?: string } = {}) => {
return createNano({
url: COUCH_DB_URL,
requestDefaults: {
headers: {
"content-type": "application/json",
"x-access-secret": xAccessSecret,
accept: "application/json",
...(jwtToken && {
Authorization: `Bearer ${jwtToken}`
Expand All @@ -378,24 +382,11 @@ export const getNano = async ({ jwtToken }: { jwtToken?: string } = {}) => {
* WARNING: be very careful when using nano as admin since you will have full power.
* As you know with great power comes great responsibilities
*/
export const getAdminNano = async (options: {
sub?: string
privateKey: string
}) => {
export const getDangerousAdminNano = async (
options: {
sub?: string
privateKey: string
} & Omit<NonNullable<Parameters<typeof getNano>[0]>, "jwtToken">
) => {
return getNano({ jwtToken: await generateAdminToken(options) })
}

export const auth = async (username: string, userpass: string) => {
const db = await getNano()

try {
const response = await db.auth(username, userpass)
if (!response.ok || !response.name) {
return null
}
return response
} catch (e) {
if ((e as any)?.statusCode === 401) return null
throw e
}
}
1 change: 1 addition & 0 deletions packages/api/src/libs/ssm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ssm = new SSMClient({ region: "us-east-1" })

type ParameterName =
| `jwt-private-key`
| `x-access-secret`
| `GOOGLE_CLIENT_SECRET`
| `GOOGLE_API_KEY`
| `GOOGLE_CLIENT_ID`
Expand Down

0 comments on commit 50918e1

Please sign in to comment.