From afbf18867be786a75556d6732b2022443f45d0e7 Mon Sep 17 00:00:00 2001 From: SepehrRasouli Date: Fri, 13 Sep 2024 07:51:42 +0330 Subject: [PATCH 01/12] Added keyring usage in Auth class --- hey/openai.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hey/openai.py b/hey/openai.py index e832809..f436c3f 100644 --- a/hey/openai.py +++ b/hey/openai.py @@ -1,5 +1,6 @@ import os import time +import keyring from openai import OpenAI, OpenAIError from rich.console import Console @@ -16,14 +17,13 @@ def __init__(self) -> None: self.is_valid = False def validate(self) -> str: - token = os.environ.get("HEY_TOKEN", None) + token = keyring.get_password("system","HEY_TOKEN") if token: self.is_valid = True return token - else: - raise TokenIsNotDefined( - "make sure the `HEY_TOKEN` is defined in the .bashrc/.zshrc file." - ) + raise TokenIsNotDefined( + "Token is not defined, Use `hey token [token]` to set token." + ) class ChatGPT: From 3a216e868c2e425b1b671f6110715b1f182a31eb Mon Sep 17 00:00:00 2001 From: SepehrRasouli Date: Fri, 13 Sep 2024 07:55:55 +0330 Subject: [PATCH 02/12] Added token command to get token from user --- hey/cli.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/hey/cli.py b/hey/cli.py index 9434861..cb946ca 100644 --- a/hey/cli.py +++ b/hey/cli.py @@ -74,3 +74,12 @@ def ask( result = answer(user_input, no_style) console.print(result) + +@app.command() +def token( + user_input: str, +): + """ + Take HEY_TOKEN from user. + """ + keyring.set_password("system","HEY_TOKEN",user_input) From ec289c6834ec7bcdce6dd82437a0bc62eeeccd46 Mon Sep 17 00:00:00 2001 From: SepehrRasouli Date: Fri, 13 Sep 2024 07:56:32 +0330 Subject: [PATCH 03/12] Added keyring to requirements in pyproject.toml --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index b247959..785e103 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ dependencies = [ "rich >= 13.7.1", "openai >= 1.30.1", "platformdirs >= 4.2.2" + "keyring" ] readme = { file = "README.md", content-type = "text/markdown" } license = { file = "LICENSE" } From ed3ad90a3c214d6c9bc44da4e061ce4fb770f8a2 Mon Sep 17 00:00:00 2001 From: SepehrRasouli Date: Fri, 13 Sep 2024 08:02:51 +0330 Subject: [PATCH 04/12] Fixed small bugs and errors. --- hey/cli.py | 1 + pyproject.toml | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/hey/cli.py b/hey/cli.py index cb946ca..8048353 100644 --- a/hey/cli.py +++ b/hey/cli.py @@ -2,6 +2,7 @@ from typing import Annotated, Optional import typer +import keyring from rich.markdown import Markdown from rich.panel import Panel diff --git a/pyproject.toml b/pyproject.toml index 785e103..d947b82 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,8 +12,8 @@ dependencies = [ "typer >= 0.12.3", "rich >= 13.7.1", "openai >= 1.30.1", - "platformdirs >= 4.2.2" - "keyring" + "platformdirs >= 4.2.2", + "keyring >= 25.3.0" ] readme = { file = "README.md", content-type = "text/markdown" } license = { file = "LICENSE" } From 9773ba60d028542aaca77d20acfeb9ed2da1d48b Mon Sep 17 00:00:00 2001 From: Sepehr Rasouli Date: Fri, 13 Sep 2024 08:26:55 +0330 Subject: [PATCH 05/12] Update README.md --- README.md | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index afe2707..d58ac24 100644 --- a/README.md +++ b/README.md @@ -31,18 +31,9 @@ pip install git+http://github.com/lnxpy/hey.git
-

2. Set the HEY_TOKEN environment variable

+

2. Set the HEY_TOKEN

-Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into either the `.bashrc` (or `.zshrc`) file. - -- If you use the default bash shell -```sh -echo "export HEY_TOKEN=" >> ~/.bashrc -``` -- If you use ZSH -```sh -echo "export HEY_TOKEN=" >> ~/.zshrc -``` +Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into hey, use `hey token [HEY_TOKEN]` to add your token.
From 0fce51b94593bad6f774aa42d8ebcb6c8f2503cc Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Sun, 15 Sep 2024 18:11:25 +0330 Subject: [PATCH 06/12] Changed TokenIsNotDefined error message --- hey/openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hey/openai.py b/hey/openai.py index f436c3f..548abc2 100644 --- a/hey/openai.py +++ b/hey/openai.py @@ -22,7 +22,7 @@ def validate(self) -> str: self.is_valid = True return token raise TokenIsNotDefined( - "Token is not defined, Use `hey token [token]` to set token." + "Token is not defined, Use `hey token` to set token." ) From b6f5576f5ad8dd57f8c91399b30bd2c5c6538212 Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Sun, 15 Sep 2024 18:19:49 +0330 Subject: [PATCH 07/12] Fixed security flaw in the token command as said by @lnxpy --- hey/cli.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hey/cli.py b/hey/cli.py index 8048353..c4735c6 100644 --- a/hey/cli.py +++ b/hey/cli.py @@ -3,6 +3,7 @@ import typer import keyring +import getpass from rich.markdown import Markdown from rich.panel import Panel @@ -78,9 +79,10 @@ def ask( @app.command() def token( - user_input: str, + user_input: str, # This for unhandled exceptions that happen when user gives too many args. ): """ Take HEY_TOKEN from user. """ - keyring.set_password("system","HEY_TOKEN",user_input) + password = getpass.getpass("Copy and paste token here, [Token will not be echoed] > ") + keyring.set_password("system","HEY_TOKEN",password) From c7266e0f817e1defaaeb2f20aa28b0fe60c817d4 Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Sun, 15 Sep 2024 18:21:02 +0330 Subject: [PATCH 08/12] Changed README.MD to reflect new changes --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d58ac24..12b9d1a 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,9 @@ pip install git+http://github.com/lnxpy/hey.git

2. Set the HEY_TOKEN

-Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into hey, use `hey token [HEY_TOKEN]` to add your token. +Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into hey, use `hey token` to add your token. +> :warning:: Your token will not be echoed.
### Usage From 210886cf6dc3a6d6eadc8ba3e25589f861e9957b Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Sun, 15 Sep 2024 18:31:11 +0330 Subject: [PATCH 09/12] Made a name change in the token command, changing it from token to auth, also made minor changes in other files to reflect it. --- README.md | 2 +- hey/cli.py | 4 ++-- hey/openai.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 12b9d1a..fcf2613 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ pip install git+http://github.com/lnxpy/hey.git

2. Set the HEY_TOKEN

-Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into hey, use `hey token` to add your token. +Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into hey, use `hey auth` to add your token. > :warning:: Your token will not be echoed.
diff --git a/hey/cli.py b/hey/cli.py index c4735c6..7d69fbe 100644 --- a/hey/cli.py +++ b/hey/cli.py @@ -78,11 +78,11 @@ def ask( console.print(result) @app.command() -def token( +def auth( user_input: str, # This for unhandled exceptions that happen when user gives too many args. ): """ Take HEY_TOKEN from user. """ - password = getpass.getpass("Copy and paste token here, [Token will not be echoed] > ") + password = getpass.getpass("Copy and paste your token here, [Token will not be echoed] > ") keyring.set_password("system","HEY_TOKEN",password) diff --git a/hey/openai.py b/hey/openai.py index 548abc2..752f3c8 100644 --- a/hey/openai.py +++ b/hey/openai.py @@ -22,7 +22,7 @@ def validate(self) -> str: self.is_valid = True return token raise TokenIsNotDefined( - "Token is not defined, Use `hey token` to set token." + "Token is not defined, Use `hey auth` to set your token." ) From e4201d9256908dd07a419dcfd403d4a71e065c9c Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Mon, 16 Sep 2024 20:53:34 +0330 Subject: [PATCH 10/12] changed uppercase word to lowercase --- hey/openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hey/openai.py b/hey/openai.py index 752f3c8..e36ae31 100644 --- a/hey/openai.py +++ b/hey/openai.py @@ -22,7 +22,7 @@ def validate(self) -> str: self.is_valid = True return token raise TokenIsNotDefined( - "Token is not defined, Use `hey auth` to set your token." + "token is not defined, Use `hey auth` to set your token." ) From 3df166d4453b30d1510b8cb97235ce4039d603ba Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Mon, 16 Sep 2024 20:54:18 +0330 Subject: [PATCH 11/12] removed user_input from the auth function --- hey/cli.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hey/cli.py b/hey/cli.py index 7d69fbe..379e8d1 100644 --- a/hey/cli.py +++ b/hey/cli.py @@ -78,9 +78,7 @@ def ask( console.print(result) @app.command() -def auth( - user_input: str, # This for unhandled exceptions that happen when user gives too many args. -): +def auth(): """ Take HEY_TOKEN from user. """ From 4275c47c5000821d842981c043e2cc7816b7d3f2 Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Mon, 16 Sep 2024 20:58:36 +0330 Subject: [PATCH 12/12] added hey_token_key to consts.py --- hey/cli.py | 3 ++- hey/consts.py | 1 + hey/openai.py | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/hey/cli.py b/hey/cli.py index 379e8d1..0b56890 100644 --- a/hey/cli.py +++ b/hey/cli.py @@ -12,6 +12,7 @@ from hey.consts import APP_NAME from hey.editor import open_tmp_editor from hey.openai import answer +from hey.consts import HEY_TOKEN_KEY app = typer.Typer() app.add_typer(cli.app, name="config") @@ -83,4 +84,4 @@ def auth(): Take HEY_TOKEN from user. """ password = getpass.getpass("Copy and paste your token here, [Token will not be echoed] > ") - keyring.set_password("system","HEY_TOKEN",password) + keyring.set_password("system",HEY_TOKEN_KEY,password) diff --git a/hey/consts.py b/hey/consts.py index 76771d3..dd9b281 100644 --- a/hey/consts.py +++ b/hey/consts.py @@ -5,6 +5,7 @@ # App name APP_NAME = "Hey" +HEY_TOKEN_KEY = "HEY_TOKEN" if platform.system().lower() == "windows": DEFAULT_EDITOR = os.environ.get("EDITOR", "notepad") diff --git a/hey/openai.py b/hey/openai.py index e36ae31..e0948ee 100644 --- a/hey/openai.py +++ b/hey/openai.py @@ -9,6 +9,7 @@ from hey.configs import configs from hey.consts import BASE_CONFIG +from hey.consts import HEY_TOKEN_KEY from hey.exceptions import ConnectionIssue, TokenIsNotDefined @@ -17,7 +18,7 @@ def __init__(self) -> None: self.is_valid = False def validate(self) -> str: - token = keyring.get_password("system","HEY_TOKEN") + token = keyring.get_password("system",HEY_TOKEN_KEY) if token: self.is_valid = True return token