Skip to content

Commit

Permalink
feat: HTTP API key generator
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Jan 16, 2022
1 parent b6428d0 commit d10a7fa
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ LEON_TTS=false
LEON_TTS_PROVIDER=flite

# Enable/disable packages to be available over HTTP
PACKAGES_OVER_HTTP=true
LEON_PACKAGES_OVER_HTTP=true
# HTTP API key (use "npm run generate:http-api-key" to regenerate one)
LEON_HTTP_API_KEY=

# Enable/disable collaborative logger
LEON_LOGGER=true
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"wake": "cross-env LEON_HOST=http://localhost LEON_PORT=1337 node hotword/index.js",
"delete-dist:server": "shx rm -rf ./server/dist",
"prepare": "husky install",
"generate:pkgs-endpoints": "babel-node scripts/run-generate-pkgs-endpoints.js",
"generate:pkgs-endpoints": "babel-node scripts/generate/run-generate-pkgs-endpoints.js",
"generate:http-api-key": "babel-node scripts/generate/run-generate-http-api-key.js",
"build": "npm run build:app && npm run build:server",
"build:app": "cross-env LEON_NODE_ENV=production babel-node scripts/app/run-build-app.js",
"build:server": "npm run delete-dist:server && npm run train && npm run generate:pkgs-endpoints && babel ./server/src -d ./server/dist --copy-files && shx mkdir -p server/dist/tmp",
Expand Down
71 changes: 71 additions & 0 deletions scripts/generate/generate-http-api-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import dotenv from 'dotenv'
import crypto from 'crypto'
import fs from 'fs'
import { prompt } from 'inquirer'
import path from 'path'

import log from '@/helpers/log'
import string from '@/helpers/string'

dotenv.config()

/**
* Generate HTTP API key script
* save it in the .env file
*/
const generateHttpApiKey = () => new Promise(async (resolve, reject) => {
log.info('Generating the HTTP API key...')

try {
const shasum = crypto.createHash('sha1')
const str = string.random(11)
const dotEnvPath = path.join(process.cwd(), '.env')
const envVarKey = 'LEON_HTTP_API_KEY'
let content = fs.readFileSync(dotEnvPath, 'utf8')

shasum.update(str)
const sha1 = shasum.digest('hex')

let lines = content.split('\n')
lines = lines.map((line) => {
if (line.indexOf(`${envVarKey}=`) !== -1) {
line = `${envVarKey}=${sha1}`
}

return line
})

content = lines.join('\n')

fs.writeFileSync(dotEnvPath, content)
log.success('HTTP API key generated')

resolve()
} catch (e) {
log.error(e.message)
reject(e)
}
})

export default () => new Promise(async (resolve, reject) => {
try {
if (process.env.LEON_HTTP_API_KEY === '') {
await generateHttpApiKey()
} else {
const answer = await prompt({
type: 'confirm',
name: 'generate.httpApiKey',
message: 'Do you want to regenerate the HTTP API key?',
default: false
})

if (answer.generate.httpApiKey === true) {
await generateHttpApiKey()
}
}

resolve()
} catch (e) {
reject(e)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default () => new Promise(async (resolve, reject) => {
const supportedMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']
const packagesDir = 'packages'
const outputFile = '/core/pkgs-endpoints.json'
const outputFilePath = path.join(__dirname, `..${outputFile}`)
const outputFilePath = path.join(__dirname, `../..${outputFile}`)
const lang = langs[process.env.LEON_LANG].short.toLowerCase().substr(0, 2)

try {
Expand Down
14 changes: 14 additions & 0 deletions scripts/generate/run-generate-http-api-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import log from '@/helpers/log'

import generateHttpApiKey from './generate-http-api-key'

/**
* Execute the generating HTTP API key script
*/
(async () => {
try {
await generateHttpApiKey()
} catch (e) {
log.error(`Failed to generate the HTTP API key: ${e}`)
}
})()
File renamed without changes.
4 changes: 4 additions & 0 deletions scripts/setup/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import log from '@/helpers/log'
import os from '@/helpers/os'

import train from '../train'
import generateHttpApiKey from '../generate/generate-http-api-key'
import setupDotenv from './setup-dotenv'
import setupCore from './setup-core'
import setupPackagesConfig from './setup-packages-config'
Expand All @@ -31,6 +32,9 @@ import setupPythonPackages from './setup-python-packages'
setupPackagesConfig()
])
await setupPythonPackages()
loader.stop()
await generateHttpApiKey()
loader.start()
await train()
if (info.type === 'windows') {
log.info('Windows detected, reinjecting DeepSpeech into package.json...')
Expand Down
2 changes: 1 addition & 1 deletion server/src/core/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ const bootstrap = async () => {
}
})

if (process.env.PACKAGES_OVER_HTTP === 'true') {
if (process.env.LEON_PACKAGES_OVER_HTTP === 'true') {
generatePackagesRoutes()
}

Expand Down

0 comments on commit d10a7fa

Please sign in to comment.