-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
51 lines (42 loc) · 1.93 KB
/
backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from pywarp.backends import CredentialStorageBackend
from pywarp import Credential
class InvalidChallengeType(Exception):
pass
class SQLliteBackend(CredentialStorageBackend):
def __init__(self, db, user_model):
self.database_client = db
self.user_model = user_model
def get_or_create(self, email):
user = self.user_model.query.filter_by(email=email).first()
if user is None:
user = self.user_model(email)
self.database_client.session.add(user)
self.database_client.session.commit()
return user
def get_credential_by_email(self, email):
user_record = self.user_model.query.filter_by(email=email).first()
return Credential(credential_id=user_record.credential_id,
credential_public_key=user_record.public_key)
def save_credential_for_user(self, email, credential):
user_record = self.user_model.query.filter_by(email=email).first()
user_record.credential_id = credential.id
user_record.public_key = bytes(credential.public_key)
self.database_client.session.commit()
def save_challenge_for_user(self, email, challenge, type):
user = self.get_or_create(email)
user_record = self.user_model.query.filter_by(email=email).first()
if type == "registration":
user_record.registration_challenge = challenge
elif type == "authentication":
user_record.authentication_challenge = challenge
else:
raise InvalidChallengeType
self.database_client.session.commit()
def get_challenge_for_user(self, email, type):
user_record = self.user_model.query.filter_by(email=email).first()
if type == "registration":
return user_record.registration_challenge
elif type == "authentication":
return user_record.authentication_challenge
else:
raise InvalidChallengeType