Skip to content

Commit

Permalink
Key validation (reworkd#219)
Browse files Browse the repository at this point in the history
* added regex key validation to setup.sh

* added key validation to setup script

* added key validation to settings dialog
  • Loading branch information
matthope1 authored Apr 18, 2023
1 parent 6741975 commit 0004cc8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
17 changes: 15 additions & 2 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
#!/bin/bash
cd "$(dirname "$0")" || exit

echo -n "Enter your OpenAI Key (eg: sk...): "
is_valid_sk_key() {
local api_key=$1
local pattern="^sk-[a-zA-Z0-9]{48}$"
[[ $api_key =~ $pattern ]] && return 0 || return 1
}

echo -n "Enter your OpenAI Key (eg: sk...) or press enter to continue with no key: "
read OPENAI_API_KEY

if is_valid_sk_key $OPENAI_API_KEY || [ -z "$OPENAI_API_KEY" ]; then
echo "Valid API key"
else
echo "Invalid API key. Please ensure that you have billing set up on your OpenAI account"
exit
fi

NEXTAUTH_SECRET=$(openssl rand -base64 32)

ENV="NODE_ENV=development\n\
Expand All @@ -24,4 +37,4 @@ else
./prisma/useSqlite.sh
npm install
npm run dev
fi
fi
14 changes: 12 additions & 2 deletions src/components/SettingsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,19 @@ export default function SettingsDialog({
close();
};

function is_valid_key(key: string) {
const pattern = /^sk-[a-zA-Z0-9]{48}$/;
return pattern.test(key);
}

const handleSave = () => {
setCustomApiKey(key);
close();
if (is_valid_key(key)) {
setCustomApiKey(key);
close();
}
else {
alert("key is invalid, please ensure that you have set up billing in your OpenAI account")
}
};

React.useEffect(() => {
Expand Down

0 comments on commit 0004cc8

Please sign in to comment.