This commit is contained in:
zezha-msft 2018-06-20 14:03:59 -07:00
Родитель 6a7a25259a
Коммит e67d81ad19
1 изменённых файлов: 16 добавлений и 11 удалений

Просмотреть файл

@ -12,6 +12,7 @@ from azure.storage.blob import (
) )
import tests.settings_real as settings import tests.settings_real as settings
import time import time
import datetime
import threading import threading
import adal import adal
@ -22,16 +23,16 @@ class AutoUpdatedTokenCredential(TokenCredential):
It shows one way of making sure the credential does not become expired. It shows one way of making sure the credential does not become expired.
""" """
def __init__(self): def __init__(self):
# get the first token super(AutoUpdatedTokenCredential, self).__init__()
first_token, first_interval = self.get_token_func()
super(AutoUpdatedTokenCredential, self).__init__(first_token)
# a timer is used to trigger a callback to update the token # a timer is used to trigger a callback to update the token
# the timer needs to be protected, as later on it is possible that one thread is setting a new timer and # the timer needs to be protected, as later on it is possible that one thread is setting a new timer and
# another thread is trying to cancel the timer # another thread is trying to cancel the timer
self.lock = threading.Lock() self.lock = threading.Lock()
self.timer = threading.Timer(first_interval, self.timer_callback) self.timer_stopped = False
self.timer.start()
# get the initial token and schedule the timer for the very first time
self.refresh_token()
# support context manager # support context manager
def __enter__(self): def __enter__(self):
@ -41,9 +42,7 @@ class AutoUpdatedTokenCredential(TokenCredential):
def __exit__(self, *args): def __exit__(self, *args):
self.stop_refreshing_token() self.stop_refreshing_token()
def timer_callback(self): def refresh_token(self):
print("TOKEN UPDATER WAS TRIGGERED")
# call the get token function to get a new token, as well as the time to wait before calling it again # call the get token function to get a new token, as well as the time to wait before calling it again
token, next_interval = self.get_token_func() token, next_interval = self.get_token_func()
@ -51,7 +50,8 @@ class AutoUpdatedTokenCredential(TokenCredential):
self.token = token self.token = token
with self.lock: with self.lock:
self.timer = threading.Timer(next_interval, self.timer_callback) if self.timer_stopped is False:
self.timer = threading.Timer(next_interval, self.refresh_token)
self.timer.start() self.timer.start()
def stop_refreshing_token(self): def stop_refreshing_token(self):
@ -59,6 +59,7 @@ class AutoUpdatedTokenCredential(TokenCredential):
The timer needs to be canceled if the application is terminating, if not the timer will keep going. The timer needs to be canceled if the application is terminating, if not the timer will keep going.
""" """
with self.lock: with self.lock:
self.timer_stopped = True
self.timer.cancel() self.timer.cancel()
@staticmethod @staticmethod
@ -67,6 +68,8 @@ class AutoUpdatedTokenCredential(TokenCredential):
This function makes a call to AAD to fetch an OAuth token This function makes a call to AAD to fetch an OAuth token
:return: the OAuth token and the interval to wait before refreshing it :return: the OAuth token and the interval to wait before refreshing it
""" """
print("{}: token updater was triggered".format(datetime.datetime.now()))
# in this example, the OAuth token is obtained using the ADAL library # in this example, the OAuth token is obtained using the ADAL library
# however, the user can use any preferred method # however, the user can use any preferred method
context = adal.AuthenticationContext( context = adal.AuthenticationContext(
@ -97,7 +100,9 @@ def test_token_credential_with_timer():
for i in range(10): for i in range(10):
result = service.exists("test") result = service.exists("test")
if result is None: if result is None:
print("something is wrong") print("{}: something is wrong".format(datetime.datetime.now()))
else:
print("{}: all is well".format(datetime.datetime.now()))
time.sleep(600) time.sleep(600)