Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,15 @@ send-api-req-prod: ## Send request to production
.PHONY: db-shell
db-shell: ## Opens the mysql shell inside the db container
docker compose exec db bash -c 'mysql -uopenpodcast -popenpodcast openpodcast'

.PHONY: insert-api-key
insert-api-key: ## Insert an API key into the dev database (usage: make insert-api-key API_KEY=dummy-cn389ncoiwuencr ACCOUNT_ID=3)
ifndef API_KEY
@echo "Error: API_KEY is missing. Please specify the API_KEY variable. Example: make insert-api-key API_KEY=dummy-cn389ncoiwuencr ACCOUNT_ID=3"
@exit 1
endif
ifndef ACCOUNT_ID
@echo "Error: ACCOUNT_ID is missing. Please specify the ACCOUNT_ID variable. Example: make insert-api-key API_KEY=dummy-cn389ncoiwuencr ACCOUNT_ID=3"
@exit 1
endif
@set -a && source env.local.test && set +a && npm run insert-api-key $(API_KEY) $(ACCOUNT_ID)
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,29 @@ make test-one-e2e-"should return not be authenticated with random token"

- `make up-db` starts the db and inits basic auth related tables
- `make dev` starts the dev server which also creates the tables using migrations
- to finalize the auth data run `make init-auth-db` which creates the auth structures
- to finalize the auth data run `make init-auth-db` which creates the auth structures

#### Adding API Keys

To add new API keys to the development database:

**Using npm:**
```bash
npm run insert-api-key <api-key> <account-id>
```

**Using make:**
```bash
make insert-api-key API_KEY=<api-key> ACCOUNT_ID=<account-id>
```

**Examples:**
```bash
npm run insert-api-key dummy-cn389ncoiwuencr 3
make insert-api-key API_KEY=dummy-cn389ncoiwuencr ACCOUNT_ID=3
```

The script will:
- Hash the API key using SHA256 (matching the existing authentication system)
- Insert it into the `openpodcast_auth.apiKeys` table
- Associate it with the specified account ID
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"build": "tsc",
"dev": "nodemon --exec ts-node src/index.ts --watch src --ext ts",
"lint": "npx eslint --config .eslintrc.js ./src",
"test": "jest"
"test": "jest",
"insert-api-key": "ts-node scripts/insert-api-key.ts"
},
"license": "MIT",
"private": true,
Expand Down
49 changes: 49 additions & 0 deletions scripts/insert-api-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import mysql from 'mysql2/promise'
import crypto from 'crypto'

function hashKey(apiKey: string): string {
return crypto.createHash('sha256').update(apiKey).digest('hex')
}

async function insertApiKey(apiKey: string, accountId: number): Promise<void> {
const connection = await mysql.createConnection({
host: process.env.MYSQL_HOST || 'localhost',
port: parseInt(process.env.MYSQL_PORT || '3306'),
user: process.env.MYSQL_USER || 'openpodcast',
password: process.env.MYSQL_PASSWORD || 'openpodcast',
database: process.env.MYSQL_DATABASE || 'openpodcast'
})

try {
const hashedKey = hashKey(apiKey)
const query = 'REPLACE INTO openpodcast_auth.apiKeys (key_hash, account_id) VALUES (?, ?)'

console.log(`Inserting API key for account ${accountId}`)
console.log(`Hashed key: ${hashedKey}`)

await connection.execute(query, [hashedKey, accountId])
console.log('API key inserted successfully')
} catch (error) {
console.error('Error inserting API key:', error)
process.exit(1)
} finally {
await connection.end()
}
}

// Command line argument parsing
if (process.argv.length !== 4) {
console.error('Usage: ts-node scripts/insert-api-key.ts <api-key> <account-id>')
console.error('Example: ts-node scripts/insert-api-key.ts dummy-cn389ncoiwuencr 3')
process.exit(1)
}

const apiKey = process.argv[2]
const accountId = parseInt(process.argv[3])

if (isNaN(accountId)) {
console.error('Account ID must be a valid number')
process.exit(1)
}

insertApiKey(apiKey, accountId)
Loading