PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/datatypes/display_applications/util.py

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