[2] | 1 | import pkg_resources |
---|
| 2 | pkg_resources.require( "pycrypto" ) |
---|
| 3 | from Crypto.Cipher import Blowfish |
---|
| 4 | |
---|
| 5 | def encode_dataset_user( trans, dataset, user ): |
---|
| 6 | #encode dataset id as usual |
---|
| 7 | #encode user id using the dataset create time as the key |
---|
| 8 | dataset_hash = trans.security.encode_id( dataset.id ) |
---|
| 9 | if user is None: |
---|
| 10 | user_hash = 'None' |
---|
| 11 | else: |
---|
| 12 | user_hash = str( user.id ) |
---|
| 13 | # Pad to a multiple of 8 with leading "!" |
---|
| 14 | user_hash = ( "!" * ( 8 - len( user_hash ) % 8 ) ) + user_hash |
---|
| 15 | cipher = Blowfish.new( str( dataset.create_time ) ) |
---|
| 16 | user_hash = cipher.encrypt( user_hash ).encode( 'hex' ) |
---|
| 17 | return dataset_hash, user_hash |
---|
| 18 | |
---|
| 19 | def decode_dataset_user( trans, dataset_hash, user_hash ): |
---|
| 20 | #decode dataset id as usual |
---|
| 21 | #decode user id using the dataset create time as the key |
---|
| 22 | dataset_id = trans.security.decode_id( dataset_hash ) |
---|
| 23 | dataset = trans.sa_session.query( trans.app.model.HistoryDatasetAssociation ).get( dataset_id ) |
---|
| 24 | assert dataset, "Bad Dataset id provided to decode_dataset_user" |
---|
| 25 | if user_hash in [ None, 'None' ]: |
---|
| 26 | user = None |
---|
| 27 | else: |
---|
| 28 | cipher = Blowfish.new( str( dataset.create_time ) ) |
---|
| 29 | user_id = cipher.decrypt( user_hash.decode( 'hex' ) ).lstrip( "!" ) |
---|
| 30 | user = trans.sa_session.query( trans.app.model.User ).get( int( user_id ) ) |
---|
| 31 | assert user, "A Bad user id was passed to decode_dataset_user" |
---|
| 32 | return dataset, user |
---|