Invite tokens were trying to save to the wrong place, but failing anyway, and some other bugs.

This commit is contained in:
Zergling_man 2024-03-24 05:16:36 +11:00
parent 620f780113
commit 43c4ff2d75
2 changed files with 20 additions and 7 deletions

View File

@ -15,7 +15,7 @@ def _load(name,pos,file,obj):
for line in linegen(file):
split=line.split(',')
if split[pos]==name: return obj(*split)
raise Exception('No such',obj.__name__,'with identifier',name)
raise Exception(f'No such {obj.__name__} with identifier {name}')
def _load_multi(name,pos,file,obj):
out=[]
for line in linegen(file):
@ -62,7 +62,7 @@ def delete_invite(token):
def linegen(path):
i=0
file=open(path)
line=file.readline()
line=file.readline().rstrip('\n') # Maybe sometimes there won't be one.
while line:
i+=1
response=yield line
@ -91,7 +91,7 @@ def writefile(path,action,post=None):
while line:
i+=1
action(write,i,line)
line=file.readline()
line=file.readline().rstrip('\n')
if post is not None: post(write)
file.close()
write.close()

View File

@ -24,12 +24,15 @@ class Token():
self.owner=owner
def revoke(self):
backend.delete_token(self)
@property
def serialise(self):
return ','.join([self.value,self.owner])
@dataclass
class InviteToken(Token):
_uses: int=0
_max_uses: int=-1
_expires: datetime=None
expires: datetime=None
def __init__(self,*args,**kwargs):
return super().__init__(*args,**kwargs)
@property
@ -44,6 +47,9 @@ class InviteToken(Token):
def max_uses(self,val):
if -1<val<=self.uses: self.revoke()
self._max_uses=val
@property
def serialise(self):
return ','.join([self.value,self.owner,self.uses,self.max_uses,self.expires])
@dataclass
class AccessToken(Token):
@ -55,14 +61,18 @@ class User(AbstractUser):
username: str
password_hash: str
salt: str
invited_by: AbstractUser=None # Root node will just reference itself
_invited_by: AbstractUser # Root node will just reference itself
@property
def invited_by(self):
if isinstance(self._invited_by,str): self._invited_by=backend.load_user(self._invited_by)
return self._invited_by
email: str=''
@property
def serialise(self):
return ','.join([self.username,self.password_hash,str(self.salt),self.invited_by.username,self.email])
def create_inv_token(self,*args,**kwargs):
tok=InviteToken(self,*args,**kwargs)
backend.save_token(tok)
backend.save_invite(tok)
return tok
def change_password(self,old_pw:str|None,new_pw:str): pass
def save(self):
@ -72,7 +82,10 @@ class User(AbstractUser):
def login(cls,username:str,password:str):
u=backend.load_user(username)
if u is None: raise Exception("User doesn't exist")
if phash(password,u.salt)[0]==u.password_hash: return AccessToken(u)
if phash(password,u.salt)[0]==u.password_hash:
a=AccessToken(u)
backend.save_token(a)
return a
raise Exception("Incorrect password")
@classmethod
def register(cls,username:str,password:str,invite:InviteToken,email:str|None):