Skip to content

Commit

Permalink
fix token caching mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
devksingh4 committed Oct 19, 2023
1 parent 1babf65 commit 72f521f
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions code/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ class GraphAPI:
clientId: str
clientSecret: str
token: Token
activationTime: float

def __init__(self, client, secret):
self.clientId = client
self.clientSecret = secret
self.token = Token(expires_in=0, access_token='')
self.activationTime = time.time()
async def createNewToken(self):
self.token = Token(expires_in=-1, access_token='')

async def createNewToken(self, force_new=False):
try:
if force_new:
raise Exception("Forcing new access token.")
response = table.get_item(
Key={
'key': 'access_token'
}
)
parsed = response['Item']['value']
self.activationTime = time.time()
self.token.access_token = parsed['access_token']
self.token.expires_in = response['Item']['TimeToLive'] - int(time.time())
print("Got AAD token from dynamo cache.")
Expand All @@ -46,7 +46,6 @@ async def createNewToken(self):
async with aiohttp.ClientSession() as session:
async with session.post(TOKEN_URL, data=data) as resp:
parsed = await resp.json()
self.activationTime = time.time()
self.token.access_token = parsed['access_token']
self.token.expires_in = parsed['expires_in']
table.put_item(
Expand All @@ -60,9 +59,9 @@ async def createNewToken(self):

async def isPaidMember(self, netID: str) -> bool:
netID = netID.lower()
timeDiff = time.time() - self.activationTime
if self.token.expires_in <= 0 or self.token.access_token == '' or timeDiff > self.token.expires_in:
await self.createNewToken()
await self.createNewToken()
if self.token.expires_in <= 0:
await self.createNewToken(force_new=True)
headers = {
'Authorization': f"Bearer {self.token.access_token}",
'Content-Type': 'application/json',
Expand Down

0 comments on commit 72f521f

Please sign in to comment.