Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(powerbi): fix access token expiry #8680

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class Constant:
TITLE = "title"
EMBED_URL = "embedUrl"
ACCESS_TOKEN = "access_token"
ACCESS_TOKEN_EXPIRY = "expires_in"
IS_READ_ONLY = "isReadOnly"
WEB_URL = "webUrl"
ODATA_COUNT = "@odata.count"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import math
from abc import ABC, abstractmethod
from datetime import datetime, timedelta
from time import sleep
from typing import Any, Dict, List, Optional

Expand Down Expand Up @@ -59,6 +60,7 @@ def __init__(
tenant_id: str,
):
self.__access_token: Optional[str] = None
self.__access_token_expiry_time: Optional[datetime] = None
self.__tenant_id = tenant_id
# Test connection by generating access token
logger.info("Trying to connect to {}".format(self._get_authority_url()))
Expand Down Expand Up @@ -128,7 +130,7 @@ def get_authorization_header(self):
return {Constant.Authorization: self.get_access_token()}

def get_access_token(self):
if self.__access_token is not None:
if self.__access_token is not None and not self._is_access_token_expired():
return self.__access_token

logger.info("Generating PowerBi access token")
Expand All @@ -150,11 +152,18 @@ def get_access_token(self):
self.__access_token = "Bearer {}".format(
auth_response.get(Constant.ACCESS_TOKEN)
)
safety_gap = 300
self.__access_token_expiry_time = datetime.now() + timedelta(
seconds=(max(auth_response.get(Constant.ACCESS_TOKEN_EXPIRY, 0) - safety_gap, 0))
)

logger.debug(f"{Constant.PBIAccessToken}={self.__access_token}")

return self.__access_token

def _is_access_token_expired(self) -> bool:
return self.__access_token_expiry_time < datetime.now()

def get_dashboards(self, workspace: Workspace) -> List[Dashboard]:
"""
Get the list of dashboard from PowerBi for the given workspace identifier
Expand Down