class TestBase(TestCase): def setUp(self): self.db_session = DBSession() self.connection = engine.connect() # # Configure Search DDL triggers. Base.metadata.drop_all(self.connection) Base.metadata.create_all(self.connection) self.client = TestClient(app) def tearDown(self): self.db_session.rollback() self.db_session.close() def create_system_admin(self, *args, **kwargs): from app.controllers.account import create_account from app.schemas.account import AccountCreate return create_account( self.db_session, first_name="Admin", last_name="Istrator", email="*****@*****.**", password="******", is_system_admin=True, is_active=True, send_registration_email=False, ) def auth_headers(self, email="*****@*****.**", password="******"): payload = {"username": email, "password": password} resp = self.client.post("/auth/token", data=payload) return {"Authorization": "Bearer " + resp.json().get("access_token")}
def setUp(self): self.db_session = DBSession() self.connection = engine.connect() # # Configure Search DDL triggers. Base.metadata.drop_all(self.connection) Base.metadata.create_all(self.connection) self.client = TestClient(app)
async def create_admin(): """If admin account doesn't exist, create it.""" from app.database import DBSession from app.controllers.account import create_account from app.controllers.account import get_account_by_email db_session = DBSession() account_data = { "email": "*****@*****.**", "password": "******", "first_name": "Admin", "last_name": "Istrator", "is_system_admin": True, "is_active": True, } account_obj = get_account_by_email(db_session, email=account_data["email"]) if account_obj: return create_account(db_session, **account_data) db_session.close()
def by_aa(cls, aa): result = {} values = (DBSession.query( cls.name, cls.value, func.count(cls.value), cls.aaid).filter_by(aaid=aa.authorizable_attribute_id).group_by( cls.name, cls.value).all()) for v in values: existent_list = result.get(v[0], []) existent_list.append({v[1]: v[2]}) if v[2] >= aa.optional_k(v[0]): result[v[0]] = existent_list result["total"] = ValidatedCredentials.total_by_aaid( aa.authorizable_attribute_id) return result
def aggregate(cls): result = {} values = (DBSession.query(cls.name, cls.value, func.count(cls.value), cls.aaid).group_by(cls.name, cls.value).all()) for v in values: existent_list = result.get(v[0], []) existent_list.append({v[1]: v[2]}) aa = AuthorizableAttribute.by_aa_id(v[3]) if v[2] >= aa.optional_k(v[0]): result[v[0]] = existent_list result["total"] = ValidatedCredentials.total() return result
def prepared_db(): DBSession.configure(bind=db_engine) Base.metadata.create_all(db_engine)
def credential(item: ValidateAuthorizableAttributeInfoInput = Body( ..., example={ "authorizable_attribute_id": "Authorizable Attribute Unique Identifier", "blind_sign_request": { "request": { "pi_s": { "c": "9fbe906553d2d15959094a42e3863f71f90634a15172d30cf5d5cda555836c1a", "rk": "0cff3b2b8b9e28c63f4902303e4d6ba2c6aac096543098b87ff8fa817be22d61", "rm": "2698c1e293341ca1d28782a34ef74814249b516431aab963fcb31c19a22f41a0", "rr": "32c0c70c0f6a65b58ab8f23742e695743c4e3672f1ca4c92fd30545aab4e5b45", }, "zenroom": "0.8.1", "schema": "lambda", "c": { "a": "040b7c7b31f9c81daf812d87999a9f53e5a2e6c9b1a2a3df42db155066d1c22674acc579798d6d544427b23dd77b379b9e3d4556806f9b2bdd940aba61495f9d392011a639f8d75f1f0e68906f0e1a0d61eda3a2a621e4f173b35a6d00a36dd51d", "b": "040acd0a9a4b39409c17ea2cbf73354c9f6bb410e126c25865003dcc356bcec31a81c8696e6dbe4011962f98d316f475a01d76d8b95ddd99dc97ef67119f82ccd6bd5711e5c63af48414a945604d620ac4dbf357cd2b250fc787e98ac754b66805", }, "public": "0428f6fd3e9cb1b2a95acce09cc928097f8fe64802b30511d3b18e5fa0f1c50a902c0090b74942070f0fe5e2b84124590b0a45f37507a33ead6cd2f3650f606aea28dbe3506cd0011bddf7657de5d0211582803ea91e67103310d2f2b5c97509d3", "cm": "043a0ddff5a122cd75a4bda44bd220b13d0d05d2b2e751d02a30b92ca148fd5e56fdf7530bf8c0f1071dab4ccb504b49f522f54047cd15fc4b3a5b34a42c702a8e6e4d396d3e60eb88f309530389c903f1d9f0354f44b1de4d4dccdb28705e677f", "encoding": "hex", "curve": "bls383", } }, "values": [ { "name": "zip_code", "value": "08001" }, { "name": "email", "value": "*****@*****.**" }, ], "optional_values": [{ "name": "age", "value": "18-25" }], }, )): aa = __get_aa(item.authorizable_attribute_id) received_values = sorted([json.loads(_.json()) for _ in item.values], key=lambda k: k["name"]) __validate_reissuing(aa, received_values) __check_mandatory_info_fields(aa, received_values) __check_wrong_info_fields(aa, received_values) issued_credential = __issue_credential(aa.keypair, item.blind_sign_request.json()) vc = ValidatedCredentials(aaid=aa.authorizable_attribute_id, value=json.dumps(received_values)) DBSession.add(vc) __check_optional_values(aa, item.optional_values) for option in item.optional_values: option = json.loads(option.json()) s = Statistics( aaid=aa.authorizable_attribute_id, name=option["name"], value=option["value"], ) DBSession.add(s) DBSession.commit() return json.loads(issued_credential)
def authorizable_attribute( item: AuthorizableAttributeSchema = Body( ..., example={ "authorizable_attribute_id": "Authorizable Attribute %s" % "".join(random.choice(string.hexdigits) for i in range(10)), "authorizable_attribute_info": [ { "name": "email", "type": "str", "value_set": [ "*****@*****.**", "*****@*****.**", "*****@*****.**", ], }, { "name": "zip_code", "type": "int", "value_set": ["08001", "08002", "08003", "08004", "08005", "08006"], }, ], "authorizable_attribute_info_optional": [ { "name": "age", "type": "str", "value_set": ["0-18", "18-25", "25-45", ">45"], "k": 2, } ], "reissuable": False, }, ), token: str = Security(oauth2_scheme), ): info = [_.json() for _ in item.authorizable_attribute_info] optional = [_.json() for _ in item.authorizable_attribute_info_optional] keypair_contract = ZenContract(CONTRACTS.GENERATE_KEYPAIR, {"issuer_identifier": config.get("uid")}) keypair = keypair_contract.execute() contract = ZenContract(CONTRACTS.PUBLIC_VERIFY, {"issuer_identifier": config.get("uid")}) contract.keys(keypair) verification_key = contract.execute() aa = AuthorizableAttribute( authorizable_attribute_id=item.authorizable_attribute_id, authorizable_attribute_info=json.dumps(info), authorizable_attribute_info_optional=json.dumps(optional), keypair=keypair, verification_key=verification_key, reissuable=item.reissuable, ) try: DBSession.add(aa) DBSession.commit() except IntegrityError: DBSession.rollback() raise HTTPException(status_code=HTTP_409_CONFLICT, detail="Duplicate Authorizable Attribute Id") return { "verification_key": json.loads(verification_key), "credential_issuer_id": config.get("uid"), "authorizable_attribute_id": aa.authorizable_attribute_id, }
def total(cls): return DBSession.query(cls).count()
def exists(cls, aaid, value): return (DBSession.query(cls).filter_by(aaid=aaid).filter_by( value=json.dumps(value)).first())
def by_aa_id(cls, aa_id): return DBSession.query(cls).filter_by( authorizable_attribute_id=aa_id).first()
def total_by_aaid(cls, aaid): return DBSession.query(cls).filter_by(aaid=aaid).count()
def get_db(): try: db_session = DBSession() yield db_session finally: db_session.close()
def add(instance): session = DBSession() session.add(instance) session.commit()
def query(cls): return DBSession().query(cls)
def delete(instance): session = DBSession() session.delete(instance) session.commit()