Skip to content
This repository was archived by the owner on May 13, 2023. It is now read-only.

Commit 7b322c9

Browse files
authored
Merge pull request #10 from innonymous/dev
v1.0.0
2 parents 35d64bf + e99f1ee commit 7b322c9

File tree

12 files changed

+72
-16
lines changed

12 files changed

+72
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
logs
22

3+
.venv/
34
env
45
.env
56

innonymous/api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
settings = APISettings()
1818

1919
app = FastAPI(
20-
version='0.0.2', title='InnonymousApi', root_path=settings.root_path or ''
20+
version='1.0.0', title='InnonymousApi', root_path=settings.root_path or ''
2121
)
2222
app.add_middleware(
2323
CORSMiddleware,

innonymous/api/schemas/room/create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66

77
class RoomCreateSchema(BaseModel):
8-
name: constr(regex=r'^.{5,32}$')
8+
name: constr(regex=r'^[\w0-9][\w0-9\s\-_]{3,30}[\w0-9]$')

innonymous/api/schemas/token/payload/create.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class TokenCreatePayloadSchema(UserCreateSchema):
99
uuid: UUID
1010
captcha: str
11+
exp: float
1112

1213
class Config:
1314
extra = Extra.forbid

innonymous/api/schemas/user/create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66

77
class UserCreateSchema(BaseModel):
8-
name: constr(regex=r'^.{5,32}$')
8+
name: constr(regex=r'^[\w0-9][\w0-9\s\-_]{0,30}[\w0-9]$')

innonymous/api/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class APISettings(BaseSettings):
1111
amqp_url: str
1212
database_url: str
1313

14+
captcha_ttl: int = 300
15+
minimal_delay: float = 0.5
16+
1417
# Root of the api endpoints.
1518
root_path: Optional[str]
1619

innonymous/api/utils/_captcha.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import hmac
2-
from os import urandom
2+
from random import choices
3+
from string import digits, ascii_lowercase
34

45
from captcha.image import ImageCaptcha
56

67

78
class Captcha:
8-
length = 8
9+
length = 4
910
algorithm = 'SHA256'
1011

12+
# a-z, 0-9, except similar characters.
13+
alphabet = ''.join(
14+
set(ascii_lowercase + digits).difference(set('9q0ocda17uv6b'))
15+
)
16+
1117
def __init__(self, key: str) -> None:
1218
self.__key = key.encode()
1319
self.__image = ImageCaptcha()
1420

1521
def generate(self) -> tuple[str, bytes]:
16-
payload = urandom((Captcha.length + 1) // 2).hex()
22+
payload = ''.join(choices(Captcha.alphabet, k=Captcha.length))
1723

1824
with self.__image.generate(payload, 'jpeg') as buffer:
1925
captcha = buffer.getvalue()

innonymous/api/views/messages.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from innonymous.api import (
1616
auth,
1717
db_engine,
18-
mq
18+
mq,
19+
settings
1920
)
2021
from innonymous.api.schemas.message import (
2122
MessageCreateSchema,
@@ -77,7 +78,7 @@ async def create(
7778
user: UserModel = Depends(auth.authenticate),
7879
session: AsyncSession = Depends(db_engine.session)
7980
) -> None:
80-
if inactivity_interval(user).total_seconds() < 0.5:
81+
if inactivity_interval(user).total_seconds() < settings.minimal_delay:
8182
await update_active(user, session)
8283

8384
raise HTTPException(
@@ -101,7 +102,6 @@ async def create(
101102
session.add(message)
102103

103104
await session.commit()
104-
await update_active(room, session)
105105
await update_active(user, session)
106106

107107
await mq.publish(MessageInfoSchema.from_orm(message))

innonymous/api/views/rooms.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
from innonymous.api import (
1212
auth,
13-
db_engine
13+
db_engine,
14+
settings,
15+
mq
1416
)
17+
from innonymous.api.schemas.message import MessageInfoSchema
1518
from innonymous.api.schemas.room import (
1619
RoomCreateSchema,
1720
RoomInfoSchema,
@@ -23,7 +26,9 @@
2326
)
2427
from innonymous.database.models import (
2528
RoomModel,
26-
UserModel
29+
UserModel,
30+
MessageModel,
31+
MessageType
2732
)
2833
from innonymous.database.utils import get_all, get_by
2934

@@ -64,7 +69,7 @@ async def create(
6469
user: UserModel = Depends(auth.authenticate),
6570
session: AsyncSession = Depends(db_engine.session)
6671
) -> RoomInfoSchema:
67-
if inactivity_interval(user).total_seconds() < 0.5:
72+
if inactivity_interval(user).total_seconds() < settings.minimal_delay:
6873
await update_active(user, session)
6974

7075
raise HTTPException(
@@ -79,4 +84,12 @@ async def create(
7984
await session.refresh(room)
8085
await update_active(user, session)
8186

87+
message = MessageModel(
88+
type=MessageType.text, data=b'Room created.', user=user, room=room
89+
)
90+
91+
session.add(message)
92+
await session.commit()
93+
await mq.publish(MessageInfoSchema.from_orm(message))
94+
8295
return RoomInfoSchema.from_orm(room)

innonymous/api/views/users.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from base64 import b64encode
2+
import time
23
from uuid import (
34
UUID,
45
uuid4
@@ -15,7 +16,8 @@
1516
from innonymous.api import (
1617
auth,
1718
captcha,
18-
db_engine
19+
db_engine,
20+
settings
1921
)
2022
from innonymous.api.schemas.token import TokenInfoSchema
2123
from innonymous.api.schemas.token.payload import (
@@ -54,7 +56,10 @@ async def create(user: UserCreateSchema) -> UserConfirmSchema:
5456

5557
# noinspection Pydantic
5658
payload = TokenCreatePayloadSchema(
57-
uuid=uuid4(), captcha=_hash, **user.dict()
59+
uuid=uuid4(),
60+
captcha=_hash,
61+
exp=time.time() + settings.captcha_ttl,
62+
**user.dict(),
5863
)
5964

6065
return UserConfirmSchema(

0 commit comments

Comments
 (0)