-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathauth_clear_password.py
49 lines (35 loc) · 1.31 KB
/
auth_clear_password.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import logging
import asyncio
from mysql_mimic import (
MysqlServer,
IdentityProvider,
User,
)
from mysql_mimic.auth import AbstractClearPasswordAuthPlugin
logger = logging.getLogger(__name__)
# Storing plain text passwords is not safe.
# This is just done for demonstration purposes.
USERS = {
"user1": "password1",
"user2": "password2",
}
class CustomAuthPlugin(AbstractClearPasswordAuthPlugin):
name = "custom_plugin"
async def check(self, username, password):
return username if USERS.get(username) == password else None
class CustomIdentityProvider(IdentityProvider):
def get_plugins(self):
return [CustomAuthPlugin()]
async def get_user(self, username):
# Because we're storing users/passwords in an external system (the USERS dictionary, in this case),
# we just assume all users exist.
return User(name=username, auth_plugin=CustomAuthPlugin.name)
async def main():
logging.basicConfig(level=logging.DEBUG)
identity_provider = CustomIdentityProvider()
server = MysqlServer(identity_provider=identity_provider)
await server.serve_forever()
if __name__ == "__main__":
asyncio.run(main())
# To connect with the MySQL command line interface:
# mysql -h127.0.0.1 -P3306 -uuser1 -ppassword1 --enable-cleartext-plugin