Skip to content

Commit

Permalink
Fixes issues with logout
Browse files Browse the repository at this point in the history
  • Loading branch information
SaahilNotSahil committed Jan 12, 2024
1 parent 2c14751 commit dd145b0
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 31 deletions.
4 changes: 3 additions & 1 deletion autorepo/commands/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from autorepo.utils import (add_user, delete_auth_token, get_current_user,
remove_user, set_auth_token, set_current_user,
token_exists)
set_default_current_user, token_exists)


@click.command(
Expand Down Expand Up @@ -40,13 +40,15 @@ def login_cmd():
)
def logout_cmd():
current_user = get_current_user()

if not current_user:
click.echo("You are not logged in to any GitHub account", err=True)

return

delete_auth_token(current_user)
remove_user(current_user)
set_default_current_user()

click.echo(f"Logged out of {current_user}")

Expand Down
9 changes: 5 additions & 4 deletions autorepo/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ def list_group():
def users_cmd():
users = list_users()

if not users:
if len(users) == 0:
click.echo("No users have logged in to autorepo")

return

click.echo("Users:\n")
click.echo(f"{len(users)} user(s) have logged in to autorepo\n")
click.echo("Users:")

for user in users:
click.echo(f"{user}\n")
for i, user in enumerate(users):
click.echo(f"{i+1}: {user}")


@click.command(
Expand Down
2 changes: 1 addition & 1 deletion autorepo/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from autorepo.utils.auth import (add_user, delete_auth_token, get_auth_token,
get_current_user, list_users, remove_user,
set_auth_token, set_current_user,
token_exists)
set_default_current_user, token_exists)
from autorepo.utils.list import (list_gitignore_templates, list_licenses,
list_repositories)
from autorepo.utils.repo import (add_remote, clone_repo, create_repo,
Expand Down
121 changes: 97 additions & 24 deletions autorepo/utils/auth.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,126 @@
import keyring


def get_auth_token(username):
return keyring.get_password("autorepo", f"gh_token_{username}")
def get_auth_token(username: str) -> str | None:
try:
token = keyring.get_password("autorepo", f"gh_token_{username}")
except Exception:
return None

return token

def set_auth_token(username, token):
keyring.set_password("autorepo", f"gh_token_{username}", token)

def token_exists(username: str) -> bool:
return get_auth_token(username) is not None

def delete_auth_token(username):
keyring.delete_password("autorepo", f"gh_token_{username}")

def set_auth_token(username: str, token: str) -> bool:
try:
keyring.set_password("autorepo", f"gh_token_{username}", token)

def set_current_user(username):
keyring.set_password("autorepo", "current_user", username)
return True
except Exception:
return False


def get_current_user():
return keyring.get_password("autorepo", "current_user")
def delete_auth_token(username: str) -> bool:
if not token_exists(username):
return False

try:
keyring.delete_password("autorepo", f"gh_token_{username}")

def token_exists(username):
return get_auth_token(username) is not None
return True
except Exception:
return False


def list_users():
users = keyring.get_password("autorepo", "all_users")
if users is None:
return []
def set_current_user(username: str) -> bool:
if not token_exists(username):
return False

try:
keyring.set_password("autorepo", "current_user", username)

return True
except Exception:
return False


def get_current_user() -> str | None:
try:
username = keyring.get_password("autorepo", "current_user")
except Exception:
return None

return username


def delete_current_user() -> bool:
try:
keyring.delete_password("autorepo", "current_user")

return True
except Exception:
return False


return users.split(";")
def list_users() -> list:
try:
users = keyring.get_password("autorepo", "all_users")
if users == "" or users == " " or users is None:
return []

users = users.split(";")

def add_user(username):
return users
except Exception:
return []


def add_user(username: str) -> bool:
users = list_users()
if username in users:
return
return False

users.append(username)

keyring.set_password("autorepo", "all_users", ";".join(users))
try:
keyring.set_password("autorepo", "all_users", ";".join(users))

return True
except Exception:
return False

def remove_user(username):

def remove_user(username: str) -> bool:
users = list_users()
if username not in users:
return
return False

users.pop(users.index(username))

try:
keyring.set_password("autorepo", "all_users", ";".join(users))

return True
except Exception:
return False


def set_default_current_user() -> bool:
users = list_users()
if len(users) == 0:
delete_current_user()

return False

return set_current_user(users[0])

users.remove(username)

keyring.set_password("autorepo", "all_users", ";".join(users))
if __name__ == "__main__":
print(list_users())
print(len(list_users()))
print(get_current_user())
print(delete_current_user())
print(get_current_user())
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "autorepo"
version = "0.1.7"
version = "0.1.8"
description = "AutoRepo is a simple, cli-based tool for creating github repositories, without leaving your terminal!"
authors = ["XanderWatson <saahil_bhavsar@outlook.com>"]
readme = "README.md"
Expand Down

0 comments on commit dd145b0

Please sign in to comment.