Skip to content

Commit 2aab1a5

Browse files
authored
Merge pull request #5 from sanujar/main
Option to disable registration of new user accounts by env var.
2 parents 015d48c + f699e3b commit 2aab1a5

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Set it true to retrieve always a random device id to force a progress sync.
3737
This is usefull if you only sync your progress from one device and
3838
usually delete the *.sdr files with some cleaning tools.
3939

40+
* OPEN_REGISTRATIONS ("True"|"False")
41+
42+
Enable/disable new registrations to the server. Useful if you want to run a private server for a few users, although it doesn't necessarily improve security by itself.
43+
Set to True (enabled) by default.
4044

4145
## Dockerhub
4246

kosync.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,24 @@ class KosyncDocument(BaseModel):
3232

3333
@app.post("/users/create")
3434
def register(kosync_user: KosyncUser):
35-
# check if username or password is missing
36-
if kosync_user.username is None or kosync_user.password is None:
37-
return JSONResponse(status_code=400, content={"message": f"Invalid request"})
38-
# check if user already exists
39-
QUser = Query()
40-
if users.contains(QUser.username == kosync_user.username):
41-
return JSONResponse(status_code=409, content="Username is already registered.")
42-
# register new user
43-
if users.insert({'username': kosync_user.username, 'password': kosync_user.password}):
44-
return JSONResponse(status_code=201, content={"username": kosync_user.username})
45-
# if something went wrong
46-
return JSONResponse(status_code=500, content="Unknown server error")
35+
# Check whether new registrations are allowed on this server based on the OPEN_REGISTRATIONS environment variable.
36+
# By default registrations are enabled.
37+
registrations_allowed = bool(strtobool(getenv("OPEN_REGISTRATIONS", "True")))
38+
if registrations_allowed:
39+
# check if username or password is missing
40+
if kosync_user.username is None or kosync_user.password is None:
41+
return JSONResponse(status_code=400, content={"message": f"Invalid request"})
42+
# check if user already exists
43+
QUser = Query()
44+
if users.contains(QUser.username == kosync_user.username):
45+
return JSONResponse(status_code=409, content="Username is already registered.")
46+
# register new user
47+
if users.insert({'username': kosync_user.username, 'password': kosync_user.password}):
48+
return JSONResponse(status_code=201, content={"username": kosync_user.username})
49+
# if something went wrong
50+
return JSONResponse(status_code=500, content="Unknown server error")
51+
else:
52+
return JSONResponse(status_code=403, content="This server is currently not accepting new registrations.")
4753

4854

4955
@app.get("/users/auth")

0 commit comments

Comments
 (0)