|
| 1 | +from . import oauth2 |
| 2 | +from .exceptions import MsalServiceError |
| 3 | + |
| 4 | + |
| 5 | +class BaseRequest(object): |
| 6 | + TOKEN_ENDPOINT_PATH = 'oauth2/v2.0/token' |
| 7 | + |
| 8 | + def __init__( |
| 9 | + self, authority=None, token_cache=None, scope=None, policy="", |
| 10 | + client_id=None, client_credential=None, authenticator=None, |
| 11 | + support_adfs=False, restrict_to_single_user=False): |
| 12 | + if not scope: |
| 13 | + raise ValueError("scope cannot be empty") |
| 14 | + self.__dict__.update(locals()) |
| 15 | + |
| 16 | + def run(self): |
| 17 | + # TODO Some cache stuff here |
| 18 | + raw = self.get_token() |
| 19 | + if 'error' in raw: |
| 20 | + raise MsalServiceError(**raw) |
| 21 | + # TODO: Deal with refresh_token |
| 22 | + return { # i.e. the AuthenticationResult |
| 23 | + "token": raw.get('access_token'), |
| 24 | + "expires_on": raw.get('expires_in'), # TODO: Change into EPOCH |
| 25 | + "tenant_id": None, # TODO |
| 26 | + "user": None, # TODO |
| 27 | + "id_token": None, # TODO |
| 28 | + "scope": set([]), # TODO |
| 29 | + } |
| 30 | + |
| 31 | + def get_token(self): |
| 32 | + raise NotImplemented("Use proper sub-class instead") |
| 33 | + |
| 34 | + |
| 35 | +class ClientCredentialRequest(BaseRequest): |
| 36 | + def get_token(self): |
| 37 | + return oauth2.ClientCredentialGrant( |
| 38 | + self.client_id, |
| 39 | + token_endpoint="%s%s?policy=%s" % ( |
| 40 | + self.authority, self.TOKEN_ENDPOINT_PATH, self.policy), |
| 41 | + ).get_token(scope=self.scope, client_secret=self.client_credential) |
| 42 | + |
0 commit comments