Skip to content
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

Use bcrypt rather than crypt in simple_server example #697

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions examples/simple_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
# private key in it to use as a server host key. An SSH host certificate
# can optionally be provided in the file ``ssh_host_key-cert.pub``.

import asyncio, asyncssh, crypt, sys
import asyncio, asyncssh, bcrypt, sys
from typing import Optional

passwords = {'guest': '', # guest account with no password
'user123': 'qV2iEadIGV2rw' # password of 'secretpw'
passwords = {'guest': b'', # guest account with no password
'user123': bcrypt.hashpw(b'secretpw', bcrypt.gensalt()),
}

def handle_client(process: asyncssh.SSHServerProcess) -> None:
Expand All @@ -49,14 +49,18 @@ def connection_lost(self, exc: Optional[Exception]) -> None:

def begin_auth(self, username: str) -> bool:
# If the user's password is the empty string, no auth is required
return passwords.get(username) != ''
return passwords.get(username) != b''

def password_auth_supported(self) -> bool:
return True

def validate_password(self, username: str, password: str) -> bool:
pw = passwords.get(username, '*')
return crypt.crypt(password, pw) == pw
if username not in passwords:
return False
pw = passwords[username]
if not password and not pw:
return True
return bcrypt.checkpw(password.encode('utf-8'), pw)

async def start_server() -> None:
await asyncssh.create_server(MySSHServer, '', 8022,
Expand Down
Loading