Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): provision private key #638

Merged
merged 14 commits into from
Oct 26, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/backend/src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const Config = {
'AUTH_SERVER_SPEC',
'https://raw.githubusercontent.com/interledger/open-payments/77462cd0872be8d0fa487a4b233defe2897a7ee4/auth-server-open-api-spec.yaml'
),
privateKey: parseOrProvisionKey(envString('PRIVATE_KEY_PATH', '')),

/** Frontend **/
frontendUrl: envString('FRONTEND_URL', 'http://localhost:3000')
Expand Down Expand Up @@ -137,3 +138,21 @@ function parseRedisTlsConfig(

return Object.keys(options).length > 0 ? options : undefined
}

function parseOrProvisionKey(keyPath: string): crypto.KeyObject {
sabineschaller marked this conversation as resolved.
Show resolved Hide resolved
if (keyPath !== '') {
try {
const key = crypto.createPrivateKey(readFileSync(keyPath))
sabineschaller marked this conversation as resolved.
Show resolved Hide resolved
const jwk = key.export({ format: 'jwk' })
if (jwk.crv === 'Ed25519') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have 'Ed25519' coming from a type declaration, but it looks like we are limited to just a string here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return key
} else {
console.log('Private key is not EdDSA-Ed25519 key. Generating new key.')
}
} catch {
console.log('Private key could not be loaded. Generating new key.')
}
}
const keypair = crypto.generateKeyPairSync('ed25519')
sabineschaller marked this conversation as resolved.
Show resolved Hide resolved
return keypair.privateKey
}