This repository was archived by the owner on Oct 2, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -632,3 +632,31 @@ def get_metadata(self, genomespace_url):
632
632
r'\g<2>v1.0/filemetadata' , genomespace_url )
633
633
json_data = self ._api_get_request (url )
634
634
return GSFileMetadata .from_json (json_data )
635
+
636
+ def get_remaining_token_time (self , genomespace_url ):
637
+ """
638
+ Gets the time to live for the gs-token if you have one.
639
+ If you don't have one, will return 0, as the non existent token has
640
+ no time left to live. See:
641
+ http://www.genomespace.org/support/api/restful-access-to-identity-server#get_token_time
642
+
643
+ E.g.
644
+
645
+ client.get_remaining_token_time('https://genomespace.genome.edu.au/')
646
+
647
+ :type genomespace_url: :class:`str`
648
+ :param genomespace_url: GenomeSpace URL.
649
+
650
+ :rtype: :class:`int`
651
+ :return: the time the token has left to live in milliseconds.
652
+ """
653
+ if not self .token :
654
+ return 0
655
+ url_components = urlparse (genomespace_url )
656
+ location = '{uri.scheme}://{uri.netloc}' \
657
+ '/identityServer/usermanagement/utility/token/remainingTime'
658
+ url = location .format (uri = url_components )
659
+ result = requests .get (url , cookies = {"gs-token" : self .token })
660
+ if result .status_code == requests .codes .ok :
661
+ return int (result .text )
662
+ return 0
Original file line number Diff line number Diff line change 9
9
from genomespaceclient import GenomeSpaceClient
10
10
from genomespaceclient import main
11
11
12
+ try :
13
+ from urllib .parse import urlparse
14
+ except ImportError :
15
+ from urlparse import urlparse
12
16
13
17
def get_test_username ():
14
18
return os .environ ["GENOMESPACE_USERNAME" ]
@@ -27,6 +31,16 @@ def get_remote_test_folder():
27
31
return os .environ ["GENOMESPACE_TEST_FOLDER" ]
28
32
29
33
34
+ def get_genomespace_url ():
35
+ """
36
+ :return: the scheme and net location of the remote test folder in the form of
37
+ a url. E.g. https://genomespace.genome.edu.au
38
+ :rtype: :class:`str`
39
+ """
40
+ url_components = urlparse (get_remote_test_folder ())
41
+ return '{uri.scheme}://{uri.netloc}' .format (uri = url_components )
42
+
43
+
30
44
def run_python_script (command , args ):
31
45
32
46
@contextlib .contextmanager
Original file line number Diff line number Diff line change @@ -276,3 +276,18 @@ def test_get_metadata(self):
276
276
self .assertTrue (
277
277
access_control_entries [0 ].sid .name == owner ,
278
278
"Expected sid name to be the owner" )
279
+
280
+ def test_get_token_expiry (self ):
281
+ client = helpers .get_genomespace_client ()
282
+ genomespace_url = helpers .get_genomespace_url ()
283
+ # no gs-token, as have not yet performed any actions
284
+ milliseconds_left = client .get_remaining_token_time (genomespace_url )
285
+ self .assertTrue (
286
+ milliseconds_left == 0 ,
287
+ "Expected client not yet logged in to have no token expiry time" )
288
+ # now force a login and hence the client to get a gs-token
289
+ client .list (helpers .get_remote_test_folder ())
290
+ milliseconds_left = client .get_remaining_token_time (genomespace_url )
291
+ self .assertTrue (
292
+ milliseconds_left > 0 ,
293
+ "Expected a logged in client to have a token expiry time" )
You can’t perform that action at this time.
0 commit comments