This commit is contained in:
parent
b3bae6f5ad
commit
bd59c40ac6
20 changed files with 431 additions and 25 deletions
|
|
@ -22,7 +22,8 @@ class KnownIdentity(models.Model):
|
|||
def __str__(self):
|
||||
return f"{self.username}@{self.domain}"
|
||||
|
||||
def is_authenticated(self):
|
||||
@staticmethod
|
||||
def is_authenticated():
|
||||
return True
|
||||
|
||||
def friends_or_self(self):
|
||||
|
|
@ -30,9 +31,9 @@ class KnownIdentity(models.Model):
|
|||
public_identity=self)
|
||||
|
||||
def verify(self, message, signature):
|
||||
if len(signature) != 128 or type(signature) != str:
|
||||
if len(signature) != 128 or not isinstance(signature, str):
|
||||
raise TypeError('Signature must be 128 characters long and a string')
|
||||
if type(message) != str:
|
||||
if not isinstance(message, str):
|
||||
raise TypeError('Message must be a string')
|
||||
try:
|
||||
VerifyKey(bytes.fromhex(self.public_key)).verify(message.encode('utf-8'), bytes.fromhex(signature))
|
||||
|
|
@ -44,14 +45,17 @@ class KnownIdentity(models.Model):
|
|||
class ToolshedUserManager(auth.models.BaseUserManager):
|
||||
def create_user(self, username, email, password, **extra_fields):
|
||||
domain = extra_fields.pop('domain', 'localhost')
|
||||
private_key_hex = extra_fields.pop('private_key', None)
|
||||
if private_key_hex and type(private_key_hex) != str:
|
||||
raise TypeError('Private key must be a string or no private key must be provided')
|
||||
if private_key_hex and len(private_key_hex) != 64:
|
||||
raise ValueError('Private key must be 64 characters long or no private key must be provided')
|
||||
if private_key_hex and not all(c in '0123456789abcdef' for c in private_key_hex):
|
||||
raise ValueError('Private key must be a hexadecimal string or no private key must be provided')
|
||||
private_key = SigningKey(bytes.fromhex(private_key_hex)) if private_key_hex else SigningKey.generate()
|
||||
private_key_hex: str | None = extra_fields.pop('private_key', None)
|
||||
if private_key_hex is not None:
|
||||
if not isinstance(private_key_hex, str):
|
||||
raise TypeError('Private key must be a string or no private key must be provided')
|
||||
if len(private_key_hex) != 64:
|
||||
raise ValueError('Private key must be 64 characters long or no private key must be provided')
|
||||
if not all(c in '0123456789abcdef' for c in private_key_hex):
|
||||
raise ValueError('Private key must be a hexadecimal string or no private key must be provided')
|
||||
private_key = SigningKey(bytes.fromhex(private_key_hex))
|
||||
else:
|
||||
private_key = SigningKey.generate()
|
||||
public_key = SigningKey(private_key.encode()).verify_key
|
||||
extra_fields['private_key'] = private_key.encode(encoder=HexEncoder).decode('utf-8')
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import json
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.test import Client, RequestFactory
|
||||
from nacl.encoding import HexEncoder
|
||||
from nacl.signing import SigningKey
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue