Skip to content

Commit 0004cc8

Browse files
authoredApr 18, 2023
Key validation (reworkd#219)
* added regex key validation to setup.sh * added key validation to setup script * added key validation to settings dialog
1 parent 6741975 commit 0004cc8

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed
 

‎setup.sh

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
#!/bin/bash
22
cd "$(dirname "$0")" || exit
33

4-
echo -n "Enter your OpenAI Key (eg: sk...): "
4+
is_valid_sk_key() {
5+
local api_key=$1
6+
local pattern="^sk-[a-zA-Z0-9]{48}$"
7+
[[ $api_key =~ $pattern ]] && return 0 || return 1
8+
}
9+
10+
echo -n "Enter your OpenAI Key (eg: sk...) or press enter to continue with no key: "
511
read OPENAI_API_KEY
612

13+
if is_valid_sk_key $OPENAI_API_KEY || [ -z "$OPENAI_API_KEY" ]; then
14+
echo "Valid API key"
15+
else
16+
echo "Invalid API key. Please ensure that you have billing set up on your OpenAI account"
17+
exit
18+
fi
19+
720
NEXTAUTH_SECRET=$(openssl rand -base64 32)
821

922
ENV="NODE_ENV=development\n\
@@ -24,4 +37,4 @@ else
2437
./prisma/useSqlite.sh
2538
npm install
2639
npm run dev
27-
fi
40+
fi

‎src/components/SettingsDialog.tsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,19 @@ export default function SettingsDialog({
4545
close();
4646
};
4747

48+
function is_valid_key(key: string) {
49+
const pattern = /^sk-[a-zA-Z0-9]{48}$/;
50+
return pattern.test(key);
51+
}
52+
4853
const handleSave = () => {
49-
setCustomApiKey(key);
50-
close();
54+
if (is_valid_key(key)) {
55+
setCustomApiKey(key);
56+
close();
57+
}
58+
else {
59+
alert("key is invalid, please ensure that you have set up billing in your OpenAI account")
60+
}
5161
};
5262

5363
React.useEffect(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.