-
Notifications
You must be signed in to change notification settings - Fork 20
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
[feat] Migrating users from config file to database #261
Draft
haasal
wants to merge
4
commits into
MatterMiners:master
Choose a base branch
from
haasal:user-db
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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,64 @@ | ||
import sqlite3 | ||
import json | ||
from typing import Tuple | ||
|
||
from .crud import ADD_USER, CREATE_USERS, DELETE_USER, DUMP_USERS, GET_USER | ||
from .security import DatabaseUser | ||
|
||
|
||
def to_db_user(user: tuple) -> DatabaseUser: | ||
return DatabaseUser( | ||
user_name=user[0], hashed_password=user[1], scopes=json.loads(user[2]) | ||
) | ||
|
||
|
||
class UserDB: | ||
def __init__(self, path: str): | ||
self.path = path | ||
|
||
def try_create_users(self): | ||
self.execute(CREATE_USERS) | ||
|
||
def drop_users(self): | ||
self.execute("DROP TABLE Users") | ||
|
||
def add_user(self, user: DatabaseUser): | ||
try: | ||
_, conn = self.execute( | ||
ADD_USER, | ||
(user.user_name, user.hashed_password, json.dumps(user.scopes)), | ||
) | ||
conn.commit() | ||
except sqlite3.IntegrityError as e: | ||
if str(e) == "UNIQUE constraint failed: Users.user_name": | ||
raise Exception("USER EXISTS") from None | ||
else: | ||
raise e | ||
|
||
def get_user(self, user_name: str) -> DatabaseUser: | ||
cur, _ = self.execute( | ||
GET_USER, | ||
[user_name], | ||
) | ||
user = cur.fetchone() | ||
|
||
if user is None: | ||
raise Exception("USER NOT FOUND") from None | ||
|
||
return to_db_user(user) | ||
|
||
def dump_users(self): | ||
cur, _ = self.execute(DUMP_USERS) | ||
return cur.fetchall() | ||
|
||
def delete_user(self, user_name: str): | ||
_, conn = self.execute(DELETE_USER, [user_name]) | ||
conn.commit() | ||
|
||
def execute( | ||
self, sql: str, args: list = [] | ||
) -> Tuple[sqlite3.Cursor, sqlite3.Connection]: | ||
conn = sqlite3.connect(self.path) | ||
cur = conn.cursor() | ||
cur.execute(sql, args) | ||
return cur, conn |
Empty file.
This file contains 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,88 @@ | ||
from importlib.resources import path | ||
from turtle import st | ||
from typing import List | ||
import typer | ||
|
||
from .util import hash_password | ||
from ..app.userdb import UserDB | ||
from ..app.security import DatabaseUser | ||
|
||
app = typer.Typer() | ||
|
||
""" | ||
Sample usage: | ||
> create-table | ||
> add-user alice password resources:get user:get | ||
> get-user alice | ||
> delete-user alice | ||
""" | ||
|
||
|
||
@app.command() | ||
def add_user(name: str, password: str, scopes: List[str], path: str = "users.db"): | ||
""" | ||
Add a user to the database. Password gets hashed. | ||
""" | ||
print(f"Adding user {name} with scopes {scopes}") | ||
db = UserDB(path) | ||
dbuser = DatabaseUser( | ||
user_name=name, hashed_password=hash_password(password), scopes=scopes | ||
) | ||
db.add_user(dbuser) | ||
|
||
|
||
@app.command() | ||
def create_db(path: str = "users.db"): | ||
""" | ||
Create a database file with a Users table. | ||
Doesn't overwrite existing files or tables. | ||
""" | ||
print("Creating db with users table") | ||
db = UserDB(path) | ||
db.try_create_users() | ||
|
||
|
||
@app.command() | ||
def drop_users(path: str = "users.db"): | ||
""" | ||
Drop the Users table. | ||
""" | ||
print("Dropping users table") | ||
db = UserDB(path) | ||
db.drop_users() | ||
|
||
|
||
@app.command() | ||
def dump_users(path: str = "users.db"): | ||
""" | ||
Dump all users from the database. | ||
""" | ||
print("Dumping users table") | ||
db = UserDB(path) | ||
for user in db.dump_users(): | ||
print(user) | ||
|
||
|
||
@app.command() | ||
def get_user(user_name: str, path: str = "users.db"): | ||
""" | ||
Print a user from the database. | ||
""" | ||
print("Getting user") | ||
db = UserDB(path) | ||
user = db.get_user(user_name) | ||
print(user) | ||
|
||
|
||
@app.command() | ||
def delete_user(user_name: str, path: str = "users.db"): | ||
""" | ||
Delete a user from the database. | ||
""" | ||
print("Deleting user") | ||
db = UserDB(path) | ||
db.delete_user(user_name) | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
This file contains 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,6 @@ | ||
import bcrypt | ||
|
||
|
||
def hash_password(password: str) -> bytes: | ||
salt = bcrypt.gensalt() | ||
return bcrypt.hashpw(password.encode(), salt) |
This file contains 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
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.
Check notice
Code scanning / CodeQL
Unused import