Skip to content

Commit

Permalink
Use bcrypt rather than crypt in simple_server example
Browse files Browse the repository at this point in the history
`crypt` was removed from Python 3.13.  `bcrypt` isn't ideal, but it has
acceptable password hashing, is simple to use, and is already an
optional dependency of asyncssh.
  • Loading branch information
cjwatson authored and ronf committed Oct 13, 2024
1 parent f1848c1 commit 416db0e
Showing 1 changed file with 10 additions and 6 deletions.
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

0 comments on commit 416db0e

Please sign in to comment.