Skip to content

Commit

Permalink
fix(epic): construct private key from escaped JS string
Browse files Browse the repository at this point in the history
  • Loading branch information
nckhell committed Jan 21, 2025
1 parent 4cf8185 commit 78ca220
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { constructPrivateKey } from './constructPrivateKey'

const ESCAPED_PRIVATE_KEY = `-----BEGIN PRIVATE KEY-----\naaaaaaa\nbbbbb\nT62++bneShPHC6MB4Juo8OzZyn3XbNmyXxlnYgfnuy3PxF+lDg74IhApeW54u29t\ncccc\no0sppFxaEI36IFnyOOvmrFfJAO13nrMRuDLZeN9bHyZ4I+qQ2YRJmdN9fltQFZMQ\na/2n1dv00nX1Pd+mayOAV0fF20obDoI4gqZRaikG/gnNn7+ufIvxNdClft6INLhb\nddddd\nPEpsOnUyvQKBgD0ao0KxPiNSoTlV0OT8+SW+Wr8UxqCg/3JdT1+CxpER489PR1my\ngggg\nE9kfri8cvi1B+xxJu8paMitvwTYF6HU72bR5N2Yk4LXcMiHVln4gUji9cKkbQBvo\n-----END PRIVATE KEY-----`

const EXPECTED_PRIVATE_KEY = `-----BEGIN PRIVATE KEY-----
aaaaaaa
bbbbb
T62++bneShPHC6MB4Juo8OzZyn3XbNmyXxlnYgfnuy3PxF+lDg74IhApeW54u29t
cccc
o0sppFxaEI36IFnyOOvmrFfJAO13nrMRuDLZeN9bHyZ4I+qQ2YRJmdN9fltQFZMQ
a/2n1dv00nX1Pd+mayOAV0fF20obDoI4gqZRaikG/gnNn7+ufIvxNdClft6INLhb
ddddd
PEpsOnUyvQKBgD0ao0KxPiNSoTlV0OT8+SW+Wr8UxqCg/3JdT1+CxpER489PR1my
gggg
E9kfri8cvi1B+xxJu8paMitvwTYF6HU72bR5N2Yk4LXcMiHVln4gUji9cKkbQBvo
-----END PRIVATE KEY-----`

describe('constructPrivateKey', () => {
it('should format a private key', () => {
const formattedKey = constructPrivateKey(ESCAPED_PRIVATE_KEY)
expect(formattedKey).toBe(EXPECTED_PRIVATE_KEY)
})

it('should leave the key as is if it is already formatted', () => {
const formattedKey = constructPrivateKey(EXPECTED_PRIVATE_KEY)
expect(formattedKey).toBe(EXPECTED_PRIVATE_KEY)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Transforms a single-line private key with explicit \n into a properly formatted private key.
* @param inputKey - The single-line private key string.
* @returns The correctly formatted private key string.
*/
export const constructPrivateKey = (privateKey: string): string => {
// Replace explicit \n with actual newlines
const formattedKey = privateKey.replace(/\\n/g, '\n')

return formattedKey
}
1 change: 1 addition & 0 deletions extensions/epic/lib/api/auth/constructPrivateKey/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { constructPrivateKey } from './constructPrivateKey'
8 changes: 7 additions & 1 deletion extensions/epic/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Setting } from '@awell-health/extensions-core'
import { z, type ZodTypeAny } from 'zod'
import { constructPrivateKey } from './lib/api/auth/constructPrivateKey'

export const settings = {
baseUrl: {
Expand Down Expand Up @@ -38,5 +39,10 @@ export const SettingsValidationSchema = z.object({
baseUrl: z.string().min(1),
authUrl: z.string().min(1),
clientId: z.string().min(1),
privateKey: z.string().min(1),
/**
* Private keys turn out to be sensitive to formatting (newlines, etc.)
* But in Studio, a user can only enter the value of a setting in a single line.
* So we need to transform the value to the correct format here.
*/
privateKey: z.string().min(1).transform(constructPrivateKey),
} satisfies Record<keyof typeof settings, ZodTypeAny>)

0 comments on commit 78ca220

Please sign in to comment.