Added auth and webmessages

This commit is contained in:
BarsTiger
2023-07-16 23:52:18 +03:00
commit 90c8cbb4a1
9 changed files with 143 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
/.idea/
/venv/
/tests/
/build/
/dist/
/data/
__pycache__
poetry.lock

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# dragonion-core
Core library for both [[dragonion]](https://github.com/dragonionx/dragonion) and
[[dragonion-server]](https://github.com/dragonionx/dragonion-server)

View File

@@ -0,0 +1 @@
pass

View File

@@ -0,0 +1 @@
pass

View File

@@ -0,0 +1,6 @@
from .auth import AuthFile
__all__ = [
'AuthFile'
]

View File

@@ -0,0 +1,15 @@
import sqlitedict
class AuthFile(sqlitedict.SqliteDict):
"""
Valid AuthFile has fields:
host - .onion url of service
auth - v3 onion auth string in format, that can be written to .auth_private file
"""
def __init__(self, service):
super().__init__(
filename=f'{service}.auth',
tablename='auth',
autocommit=True
)

View File

@@ -0,0 +1,22 @@
from .webmessage import (
webmessage_type_literal,
webmessage_error_message_literal,
WebMessageMessage,
WebErrorMessage,
WebNotificationMessage,
WebUserMessage,
WebMessage,
webmessages_union
)
__all__ = [
'webmessage_type_literal',
'webmessage_error_message_literal',
'WebMessageMessage',
'WebErrorMessage',
'WebNotificationMessage',
'WebUserMessage',
'WebMessage',
'webmessages_union'
]

View File

@@ -0,0 +1,69 @@
from typing import Literal, Final, Union
from dataclasses_json import dataclass_json
from dataclasses import dataclass
webmessage_type_literal = Literal[
"connect", "message", "disconnect", "error", "notification"
]
webmessage_error_message_literal = Literal[
"unknown", "username_exists", "invalid_webmessage"
]
@dataclass_json
@dataclass
class _WebAnyMessage:
username: str | None = None
type: webmessage_type_literal = "message"
message: str | None = None
error_message: webmessage_error_message_literal | None = None
@dataclass_json
@dataclass
class WebMessageMessage:
username: str
message: str
type: Final = "message"
@dataclass_json
@dataclass
class WebErrorMessage:
error_message: webmessage_error_message_literal
type: Final = "error"
@dataclass_json
@dataclass
class WebUserMessage:
type: Literal["connect", "disconnect"]
username: str
@dataclass_json
@dataclass
class WebNotificationMessage:
message: str
type: Final = "notification"
webmessages_union = Union[
WebMessageMessage,
WebErrorMessage,
WebUserMessage,
WebNotificationMessage
]
class WebMessage:
@staticmethod
def from_json(data) -> webmessages_union:
return {
"connect": WebUserMessage.from_json,
"disconnect": WebUserMessage.from_json,
"message": WebMessageMessage.from_json,
"error": WebErrorMessage.from_json,
"notification": WebNotificationMessage.from_json
}[_WebAnyMessage.from_json(data).type](data)

17
pyproject.toml Normal file
View File

@@ -0,0 +1,17 @@
[tool.poetry]
name = "dragonion-core"
version = "0.1.0"
description = ""
authors = ["BarsTiger <zxcbarstiger@gmail.com>"]
readme = "README.md"
packages = [{include = "dragonion_core"}]
[tool.poetry.dependencies]
python = "^3.10"
dataclasses-json = "^0.5.9"
sqlitedict = "^2.1.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"