fix custom encrypted field set and get attrs

add group, user and union unicode return
master
Mustafa Yontar 4 years ago
parent e886e4e95c
commit 0870173e65
  1. 4
      Dockerfile
  2. 2
      docker-compose.yaml
  3. 38
      internal_lib/EncryptedField.py
  4. 2
      main.py
  5. 2
      models/Group.py
  6. 1
      models/Union.py
  7. 27
      models/User.py

@ -2,6 +2,8 @@ FROM python:3.9.1-buster
WORKDIR /code
ENV FLASK_APP=main.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_DEBUG=1
RUN apt install gcc
RUN apt-get update && apt-get install -y \
@ -14,4 +16,4 @@ COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
CMD ["flask", "run","--debugger"]

@ -21,9 +21,11 @@ services:
ME_CONFIG_BASICAUTH_PASSWORD: 4dun4710
web:
build: .
ports:
- "5000:5000"
environment:
ADUNATIO_PRIV_KEY: /code/privkey.pem
FLASK_DEBUG: 1
volumes:
- .:/code

@ -1,3 +1,4 @@
import binascii
import re
from mongoengine.base import BaseField
@ -17,17 +18,36 @@ class EncryptedStringField(BaseField):
super().__init__(**kwargs)
def __get__(self, instance, owner):
value = instance._data.get(self.name)
encryptor = PKCS1_OAEP.new(self.keyPair.publickey())
return encryptor.decrypt(value)
from flask import current_app as app
app.logger.error(self.name)
import binascii
if instance:
value = instance._data.get(self.name)
if value:
app.logger.error(value)
encryptor = PKCS1_OAEP.new(self.keyPair)
return encryptor.decrypt(binascii.unhexlify(value)).decode("utf-8")
else:
return None
def __set__(self, instance, value):
key = self.name
encryptor = PKCS1_OAEP.new(self.keyPair.publickey())
instance._data[key] = encryptor.encrypt(value)
instance._mark_as_changed(key)
super().__set__(instance, value)
if value is not None:
from flask import current_app as app
app.logger.error(self.name)
app.logger.error(instance._data)
app.logger.error("data : {} ".format(value))
print(self.name)
print(instance._data)
print(value)
key = self.name
try:
encryptor = PKCS1_OAEP.new(self.keyPair.publickey())
instance._data[key] = binascii.hexlify(encryptor.encrypt(value.encode('utf-8')))
instance._mark_as_changed(key)
except:
instance._data[key] = value
instance._mark_as_changed(key)
def to_python(self, value):
if isinstance(value, str):

@ -2,6 +2,7 @@ from flask import Flask
from flask_admin.contrib.mongoengine import ModelView
from mongoengine import connect
from models.Group import Group
from models.Union import Union
from models.User import User
from restapi import MongoApi
@ -19,6 +20,7 @@ api.register_model(User,uri="/api/user")
adm = Admin(app)
adm.add_view(ModelView(User))
adm.add_view(ModelView(Union))
adm.add_view(ModelView(Group))
if __name__ == '__main__':
app.run(host="0.0.0.0",port=5000,debug=True)

@ -7,6 +7,8 @@ class Group(Document):
union = ReferenceField(Union)
name = StringField()
rights = ListField(StringField())
def __unicode__(self):
return "{} {}".format(self.union.name,self.name)
class PaymentGroup(Document):

@ -22,6 +22,7 @@ class Union(Document):
group.rights = ["*::*::{}/*".format(self.id)]
group.save()
user = User()
user.gov_id = 'root'
user.username = "{}@root".format(self.legal_registration_number)
user.user_group = group
user.union = self

@ -3,7 +3,7 @@ from mongoengine import *
from internal_lib.EncryptedField import EncryptedStringField
from models.EmbededDocuments import Descriptions
from models.Group import Group
from models.Group import Group, PaymentGroup
from models.Union import Union
from restapi import Methods
@ -14,19 +14,20 @@ class User(Document):
'index_cls': False,
'auto_create_index': True,
'can_query': True,
'with_sub_docs': True,
"quyery": {},
'ignore_fields': ['password'],
'methods': [Methods.Get, Methods.List, Methods.Create],
"indexes": [
('union'),
('username','union'),
('username', 'union'),
('accept_date')
]
}
union = ReferenceField(Union)
member_no = LongField()
username = StringField()
photo = ImageField(thumbnail_size=(85,120))
photo = ImageField(thumbnail_size=(85, 120))
password = StringField()
name = StringField()
middle_name = StringField()
@ -105,14 +106,16 @@ class User(Document):
))
gnupg_fingerprint = StringField()
user_group = ReferenceField(Group)
payment_group = ReferenceField(Group)
def save(self,*args, **kwargs):
self.union = current_user.union
self.member_no = User.objects.filter(union=self.union).count() + 1
super(User, self).save(*args, **kwargs)
payment_group = ReferenceField(PaymentGroup)
def __unicode__(self):
try:
return ' '.join([self.name, self.middle_name, self.last_name])
except:
return str(self.id)
def save(self, *args, **kwargs):
if current_user:
self.union = current_user.union
self.member_no = User.objects.filter(union=self.union).count()
super(User, self).save(*args, **kwargs)

Loading…
Cancel
Save