Skip to content
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

Key validation #219

Merged
merged 3 commits into from
Apr 18, 2023
Merged
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
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