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): for line in linegen(file):
split=line.split(',') split=line.split(',')
if split[pos]==name: return obj(*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): def _load_multi(name,pos,file,obj):
out=[] out=[]
for line in linegen(file): for line in linegen(file):
@ -62,7 +62,7 @@ def delete_invite(token):
def linegen(path): def linegen(path):
i=0 i=0
file=open(path) file=open(path)
line=file.readline() line=file.readline().rstrip('\n') # Maybe sometimes there won't be one.
while line: while line:
i+=1 i+=1
response=yield line response=yield line
@ -91,7 +91,7 @@ def writefile(path,action,post=None):
while line: while line:
i+=1 i+=1
action(write,i,line) action(write,i,line)
line=file.readline() line=file.readline().rstrip('\n')
if post is not None: post(write) if post is not None: post(write)
file.close() file.close()
write.close() write.close()

View File

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