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. 30
      internal_lib/EncryptedField.py
  4. 2
      main.py
  5. 2
      models/Group.py
  6. 1
      models/Union.py
  7. 19
      models/User.py

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

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

@ -1,3 +1,4 @@
import binascii
import re import re
from mongoengine.base import BaseField from mongoengine.base import BaseField
@ -17,16 +18,35 @@ class EncryptedStringField(BaseField):
super().__init__(**kwargs) super().__init__(**kwargs)
def __get__(self, instance, owner): def __get__(self, instance, owner):
from flask import current_app as app
app.logger.error(self.name)
import binascii
if instance:
value = instance._data.get(self.name) value = instance._data.get(self.name)
encryptor = PKCS1_OAEP.new(self.keyPair.publickey()) if value:
return encryptor.decrypt(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): def __set__(self, instance, value):
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 key = self.name
try:
encryptor = PKCS1_OAEP.new(self.keyPair.publickey()) encryptor = PKCS1_OAEP.new(self.keyPair.publickey())
instance._data[key] = binascii.hexlify(encryptor.encrypt(value.encode('utf-8')))
instance._data[key] = encryptor.encrypt(value) instance._mark_as_changed(key)
except:
instance._data[key] = value
instance._mark_as_changed(key) instance._mark_as_changed(key)
def to_python(self, value): def to_python(self, value):

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

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

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

@ -3,7 +3,7 @@ from mongoengine import *
from internal_lib.EncryptedField import EncryptedStringField from internal_lib.EncryptedField import EncryptedStringField
from models.EmbededDocuments import Descriptions from models.EmbededDocuments import Descriptions
from models.Group import Group from models.Group import Group, PaymentGroup
from models.Union import Union from models.Union import Union
from restapi import Methods from restapi import Methods
@ -14,6 +14,7 @@ class User(Document):
'index_cls': False, 'index_cls': False,
'auto_create_index': True, 'auto_create_index': True,
'can_query': True, 'can_query': True,
'with_sub_docs': True,
"quyery": {}, "quyery": {},
'ignore_fields': ['password'], 'ignore_fields': ['password'],
'methods': [Methods.Get, Methods.List, Methods.Create], 'methods': [Methods.Get, Methods.List, Methods.Create],
@ -105,14 +106,16 @@ class User(Document):
)) ))
gnupg_fingerprint = StringField() gnupg_fingerprint = StringField()
user_group = ReferenceField(Group) user_group = ReferenceField(Group)
payment_group = ReferenceField(Group) 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): def save(self, *args, **kwargs):
if current_user:
self.union = current_user.union self.union = current_user.union
self.member_no = User.objects.filter(union=self.union).count() + 1 self.member_no = User.objects.filter(union=self.union).count()
super(User, self).save(*args, **kwargs) super(User, self).save(*args, **kwargs)

Loading…
Cancel
Save