-
Notifications
You must be signed in to change notification settings - Fork 49
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(backend): Allow project auto-claim with pre-defined token #763
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
from typing import Optional | ||
|
||
ENV_GLOBAL_CLAIM_TOKEN = "DOCAT_GLOBAL_CLAIM_TOKEN" | ||
ENV_GLOBAL_CLAIM_SALT = "DOCAT_GLOBAL_CLAIM_SALT" | ||
|
||
|
||
def get_global_claim_token() -> Optional[str]: | ||
"""Returns the global claim token which can be defined by an environment variable. | ||
|
||
Returns: | ||
The optional global claim token or None. | ||
""" | ||
return os.environ.get(ENV_GLOBAL_CLAIM_TOKEN, None) | ||
|
||
|
||
def get_global_claim_salt() -> Optional[bytes]: | ||
"""Returns the global claim salt which can be defined by an environment variable. | ||
|
||
Returns: | ||
The optional global claim salt or None. | ||
""" | ||
global_claim_salt = os.environ.get(ENV_GLOBAL_CLAIM_SALT, None) | ||
if global_claim_salt is not None: | ||
return global_claim_salt.encode() | ||
return None |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,14 @@ | |
""" | ||
import hashlib | ||
import os | ||
import secrets | ||
import shutil | ||
from pathlib import Path | ||
from zipfile import ZipFile | ||
|
||
from tinydb import Query, TinyDB | ||
|
||
from docat.constants import get_global_claim_salt, get_global_claim_token | ||
from docat.models import Project, ProjectDetail, Projects, ProjectVersion | ||
|
||
NGINX_CONFIG_PATH = Path("/etc/nginx/locations.d") | ||
|
@@ -162,3 +166,32 @@ def should_include(name: str) -> bool: | |
reverse=True, | ||
), | ||
) | ||
|
||
|
||
def claim_project(project: str, db: TinyDB) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just as an optional remark, I think this function could also take an api toke as parameter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 👍 |
||
"""Claims a project. | ||
|
||
Args: | ||
project: The project name. | ||
db: The database to use. | ||
|
||
Raises: | ||
PermissionError: If the project has already been claimed. | ||
|
||
Returns: | ||
The claim token. | ||
""" | ||
table = db.table("claims") | ||
|
||
# Check if the project has already been claimed | ||
if table.search(Query().name == project): | ||
raise PermissionError(f"Project {project} is already claimed!") | ||
|
||
# Check if the global claim token/salt is configured. Otherwise, use randomly generated values. | ||
token = get_global_claim_token() or secrets.token_hex(16) | ||
salt = get_global_claim_salt() or os.urandom(32) | ||
|
||
token_hash = calculate_token(token, salt) | ||
table.insert({"name": project, "token": token_hash, "salt": salt.hex()}) | ||
|
||
return token |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this exposed? 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I assumed that you only get the token back if you are authorized, but that is not implemented at all. I'll change the PR again tomorrow so that the token is only returned if it's not the globally defined one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before you implement too much please take a look at my other comment, i do my best to keep docat as simple and slim as possible with the least amount of features and im not convinced yet that this is in the current form is a essential feature so i would like to understand the use-case better to decide if we need this feature in the current form or something else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue that at least the use case where one can send a token on first time upload and directly claim the project makes sense both from a security perspective and automation perspective (like your CI/CD usecase @g3n35i5 ) -> #618.
With this implemented is the global env token still necessary or could your usecase also be covered by the first time submitted token feature you implemented yesterday?