-
Notifications
You must be signed in to change notification settings - Fork 79
Single User Auth #202
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
Merged
Merged
Single User Auth #202
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8c47298
feat: add single user auth via secret
stordahl f5333a5
test: use pool forks
stordahl 06852dc
some cleanup from copilot review
stordahl 24dbf17
feat: implement JWT based auth
stordahl dd8691b
chore: add generate-secrets script for dev
stordahl c694f77
fix: use secret as salt, address other review
stordahl ffe1675
chore: ignore generate-secrets script in codecov
stordahl 1434565
fix: clean up a bit
stordahl d65b92e
refactor: revert to not use JWT secret as hash salt
stordahl 5fe0246
more clean up
stordahl 8b90e8e
fix: add node compat flag
stordahl 3e396ce
refactor: explicitly handle missing secrets
stordahl 290dcaa
Merge branch 'main' into stordahl/auth
stordahl 50e5194
fix: rm rr node pkg
stordahl bed0158
feat: make auth optional
stordahl 7c38ca8
fix: add auth enabled env var to generate-secrets script
stordahl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env node | ||
|
||
import { intro, note, outro, isCancel, cancel } from "@clack/prompts"; | ||
import { generateCryptoSecret, generatePasswordHash } from "../dist/auth.js"; | ||
import { promptAppPassword } from "../dist/install.js"; | ||
|
||
async function main() { | ||
intro('🔐 Counterscale Development Secret Generator'); | ||
|
||
const jwtSecret = generateCryptoSecret(); | ||
; | ||
|
||
const userPassword = await promptAppPassword(); | ||
|
||
if (isCancel(userPassword)) { | ||
cancel('Operation cancelled'); | ||
process.exit(0); | ||
} | ||
|
||
try { | ||
const passwordHash = await generatePasswordHash(userPassword, jwtSecret); | ||
|
||
const output = [ | ||
'Copy these values to your .dev.vars file:', | ||
'', | ||
`CF_CRYPTO_SECRET='${jwtSecret}'`, | ||
`CF_PASSWORD_HASH='${passwordHash}'` | ||
].join('\n'); | ||
|
||
note(output, 'Generated Secrets'); | ||
outro('✅ Secrets generated successfully!'); | ||
|
||
} catch (error) { | ||
cancel(`Error generating password hash: ${error.message}`); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
main().catch(console.error); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import bcrypt from "bcryptjs"; | ||
import crypto from "node:crypto"; | ||
|
||
export async function generatePasswordHash(password:string, secret:string) { | ||
// Create a consistent salt from the secret | ||
const hash = crypto.createHash('sha256').update(secret).digest(); | ||
const saltBase64 = hash.subarray(0, 16).toString('base64').replace(/[+/=]/g, (c) => | ||
c === '+' ? '.' : c === '/' ? '/' : '' | ||
).substring(0, 22); | ||
|
||
const salt = `$2b$12$${saltBase64}`; | ||
return await bcrypt.hash(password, salt); | ||
} | ||
|
||
export function generateCryptoSecret() { | ||
return crypto.randomBytes(64).toString('hex'); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
# You also need to specify these as environment variables in | ||
# your Cloudflare configuration | ||
CF_BEARER_TOKEN='' | ||
CF_ACCOUNT_ID='' | ||
CF_ACCOUNT_ID='' | ||
CF_PASSWORD_HASH='' | ||
CF_CRYPTO_SECRET='' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.