Skip to content

Commit 0ee897f

Browse files
pref: 优化 Token 存储 (#25)
1 parent 5d8a1b0 commit 0ee897f

File tree

9 files changed

+138
-94
lines changed

9 files changed

+138
-94
lines changed

README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ Python 3.12.0
4444
# 创建虚拟环境
4545
python -m venv .venv
4646
# 激活虚拟环境
47-
.venv\Scripts\Activate.ps1
47+
& ".venv\Scripts\Activate.ps1"
48+
4849
# 安装所需库
49-
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
50+
pip install -r requirements.txt
51+
# TIP: 镜像源 -i https://pypi.tuna.tsinghua.edu.cn/simple
5052
```
5153

5254
> [!TIP]
@@ -58,7 +60,7 @@ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
5860
> [!TIP]
5961
> 官方文档 → [管理个人访问令牌 - GitHub 文档](https://docs.github.com/zh/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#%E5%88%9B%E5%BB%BA-personal-access-token-classic)
6062
61-
请按照以下步骤获取GitHub Token。
63+
请按照以下步骤设置GitHub Token。
6264

6365
1. 登录 GitHub:
6466
打开 GitHub 并登录到您的账户。
@@ -87,9 +89,12 @@ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
8789
7. 复制生成的Token:
8890
*请注意,在此之后你将无法再次获取这个Token,请妥善保管Token,不要上传到公共仓库中。*
8991

90-
8. 将Token填入配置文件:
91-
将你的Token填入`config.json`中的`token`中。
92-
或者运行`glm config --token <YOUR-TOKEN>`
92+
8. 设置glm中的GitHub Token:
93+
运行`glm config --token <YOUR-TOKEN>`
94+
95+
> [!TIP]
96+
> 自1.6版本起,你可以使用`glm config --token remove`移除设置的Token。
97+
> 更新Token的命令和设置Token时的一样。
9398
9499
## 如何获取
95100
[前往Releases页获取](https://github.com/DuckDuckStudio/GitHub-Labels-Manager/releases)
@@ -101,7 +106,7 @@ winget install DuckStudio.GitHubLabelsManager
101106
```
102107

103108
> [!TIP]
104-
> 设支持的版本号为n,则 $1.1 \leqslant n \leqslant 1.3$
109+
> 设支持的版本号为n,则 $1.1 \leqslant n \leqslant 1.4$
105110
106111
## 如何使用
107112
运行以下命令以获取帮助:

other-languages/en_US/README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,21 @@ After cd into the project directory, run the following command:
4444
python -m venv .venv
4545
# Activate the virtual environment
4646
.venv\Scripts\Activate.ps1
47+
4748
# Install required libraries
4849
pip install -r requirements.txt
4950
```
5051

5152
> [!TIP]
52-
> Programs obtained using winget do not need to set this.
53+
> Programs obtained using _winget_ do not need to set this.
5354
5455
### Token
5556
You need a GitHub Token to run some of the features.
5657

5758
> [!TIP]
5859
> Official document → [Managing your personal access tokens - GitHub Docs](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#%E5%88%9B%E5%BB%BA-personal-access-token-classic)
5960
60-
Follow these steps to get a GitHub Token:
61+
Follow these steps to set a GitHub Token:
6162

6263
1. Sign in to GitHub:
6364
Open GitHub and sign in to your account.
@@ -86,9 +87,12 @@ Follow these steps to get a GitHub Token:
8687
7. Copy the generated Token:
8788
*Note: You won't be able to see this Token again, so make sure to store it securely and do not upload it to public repositories.*
8889

89-
8. Insert the Token into your configuration file:
90-
Paste your Token into the `token` field in the `config.json`.
91-
Or run `glm config --token <YOUR-TOKEN>`
90+
8. Set GitHub Token in glm:
91+
Run `glm config --token <YOUR-TOKEN>`
92+
93+
> [!TIP]
94+
> Since version 1.6, you can use `glm config --token remove` to remove a set Token.
95+
> The command for updating the Token is the same as that for setting the Token.
9296
9397
## How to get the program
9498
Please [go to the Releases page](https://github.com/DuckDuckStudio/GitHub-Labels-Manager/releases).
@@ -100,7 +104,7 @@ winget install DuckStudio.GitHubLabelsManager
100104
```
101105

102106
> [!TIP]
103-
> If the supported version number is *n*, $1.1 \leqslant n \leqslant 1.3$
107+
> If the supported version number is *n*, $1.1 \leqslant n \leqslant 1.4$
104108
105109
## How to use
106110
Run the following command for help:

other-languages/en_US/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"token": "None"
2+
"version": "1.6"
33
}

other-languages/en_US/glm.py

+53-36
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,74 @@
33
import json
44
import argparse
55
import requests
6+
import win32cred
67
import webbrowser
8+
import pywintypes
79
from tkinter import filedialog
810
from colorama import init, Fore
911

1012
init(autoreset=True)
1113

12-
version = "1.5"
14+
version = "1.6"
1315
script_path = os.path.dirname(os.path.abspath(sys.argv[0]))
1416
config_path = os.path.join(script_path, "config.json")
1517

1618
# -----------------------------------------------------------------------------------------------------
1719

1820
def read_token():
21+
# 凭据 github-access-token.glm
1922
try:
20-
with open(config_path, 'r') as file:
21-
data = json.load(file)
22-
token = data.get('token')
23-
if not token.startswith('ghp_'):
24-
print(f"{Fore.YELLOW}{Fore.RESET} Please check whether the Token is correct.")
25-
try:
26-
input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...")
27-
except KeyboardInterrupt:
28-
print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.")
29-
return "token error"
30-
return token
23+
token = win32cred.CredRead("github-access-token.glm", win32cred.CRED_TYPE_GENERIC)
24+
return token['CredentialBlob'].decode()
25+
except pywintypes.error as e:
26+
print(f"{Fore.YELLOW}{Fore.RESET} You may not have set the Token yet, please try using the following command to set the Token:\n glm config --token <YOUR-TOKEN>\n")
27+
return "error"
3128
except Exception as e:
32-
print(f"{Fore.RED}{Fore.RESET} Error reading configuration file:\n{Fore.RED}{e}{Fore.RESET}")
29+
print(f"{Fore.RED}{Fore.RESET} Error reading Token:\n{Fore.RED}{e}{Fore.RESET}")
3330
return "error"
3431

3532
def set_token(token):
36-
try:
37-
# --- Token 检查 ---
38-
if not token.startswith('ghp_'):
39-
print(f"{Fore.YELLOW}{Fore.RESET} Please check whether the Token is correct.")
40-
try:
41-
input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...")
42-
except KeyboardInterrupt:
43-
print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.")
44-
return "error"
45-
# -----------------
33+
# 凭据 github-access-token.glm
34+
# == 移除 ==
35+
if token == "remove":
36+
print(f"{Fore.YELLOW}{Fore.RESET} Are you sure you want to remove the set Token?")
37+
try:
38+
input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...")
39+
win32cred.CredDelete("github-access-token.glm", win32cred.CRED_TYPE_GENERIC)
40+
print(f"{Fore.GREEN}{Fore.RESET} The Token was successfully removed.")
41+
return "successful"
42+
except KeyboardInterrupt:
43+
print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.")
44+
return "error"
45+
except Exception as e:
46+
print(f"{Fore.RED}{Fore.RESET} An error occurred removing the set Token:\n{Fore.RED}{e}{Fore.RESET}\n")
47+
return "error"
4648

47-
# 读取现有的 JSON 文件
48-
with open(config_path, 'r') as file:
49-
data = json.load(file)
50-
51-
# 更新 token 字段
52-
data['token'] = token
53-
54-
# 将更新后的 JSON 写回文件
55-
with open(config_path, 'w') as file:
56-
json.dump(data, file, indent=4)
49+
# == 添加 ==
50+
# --- Token 检查 ---
51+
if not token.startswith('ghp_'):
52+
print(f"{Fore.YELLOW}{Fore.RESET} Please check whether the Token is correct.")
53+
try:
54+
input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...")
55+
except KeyboardInterrupt:
56+
print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.")
57+
return "error"
58+
# -----------------
59+
60+
cred = {
61+
'Type': win32cred.CRED_TYPE_GENERIC,
62+
'TargetName': "github-access-token.glm",
63+
'UserName': "github-access-token",
64+
'CredentialBlob': token,
65+
'Persist': win32cred.CRED_PERSIST_ENTERPRISE
66+
}
67+
68+
try:
69+
win32cred.CredWrite(cred, 0)
5770
print(f"{Fore.GREEN}{Fore.RESET} Successfully update Token.")
5871
return "successful"
5972
except Exception as e:
60-
print(f"{Fore.RED}{Fore.RESET} An error occurred processing the configuration file:\n{Fore.RED}{e}{Fore.RESET}\n")
73+
print(f"{Fore.RED}{Fore.RESET} An error occurred setting Token:\n{Fore.RED}{e}{Fore.RESET}\n")
6174
return "error"
6275

6376
def formatting_url(url):
@@ -373,8 +386,12 @@ def main():
373386
with open(config_path, 'r') as file:
374387
data = json.load(file)
375388

376-
token = data.get('token')
377-
print(f"{Fore.GREEN}{Fore.RESET} Current configuration information:\n Account configuration:\n Token: {Fore.BLUE}{token}{Fore.RESET}\n Program configuration:\n Version: {Fore.BLUE}GitHub Labels Manager v{version} by 鸭鸭「カモ」{Fore.RESET}\n Installed in: {Fore.BLUE}{script_path}{Fore.RESET}")
389+
if read_token() != 'error':
390+
token = f"{Fore.GREEN}Set{Fore.RESET}"
391+
else:
392+
token = f"{Fore.YELLOW}Not set or read error{Fore.RESET}"
393+
394+
print(f"{Fore.GREEN}{Fore.RESET} Current configuration information:\n Account configuration:\n Token: {token}\n Program configuration:\n Version: {Fore.BLUE}GitHub Labels Manager v{version} by 鸭鸭「カモ」{Fore.RESET}\n Installed in: {Fore.BLUE}{script_path}{Fore.RESET}")
378395
elif args.token:
379396
running_result = set_token(args.token)
380397
if running_result == "error":

pack.iss

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[Setup]
22
AppName=GitHub 标签管理器
3-
AppVersion=1.5
4-
VersionInfoVersion=1.5
3+
AppVersion=1.6
4+
VersionInfoVersion=1.6
55
AppPublisher=DuckStudio
66
VersionInfoCopyright=Copyright (c) 鸭鸭「カモ」
77
AppPublisherURL=https://duckduckstudio.github.io/yazicbs.github.io/
88
DefaultDirName={autopf}\GitHub_Labels_Manager
99
DefaultGroupName=GitHub 标签管理器
1010
UninstallDisplayIcon={app}\glm.exe
1111
OutputDir=.\version\Release\zh-CN
12-
OutputBaseFilename=GitHub_Labels_Manager_Setup_v1.5
12+
OutputBaseFilename=GitHub_Labels_Manager_Setup_v1.6
1313
SetupIconFile=.\ico.ico
1414
LicenseFile=.\LICENSE
1515
Compression=lzma2

packEN.iss

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[Setup]
22
AppName=GitHub Labels Manager
3-
AppVersion=1.5
4-
VersionInfoVersion=1.5
3+
AppVersion=1.6
4+
VersionInfoVersion=1.6
55
AppPublisher=DuckStudio
66
VersionInfoCopyright=Copyright (c) 鸭鸭「カモ」
77
AppPublisherURL=https://duckduckstudio.github.io/yazicbs.github.io/
88
DefaultDirName={autopf}\GitHub_Labels_Manager
99
DefaultGroupName=GitHub Labels Manager
1010
UninstallDisplayIcon={app}\glm.exe
1111
OutputDir=.\version\Release\en-US
12-
OutputBaseFilename=GitHub_Labels_Manager_Setup_v1.5-EN
12+
OutputBaseFilename=GitHub_Labels_Manager_Setup_v1.6-EN
1313
SetupIconFile=.\ico.ico
1414
LicenseFile=.\LICENSE
1515
Compression=lzma2

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
colorama==0.4.6
22
requests==2.32.3
3+
pywin32==306

程序脚本/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"token": "None"
2+
"version": "1.6"
33
}

程序脚本/glm.py

+54-37
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,74 @@
33
import json
44
import argparse
55
import requests
6+
import win32cred
67
import webbrowser
8+
import pywintypes
79
from tkinter import filedialog
810
from colorama import init, Fore
911

1012
init(autoreset=True)
1113

12-
version = "1.5"
14+
version = "1.6"
1315
script_path = os.path.dirname(os.path.abspath(sys.argv[0]))
1416
config_path = os.path.join(script_path, "config.json")
1517

1618
# -----------------------------------------------------------------------------------------------------
1719

1820
def read_token():
21+
# 凭据 github-access-token.glm
1922
try:
20-
with open(config_path, 'r') as file:
21-
data = json.load(file)
22-
token = data.get('token')
23-
if not token.startswith('ghp_'):
24-
print(f"{Fore.YELLOW}{Fore.RESET} 请确认Token是否正确")
25-
try:
26-
input(f"按{Fore.BLUE}Enter{Fore.RESET}键确认,按{Fore.BLUE}Ctrl + C{Fore.RESET}键取消...")
27-
except KeyboardInterrupt:
28-
print(f"{Fore.BLUE}[!]{Fore.RESET} 已取消操作")
29-
return "token error"
30-
return token
23+
token = win32cred.CredRead("github-access-token.glm", win32cred.CRED_TYPE_GENERIC)
24+
return token['CredentialBlob'].decode()
25+
except pywintypes.error as e:
26+
print(f"{Fore.YELLOW}{Fore.RESET} 你可能还没设置Token, 请尝试使用以下命令设置Token:\n glm config --token <YOUR-TOKEN>\n")
27+
return "error"
3128
except Exception as e:
32-
print(f"{Fore.RED}{Fore.RESET} 读取配置文件时出错:\n{Fore.RED}{e}{Fore.RESET}")
29+
print(f"{Fore.RED}{Fore.RESET} 读取Token时出错:\n{Fore.RED}{e}{Fore.RESET}")
3330
return "error"
3431

3532
def set_token(token):
36-
try:
37-
# --- Token 检查 ---
38-
if not token.startswith('ghp_'):
39-
print(f"{Fore.YELLOW}{Fore.RESET} 请确认Token是否正确")
40-
try:
41-
input(f"按{Fore.BLUE}Enter{Fore.RESET}键确认,按{Fore.BLUE}Ctrl + C{Fore.RESET}键取消...")
42-
except KeyboardInterrupt:
43-
print(f"{Fore.BLUE}[!]{Fore.RESET} 已取消操作")
44-
return "error"
45-
# -----------------
33+
# 凭据 github-access-token.glm
34+
# == 移除 ==
35+
if token == "remove":
36+
print(f"{Fore.YELLOW}{Fore.RESET} 确定要移除设置的Token?")
37+
try:
38+
input(f"按{Fore.BLUE}Enter{Fore.RESET}键确认,按{Fore.BLUE}Ctrl + C{Fore.RESET}键取消...")
39+
win32cred.CredDelete("github-access-token.glm", win32cred.CRED_TYPE_GENERIC)
40+
print(f"{Fore.GREEN}{Fore.RESET} 成功移除设置的Token")
41+
return "successful"
42+
except KeyboardInterrupt:
43+
print(f"{Fore.BLUE}[!]{Fore.RESET} 已取消操作")
44+
return "error"
45+
except Exception as e:
46+
print(f"{Fore.RED}{Fore.RESET} 移除设置的Token时出错:\n{Fore.RED}{e}{Fore.RESET}\n")
47+
return "error"
4648

47-
# 读取现有的 JSON 文件
48-
with open(config_path, 'r') as file:
49-
data = json.load(file)
50-
51-
# 更新 token 字段
52-
data['token'] = token
53-
54-
# 将更新后的 JSON 写回文件
55-
with open(config_path, 'w') as file:
56-
json.dump(data, file, indent=4)
49+
# == 添加 ==
50+
# --- Token 检查 ---
51+
if not token.startswith('ghp_'):
52+
print(f"{Fore.YELLOW}{Fore.RESET} 请确认Token是否正确")
53+
try:
54+
input(f"按{Fore.BLUE}Enter{Fore.RESET}键确认,按{Fore.BLUE}Ctrl + C{Fore.RESET}键取消...")
55+
except KeyboardInterrupt:
56+
print(f"{Fore.BLUE}[!]{Fore.RESET} 已取消操作")
57+
return "error"
58+
# -----------------
59+
60+
cred = {
61+
'Type': win32cred.CRED_TYPE_GENERIC,
62+
'TargetName': "github-access-token.glm",
63+
'UserName': "github-access-token",
64+
'CredentialBlob': token,
65+
'Persist': win32cred.CRED_PERSIST_ENTERPRISE
66+
}
67+
68+
try:
69+
win32cred.CredWrite(cred, 0)
5770
print(f"{Fore.GREEN}{Fore.RESET} 成功更新Token")
5871
return "successful"
5972
except Exception as e:
60-
print(f"{Fore.RED}{Fore.RESET} 处理配置文件时出错:\n{Fore.RED}{e}{Fore.RESET}\n")
73+
print(f"{Fore.RED}{Fore.RESET} 更新Token时出错:\n{Fore.RED}{e}{Fore.RESET}\n")
6174
return "error"
6275

6376
def formatting_url(url):
@@ -372,9 +385,13 @@ def main():
372385
if args.show:
373386
with open(config_path, 'r') as file:
374387
data = json.load(file)
375-
376-
token = data.get('token')
377-
print(f"{Fore.GREEN}{Fore.RESET} 当前配置信息如下:\n 账户设置:\n Token: {Fore.BLUE}{token}{Fore.RESET}\n 程序设置:\n 版本: {Fore.BLUE}GitHub Labels Manager v{version} by 鸭鸭「カモ」{Fore.RESET}\n 安装在: {Fore.BLUE}{script_path}{Fore.RESET}")
388+
389+
if read_token() != 'error':
390+
token = f"{Fore.GREEN}已设置{Fore.RESET}"
391+
else:
392+
token = f"{Fore.YELLOW}未设置或读取出错{Fore.RESET}"
393+
394+
print(f"{Fore.GREEN}{Fore.RESET} 当前配置信息如下:\n 账户设置:\n Token: {token}\n 程序设置:\n 版本: {Fore.BLUE}GitHub Labels Manager v{version} by 鸭鸭「カモ」{Fore.RESET}\n 安装在: {Fore.BLUE}{script_path}{Fore.RESET}")
378395
elif args.token:
379396
running_result = set_token(args.token)
380397
if running_result == "error":

0 commit comments

Comments
 (0)