File size: 890 Bytes
4c83ab6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import msal

from app.config import Settings


class GraphAuthProvider:
    _SCOPE = ["https://graph.microsoft.com/.default"]

    def __init__(self, settings: Settings) -> None:
        self._app = msal.ConfidentialClientApplication(
            client_id=settings.azure_client_id,
            authority=f"https://login.microsoftonline.com/{settings.azure_tenant_id}",
            client_credential=settings.azure_client_secret.get_secret_value(),
        )

    def get_access_token(self) -> str:
        result = self._app.acquire_token_silent(self._SCOPE, account=None)
        if not result:
            result = self._app.acquire_token_for_client(scopes=self._SCOPE)
        if "access_token" not in result:
            raise RuntimeError(
                f"MSAL token error: {result.get('error_description', result.get('error'))}"
            )
        return result["access_token"]