-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat(password): add command to change AstrBot dashboard password #8272
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| from .cmd_conf import conf | ||
| from .cmd_init import init | ||
| from .cmd_password import password | ||
| from .cmd_plug import plug | ||
| from .cmd_run import run | ||
|
|
||
| __all__ = ["conf", "init", "plug", "run"] | ||
| __all__ = ["conf", "init", "password", "plug", "run"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import click | ||
|
|
||
| from .cmd_conf import ( | ||
| _load_config, | ||
| _save_config, | ||
| _set_dashboard_password, | ||
| _set_nested_item, | ||
| _validate_dashboard_password, | ||
| _validate_dashboard_username, | ||
| ) | ||
|
|
||
|
|
||
| @click.command(name="password") | ||
| @click.option( | ||
| "--username", | ||
| help="Optional dashboard username to set together with the new password.", | ||
| ) | ||
| def password(username: str | None) -> None: | ||
| """Change the AstrBot dashboard password.""" | ||
| config = _load_config() | ||
|
|
||
| new_password = click.prompt( | ||
| "New dashboard password", | ||
| hide_input=True, | ||
| confirmation_prompt=True, | ||
| ) | ||
| validated_password = _validate_dashboard_password(new_password) | ||
|
|
||
| if username is not None: | ||
| validated_username = _validate_dashboard_username(username.strip()) | ||
| _set_nested_item(config, "dashboard.username", validated_username) | ||
|
|
||
| _set_dashboard_password(config, validated_password) | ||
| _save_config(config) | ||
|
|
||
| click.echo("Dashboard password updated.") | ||
| if username is not None: | ||
| click.echo(f"Dashboard username updated: {validated_username}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import copy | ||
| import json | ||
|
|
||
| from click.testing import CliRunner | ||
|
|
||
| from astrbot.cli.commands.cmd_conf import conf | ||
| from astrbot.cli.commands.cmd_password import password | ||
| from astrbot.core.config.default import DEFAULT_CONFIG | ||
| from astrbot.core.utils.auth_password import verify_dashboard_password | ||
|
|
||
|
|
||
| def _write_config(root): | ||
| (root / ".astrbot").touch() | ||
| data_dir = root / "data" | ||
| data_dir.mkdir() | ||
| config = copy.deepcopy(DEFAULT_CONFIG) | ||
| config["dashboard"]["password_change_required"] = True | ||
| config["dashboard"]["password_storage_upgraded"] = False | ||
| config_path = data_dir / "cmd_config.json" | ||
| config_path.write_text( | ||
| json.dumps(config, ensure_ascii=False, indent=2), | ||
| encoding="utf-8-sig", | ||
| ) | ||
| return config_path | ||
|
|
||
|
|
||
| def _read_config(config_path): | ||
| return json.loads(config_path.read_text(encoding="utf-8-sig")) | ||
|
|
||
|
|
||
| def test_password_command_changes_dashboard_password(monkeypatch, tmp_path): | ||
| config_path = _write_config(tmp_path) | ||
| monkeypatch.chdir(tmp_path) | ||
|
|
||
| runner = CliRunner() | ||
| result = runner.invoke( | ||
| password, | ||
| input="AstrbotChanged123\nAstrbotChanged123\n", | ||
| ) | ||
|
|
||
| assert result.exit_code == 0 | ||
| config = _read_config(config_path) | ||
| dashboard_config = config["dashboard"] | ||
| assert verify_dashboard_password( | ||
| dashboard_config["pbkdf2_password"], | ||
| "AstrbotChanged123", | ||
| ) | ||
| assert verify_dashboard_password( | ||
| dashboard_config["password"], | ||
| "AstrbotChanged123", | ||
| ) | ||
| assert dashboard_config["password_storage_upgraded"] is True | ||
| assert dashboard_config["password_change_required"] is False | ||
|
|
||
|
|
||
| def test_password_command_can_update_dashboard_username(monkeypatch, tmp_path): | ||
| config_path = _write_config(tmp_path) | ||
| monkeypatch.chdir(tmp_path) | ||
|
|
||
| runner = CliRunner() | ||
| result = runner.invoke( | ||
| password, | ||
| ["--username", "astrbot-admin"], | ||
| input="AstrbotChanged123\nAstrbotChanged123\n", | ||
| ) | ||
|
|
||
| assert result.exit_code == 0 | ||
| config = _read_config(config_path) | ||
| assert config["dashboard"]["username"] == "astrbot-admin" | ||
|
|
||
|
|
||
| def test_conf_set_dashboard_password_updates_password_state(monkeypatch, tmp_path): | ||
| config_path = _write_config(tmp_path) | ||
| monkeypatch.chdir(tmp_path) | ||
|
|
||
| runner = CliRunner() | ||
| result = runner.invoke( | ||
| conf, | ||
| ["set", "dashboard.password", "AstrbotChanged123"], | ||
| ) | ||
|
|
||
| assert result.exit_code == 0 | ||
| config = _read_config(config_path) | ||
| dashboard_config = config["dashboard"] | ||
| assert verify_dashboard_password( | ||
| dashboard_config["pbkdf2_password"], | ||
| "AstrbotChanged123", | ||
| ) | ||
| assert dashboard_config["password_storage_upgraded"] is True | ||
| assert dashboard_config["password_change_required"] is False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the
value_procargument inclick.promptallows for immediate validation of the password input. This improves the user experience by allowingclickto automatically re-prompt the user if the validation fails, rather than exiting the command entirely. Note that for the retry behavior to work automatically, the validator should ideally raiseclick.UsageErrororValueError.