Пример #1
0
def save_token(token_props, req, *args, **kwargs):
    """
    Saves token to the database
    """
    result_token = None
    token_id = token_props.get('id')
    token = OauthAccessTokenEntity.get_by_id(token_id)
    # log.debug("From {} got {}".format(token_id, token))

    if token and not token.is_expired():
        # log.debug("Reuse access token: {} expiring on {} ({} seconds left)"
        #           .format(token.id, token.expires, token.expires_in))
        result_token = token
    else:
        access_token = utils.generate_token()
        # access_token = utils.generate_token_urandom(TOKEN_LENGTH)
        expires = datetime.utcnow() + timedelta(seconds=TOKEN_EXPIRES_SECONDS)
        added_at = utils.get_db_friendly_date_time()

        if token:
            result_token = OauthAccessTokenEntity.update(
                token,
                access_token=access_token,
                expires=expires,
                added_at=added_at)
        else:
            result_token = OauthAccessTokenEntity.create(
                access_token=access_token,
                token_type=TOKEN_TYPE_BEARER,
                _scopes='',
                expires=expires,
                client_id=req.client.client_id,
                added_at=added_at)
    # log.info("return from save_token: {}".format(result_token))
    return result_token
Пример #2
0
    def create_partners(self):
        """
        Create rows
        """
        added_date = utils.get_db_friendly_date_time()
        partner_uf = PartnerEntity.create(
            partner_code="UF",
            partner_description="University of Florida",
            partner_added_at=added_date)

        partner_fh = PartnerEntity.create(
            partner_code="FH",
            partner_description="Florida Hospital",
            partner_added_at=added_date)

        self.assertIsNotNone(partner_uf.id)
        self.assertIsNotNone(partner_fh.id)
        self.assertEquals("UF", partner_uf.partner_code)
        self.assertEquals("FH", partner_fh.partner_code)

        # verify that more than one
        with self.assertRaises(MultipleResultsFound):
                PartnerEntity.query.filter(
                    PartnerEntity.
                    partner_description.like(
                        '%Florida%')).one()
Пример #3
0
    def create_sample_data(self):
        """ Add some data """
        added_date = utils.get_db_friendly_date_time()

        sample_data = [
            {"first": "Aida", "last": " Xenon", "dob": "1910-11-12",
                "zip": "19116", "city": "GAINESVILLE"},
            {"first": "aida", "last": "xeNon ", "dob": "1910/11/12",
                "zip": "19116", "city": "gainesville"},
            {"first": "AIDA", "last": "XENON ", "dob": "1910:11:12  ",
                "zip": "19116", "city": "Gainesville"},
            {"first": "AiDa", "last": "XEnON ", "dob": "19101112  ",
                "zip": "19116", "city": "gainesviLLe"},
            {"first": "John", "last": "Doe", "dob": "1900-01-01",
                "zip": "32606", "city": "Palatca"},
            {"first": "JOHN", "last": "DOE", "dob": "1900-01-01",
                "zip": "32607", "city": "Palatca"}, ]

        partner = PartnerEntity.query.filter_by(
            partner_code='UF').one()

        # Note: first four entries produce the same hashes (aka chunks)
        for person_data in sample_data:
            person_orig = Person(person_data)
            person = Person.get_prepared_person(person_data)
            pers_uuid = utils.get_uuid_bin()
            hashes = get_person_hash(person,
                                     [RULE_CODE_F_L_D_Z, RULE_CODE_L_F_D_Z])

            for rule_id, ahash in hashes.items():
                link = LinkageEntity.create(
                    partner_id=partner.id,
                    linkage_uuid=pers_uuid,
                    linkage_hash=ahash,
                    linkage_addded_at=added_date)
                self.assertIsNotNone(link)

                links_by_hash = LinkageEntity.query.filter_by(
                    linkage_hash=ahash).all()

                verbose("==> Found {} link(s) for [{}] using hash: {}"
                        .format(
                            len(links_by_hash),
                            person_orig,
                            hexlify(ahash)))
Пример #4
0
    def create_partners(self):
        """
        Create rows
        """
        added_date = utils.get_db_friendly_date_time()
        partner_uf = PartnerEntity.create(
            partner_code="UF",
            partner_description="University of Florida",
            partner_added_at=added_date)

        partner_fh = PartnerEntity.create(
            partner_code="FH",
            partner_description="Florida Hospital",
            partner_added_at=added_date)

        self.assertIsNotNone(partner_uf.id)
        self.assertIsNotNone(partner_fh.id)
        self.assertEquals("UF", partner_uf.partner_code)
        self.assertEquals("FH", partner_fh.partner_code)

        # verify that more than one
        with self.assertRaises(MultipleResultsFound):
            PartnerEntity.query.filter(
                PartnerEntity.partner_description.like('%Florida%')).one()
Пример #5
0
    def create_oauth_users(self):
        """ Add user, role, user_role
        Note: partners should exist
        """
        added_at = utils.get_db_friendly_date_time()
        expires_date = utils.get_expiration_date(10)
        expires_date2 = utils.get_expiration_date(0)

        ##############
        # add role row
        role = OauthRoleEntity.create(
            role_code='root',
            role_description='super-user can do xyz...'
        )
        self.assertEquals(1, role.id)
        role = OauthRoleEntity.get_by_id(1)
        self.assertIsNotNone(role)
        verbose(role)

        ##############
        # add user row
        user = OauthUserEntity.create(
            email='*****@*****.**',
            password_hash='$6$rounds=666140$vQVDNQUwZCSDY0u7$kqmaQjQnYwWz9EQlms99UQDYaphVBwujnUs1H3XdhT741pY1HPirG1Y.oydcw3QtQnaMyVOspVZ20Dij7f24A/',  # NOQA
            added_at=added_at
        )
        self.assertEquals(1, user.id)
        verbose(user)

        ##############
        # add user_role row
        partner = PartnerEntity.query.filter_by(partner_code="UF").one()
        verbose(partner)

        user_role = OauthUserRoleEntity.create(
            partner_id=partner.id,
            user_id=user.id,
            role_id=role.id,
            added_at=added_at
        )
        user_role = OauthUserRoleEntity.get_by_id(1)
        self.assertIsNotNone(user_role)
        verbose(user_role)

        ##############
        # Verify that the user now is properly mapped to a partner and a role
        user = OauthUserEntity.get_by_id(1)
        self.assertIsNotNone(user.partner)
        self.assertIsNotNone(user.role)
        verbose(user)

        ##############
        # Verify that we can save a client, grant code, access token
        client = OauthClientEntity.create(
            client_id='client_1',
            client_secret='secret_1',
            user_id=user.id,
            added_at=added_at)

        client2 = OauthClientEntity.create(
            client_id='client_2',
            client_secret='secret_2',
            user_id=user.id,
            added_at=added_at)

        token = OauthAccessTokenEntity.create(
            client_id=client.client_id,
            token_type='Bearer',
            access_token='access_token_1',
            refresh_token='refresh_token_1',
            expires=expires_date,
            added_at=added_at)

        token2 = OauthAccessTokenEntity.create(
            client_id=client2.client_id,
            token_type='Bearer',
            access_token='access_token_2',
            refresh_token='',
            expires=expires_date2,
            added_at=added_at)

        self.assertIsNotNone(client.id)
        self.assertIsNotNone(client2.id)
        self.assertIsNotNone(token.id)
        self.assertIsNotNone(token2.id)

        # Verify the token properties
        self.assertIsNotNone(token.client)
        self.assertIsNotNone(token.client.user)
        self.assertIsNotNone(token.user)
        self.assertIsNotNone(token.user.id)
        ser = token.serialize()
        self.assertIsNotNone(ser.get('id'))
        self.assertIsNotNone(ser.get('access_token'))
        self.assertIsNotNone(ser.get('expires_in'))

        client = OauthClientEntity.get_by_id(1)
        client2 = OauthClientEntity.query.filter_by(client_id='client_1').one()
        role = OauthUserRoleEntity.get_by_id(1)

        self.assertIsNotNone(client)
        self.assertIsNotNone(client2)
        self.assertIsNotNone(role)
        verbose("Expect client: {}".format(client))
        verbose("Expect token: {}".format(token))
        verbose("Expect token serialized: {}".format(token.serialize()))
        verbose("Expect token2 serialized: {}".format(token2.serialize()))
Пример #6
0
def api_save_patient_hashes():
    """
    For each chunk save a new uuid or return an existing one from the database.

== Example of input json:
{
"data": {
   "1": {
       "1": "b2cdaea3d7c9891b2ed94d1973fe5085183e4bb4bd87b672e066a456ee67bd38"
       },

   "2": {
       "1": "345b192ae4093dcbc5c914bdcb5e8c41e58162475a295c88b1ce594bd3dd78f7",
       "2": "1dcce10470c0ea73a8a6287f69f4f862c5e13faea7c11104fae07dbc8d5ce56e"
       },
   "3": {
       "1": "995b192ae4093dcbc5c914bdcb5e8c41e58162475a295c88b1ce594bd3dd78f7"
       }
   }
}

== To find the rows in the database:
select hex(linkage_uuid), hex(linkage_hash) from linkage order by linkage_id;

    """
    auth_pieces = request.headers.get('Authorization').split(' ')
    log.debug("Authorization_header: {}".format(auth_pieces))
    access_token = auth_pieces[1].strip()
    partner = get_partner_from_token(access_token)
    json = request.get_json(silent=False)

    if not json:
        err = "Invalid json object specified"
        log.error(err)
        return utils.jsonify_error(err)

    result = collections.defaultdict(dict)
    json_data = json['data']

    # patient chunks are received in groups
    for pat_id, pat_chunks in json_data.items():
        # Since there is a chance of duplicate chunks find uniques
        chunks = list(set(chunk for chunk_num, chunk in pat_chunks.items()))
        chunks_cache = LinkageEntity.get_chunks_cache(chunks)
        uuids = LinkageEntity.get_distinct_uuids_for_chunks(chunks_cache)
        log.debug("Found [{}] matching uuids from [{}] chunks of patient "
                  "[{}]".format(len(uuids), len(chunks), pat_id))

        added_date = utils.get_db_friendly_date_time()

        if len(uuids) == 0:
            binary_uuid = utils.get_uuid_bin()
            hex_uuid = utils.hexlify(binary_uuid)
            log.debug("Generate [{}] for pat_id[{}]".format(pat_id, hex_uuid))
        elif len(uuids) == 1:
            hex_uuid = uuids.pop()
            binary_uuid = unhexlify(hex_uuid.encode('utf-8'))
            log.debug("Reuse [{}] for pat_id[{}]".format(pat_id, hex_uuid))
        else:
            log.error("Oops...It looks like we got a collision!"
                      "\n==> chunks: {}\n==> uuids: {}".format(chunks, uuids))
            raise Exception("More than one uuid for chunks attributed to "
                            "[{}] patient [{}]".format(partner.partner_code,
                                                       pat_id))

        # update the response json
        result[pat_id] = {"uuid": hex_uuid}

        for i, chunk in enumerate(chunks):
            if chunks_cache.get(chunk):
                log.debug("Skip chunk [{}] - already linked".format(chunk))
                continue

            # link every chunk to the same uuid
            binary_hash = unhexlify(chunk.encode('utf-8'))
            LinkageEntity.create(partner_id=partner.id,
                                 linkage_uuid=binary_uuid,
                                 linkage_hash=binary_hash,
                                 linkage_addded_at=added_date)
            log.debug("Created link [{}] for [{}]".format(i, chunk))

    return utils.jsonify_success(result)
Пример #7
0
    def create_oauth_users(self):
        """ Add user, role, user_role
        Note: partners should exist
        """
        added_at = utils.get_db_friendly_date_time()
        expires_date = utils.get_expiration_date(10)
        expires_date2 = utils.get_expiration_date(0)

        ##############
        # add role row
        role = OauthRoleEntity.create(
            role_code='root', role_description='super-user can do xyz...')
        self.assertEquals(1, role.id)
        role = OauthRoleEntity.get_by_id(1)
        self.assertIsNotNone(role)
        verbose(role)

        ##############
        # add user row
        user = OauthUserEntity.create(
            email='*****@*****.**',
            password_hash=
            '$6$rounds=666140$vQVDNQUwZCSDY0u7$kqmaQjQnYwWz9EQlms99UQDYaphVBwujnUs1H3XdhT741pY1HPirG1Y.oydcw3QtQnaMyVOspVZ20Dij7f24A/',  # NOQA
            added_at=added_at)
        self.assertEquals(1, user.id)
        verbose(user)

        ##############
        # add user_role row
        partner = PartnerEntity.query.filter_by(partner_code="UF").one()
        verbose(partner)

        user_role = OauthUserRoleEntity.create(partner_id=partner.id,
                                               user_id=user.id,
                                               role_id=role.id,
                                               added_at=added_at)
        user_role = OauthUserRoleEntity.get_by_id(1)
        self.assertIsNotNone(user_role)
        verbose(user_role)

        ##############
        # Verify that the user now is properly mapped to a partner and a role
        user = OauthUserEntity.get_by_id(1)
        self.assertIsNotNone(user.partner)
        self.assertIsNotNone(user.role)
        verbose(user)

        ##############
        # Verify that we can save a client, grant code, access token
        client = OauthClientEntity.create(client_id='client_1',
                                          client_secret='secret_1',
                                          user_id=user.id,
                                          added_at=added_at)

        client2 = OauthClientEntity.create(client_id='client_2',
                                           client_secret='secret_2',
                                           user_id=user.id,
                                           added_at=added_at)

        token = OauthAccessTokenEntity.create(client_id=client.client_id,
                                              token_type='Bearer',
                                              access_token='access_token_1',
                                              refresh_token='refresh_token_1',
                                              expires=expires_date,
                                              added_at=added_at)

        token2 = OauthAccessTokenEntity.create(client_id=client2.client_id,
                                               token_type='Bearer',
                                               access_token='access_token_2',
                                               refresh_token='',
                                               expires=expires_date2,
                                               added_at=added_at)

        self.assertIsNotNone(client.id)
        self.assertIsNotNone(client2.id)
        self.assertIsNotNone(token.id)
        self.assertIsNotNone(token2.id)

        # Verify the token properties
        self.assertIsNotNone(token.client)
        self.assertIsNotNone(token.client.user)
        self.assertIsNotNone(token.user)
        self.assertIsNotNone(token.user.id)
        ser = token.serialize()
        self.assertIsNotNone(ser.get('id'))
        self.assertIsNotNone(ser.get('access_token'))
        self.assertIsNotNone(ser.get('expires_in'))

        client = OauthClientEntity.get_by_id(1)
        client2 = OauthClientEntity.query.filter_by(client_id='client_1').one()
        role = OauthUserRoleEntity.get_by_id(1)

        self.assertIsNotNone(client)
        self.assertIsNotNone(client2)
        self.assertIsNotNone(role)
        verbose("Expect client: {}".format(client))
        verbose("Expect token: {}".format(token))
        verbose("Expect token serialized: {}".format(token.serialize()))
        verbose("Expect token2 serialized: {}".format(token2.serialize()))
Пример #8
0
    def create_sample_data(self):
        """ Add some data """
        added_date = utils.get_db_friendly_date_time()

        sample_data = [
            {
                "first": "Aida",
                "last": " Xenon",
                "dob": "1910-11-12",
                "zip": "19116",
                "city": "GAINESVILLE"
            },
            {
                "first": "aida",
                "last": "xeNon ",
                "dob": "1910/11/12",
                "zip": "19116",
                "city": "gainesville"
            },
            {
                "first": "AIDA",
                "last": "XENON ",
                "dob": "1910:11:12  ",
                "zip": "19116",
                "city": "Gainesville"
            },
            {
                "first": "AiDa",
                "last": "XEnON ",
                "dob": "19101112  ",
                "zip": "19116",
                "city": "gainesviLLe"
            },
            {
                "first": "John",
                "last": "Doe",
                "dob": "1900-01-01",
                "zip": "32606",
                "city": "Palatca"
            },
            {
                "first": "JOHN",
                "last": "DOE",
                "dob": "1900-01-01",
                "zip": "32607",
                "city": "Palatca"
            },
        ]

        partner = PartnerEntity.query.filter_by(partner_code='UF').one()

        # Note: first four entries produce the same hashes (aka chunks)
        for person_data in sample_data:
            person_orig = Person(person_data)
            person = Person.get_prepared_person(person_data)
            pers_uuid = utils.get_uuid_bin()
            hashes = get_person_hash(person,
                                     [RULE_CODE_F_L_D_Z, RULE_CODE_L_F_D_Z])

            for rule_id, ahash in hashes.items():
                link = LinkageEntity.create(partner_id=partner.id,
                                            linkage_uuid=pers_uuid,
                                            linkage_hash=ahash,
                                            linkage_addded_at=added_date)
                self.assertIsNotNone(link)

                links_by_hash = LinkageEntity.query.filter_by(
                    linkage_hash=ahash).all()

                verbose("==> Found {} link(s) for [{}] using hash: {}".format(
                    len(links_by_hash), person_orig, hexlify(ahash)))
Пример #9
0
def api_save_patient_hashes():
    """
    For each chunk save a new uuid or return an existing one from the database.

== Example of input json:
{
"data": {
   "1": {
       "1": "b2cdaea3d7c9891b2ed94d1973fe5085183e4bb4bd87b672e066a456ee67bd38"
       },

   "2": {
       "1": "345b192ae4093dcbc5c914bdcb5e8c41e58162475a295c88b1ce594bd3dd78f7",
       "2": "1dcce10470c0ea73a8a6287f69f4f862c5e13faea7c11104fae07dbc8d5ce56e"
       },
   "3": {
       "1": "995b192ae4093dcbc5c914bdcb5e8c41e58162475a295c88b1ce594bd3dd78f7"
       }
   }
}

== To find the rows in the database:
select hex(linkage_uuid), hex(linkage_hash) from linkage order by linkage_id;

    """
    auth_pieces = request.headers.get('Authorization').split(' ')
    log.debug("Authorization_header: {}".format(auth_pieces))
    access_token = auth_pieces[1].strip()
    partner = get_partner_from_token(access_token)
    json = request.get_json(silent=False)

    if not json:
        err = "Invalid json object specified"
        log.error(err)
        return utils.jsonify_error(err)

    result = collections.defaultdict(dict)
    json_data = json['data']

    # patient chunks are received in groups
    for pat_id, pat_chunks in json_data.items():
        # Since there is a chance of duplicate chunks find uniques
        chunks = list(set(chunk for chunk_num, chunk in pat_chunks.items()))
        chunks_cache = LinkageEntity.get_chunks_cache(chunks)
        uuids = LinkageEntity.get_distinct_uuids_for_chunks(chunks_cache)
        log.debug("Found [{}] matching uuids from [{}] chunks of patient "
                  "[{}]".format(len(uuids), len(chunks), pat_id))

        added_date = utils.get_db_friendly_date_time()

        if len(uuids) == 0:
            binary_uuid = utils.get_uuid_bin()
            hex_uuid = utils.hexlify(binary_uuid)
            log.debug("Generate [{}] for pat_id[{}]".format(pat_id, hex_uuid))
        elif len(uuids) == 1:
            hex_uuid = uuids.pop()
            binary_uuid = unhexlify(hex_uuid.encode('utf-8'))
            log.debug("Reuse [{}] for pat_id[{}]".format(pat_id, hex_uuid))
        else:
            log.error("Oops...It looks like we got a collision!"
                      "\n==> chunks: {}\n==> uuids: {}".format(chunks, uuids))
            raise Exception("More than one uuid for chunks attributed to "
                            "[{}] patient [{}]"
                            .format(partner.partner_code, pat_id))

        # update the response json
        result[pat_id] = {"uuid": hex_uuid}

        for i, chunk in enumerate(chunks):
            if chunks_cache.get(chunk):
                log.debug("Skip chunk [{}] - already linked".format(chunk))
                continue

            # link every chunk to the same uuid
            binary_hash = unhexlify(chunk.encode('utf-8'))
            LinkageEntity.create(
                partner_id=partner.id,
                linkage_uuid=binary_uuid,
                linkage_hash=binary_hash,
                linkage_addded_at=added_date)
            log.debug("Created link [{}] for [{}]".format(i, chunk))

    return utils.jsonify_success(result)