Skip to content

Commit

Permalink
chore(checkpoint): auth WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
LatentDream committed Apr 30, 2024
1 parent 899c0dd commit 0886652
Show file tree
Hide file tree
Showing 15 changed files with 6,458 additions and 8,019 deletions.
8 changes: 4 additions & 4 deletions captain/services/auth/auth_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ def compare_pass(hashed_token: str, plain_token: str):

def has_cloud_access(username: str, plain_token: str) -> bool:
user = get_user(username, plain_token)
if not user or user == "Local":
if not user or not user.is_connected:
return False
return True


def has_write_access(username: str, plain_token: str) -> bool:
user = get_user(username, plain_token)
if not user or user.permission == "Operator":
if not user or user.role == "Operator":
return False
return True

Expand Down Expand Up @@ -57,9 +57,9 @@ def save_user(user: Auth) -> bool:
if not os.path.exists(db_path):
open(db_path, "x").close()

with open(db_path, "rw") as f:
with open(db_path, "r+") as f:
config = json.load(f)
config.user = user
config["user"] = user.model_dump()
json.dump(config, f)
return True

Expand Down
30 changes: 23 additions & 7 deletions captain/types/auth.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
from enum import StrEnum
from pydantic import BaseModel
from typing import Optional
from pydantic import BaseModel, Field


class Permission(StrEnum):
class Role(StrEnum):
admin = "Admin"
operator = "Operator"
localUser = "Local"


class CloudConnection(BaseModel):
token: str
workspace: str
cloud_url: str = Field(None, alias="cloudUrl")


class Auth(BaseModel):
username: str
permission: Permission
# If connected with cloud
token: str
url: str
role: Role
connection: Optional[CloudConnection]

@property
def token(self) -> str:
return self.connection.token if self.connection else ""

@token.setter
def token(self, value: str):
if self.connection is not None:
self.connection.token = value

@property
def is_connected(self) -> bool:
return True if self.connection else False
Loading

0 comments on commit 0886652

Please sign in to comment.