Skip to content

Commit fca8610

Browse files
authored
Enhance GitHub token input validation and confirmation (#207)
2 parents 758d343 + d1ddb60 commit fca8610

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## v1.7.3-alpha
45
## v1.7.2-alpha
56
## v1.7.1-alpha
67
## v1.7.0-alpha

my_unicorn/auth.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,41 @@ def save_token() -> None:
107107
"""Prompt user for GitHub token and save it securely."""
108108
try:
109109
token: str = getpass.getpass(prompt="Enter your GitHub token (input hidden): ")
110-
if not token.strip():
110+
if token is None:
111+
logger.error("No input received for GitHub token.")
112+
raise ValueError("Token cannot be empty")
113+
114+
# Normalize input by stripping surrounding whitespace so accidental
115+
# spaces don't cause validation/confirmation mismatches. GitHub
116+
# tokens do not include leading/trailing whitespace in normal use.
117+
token = token.strip()
118+
if not token:
111119
logger.error("Attempted to save an empty GitHub token.")
112120
raise ValueError("Token cannot be empty")
113121

122+
# Confirm token input
123+
confirm_token: str = getpass.getpass(prompt="Confirm your GitHub token: ")
124+
if confirm_token is None:
125+
logger.error("No input received for GitHub token confirmation.")
126+
raise ValueError("Token confirmation does not match")
127+
128+
confirm_token = confirm_token.strip()
129+
if token != confirm_token:
130+
logger.error("GitHub token confirmation does not match.")
131+
raise ValueError("Token confirmation does not match")
132+
114133
# Validate token format before saving
115134
if not validate_github_token(token):
116135
logger.error("Invalid GitHub token format provided.")
117136
raise ValueError("Invalid GitHub token format. Must be a valid GitHub token.")
118137

119138
keyring.set_password(GitHubAuthManager.GITHUB_KEY_NAME, "token", token)
120139
logger.info("GitHub token saved successfully.")
140+
except (EOFError, KeyboardInterrupt) as e:
141+
logger.error("GitHub token input aborted: %s", e)
142+
raise ValueError("Token input aborted by user") from e
121143
except Exception as e:
122-
logger.error(f"Failed to save GitHub token to keyring: {e}")
144+
logger.error("Failed to save GitHub token to keyring: %s", e)
123145
raise
124146

125147
@staticmethod

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = 'my-unicorn'
3-
version = '1.7.2-alpha'
3+
version = '1.7.3-alpha'
44
maintainers = [{ name = "Cyber-Syntax" }]
55
license = { text = "GPL-3.0-or-later" }
66
description = 'My Unicorn is a command-line tool to manage AppImages on Linux. It allows users to install, update, and manage AppImages from GitHub repositories easily. It is designed to simplify the process of handling AppImages, making it more convenient for users to keep their applications up-to-date.'

0 commit comments

Comments
 (0)