A token backend based on environmental variable
| 425 | |
| 426 | |
| 427 | class EnvTokenBackend(BaseTokenBackend): |
| 428 | """ A token backend based on environmental variable """ |
| 429 | |
| 430 | def __init__(self, token_env_name=None): |
| 431 | """ |
| 432 | Init Backend |
| 433 | :param str token_env_name: the name of the environmental variable that will hold the token |
| 434 | """ |
| 435 | super().__init__() |
| 436 | |
| 437 | self.token_env_name = token_env_name if token_env_name else "O365TOKEN" |
| 438 | |
| 439 | def __repr__(self): |
| 440 | return str(self.token_env_name) |
| 441 | |
| 442 | def load_token(self) -> bool: |
| 443 | """ |
| 444 | Retrieves the token from the environmental variable |
| 445 | :return bool: Success / Failure |
| 446 | """ |
| 447 | if self.token_env_name in os.environ: |
| 448 | self._cache = self.deserialize(os.environ.get(self.token_env_name)) |
| 449 | return True |
| 450 | return False |
| 451 | |
| 452 | def save_token(self, force=False) -> bool: |
| 453 | """ |
| 454 | Saves the token dict in the specified environmental variable |
| 455 | :param bool force: Force save even when state has not changed |
| 456 | :return bool: Success / Failure |
| 457 | """ |
| 458 | if not self._cache: |
| 459 | return False |
| 460 | |
| 461 | if force is False and self._has_state_changed is False: |
| 462 | return True |
| 463 | |
| 464 | os.environ[self.token_env_name] = self.serialize() |
| 465 | |
| 466 | return True |
| 467 | |
| 468 | def delete_token(self) -> bool: |
| 469 | """ |
| 470 | Deletes the token environmental variable |
| 471 | :return bool: Success / Failure |
| 472 | """ |
| 473 | if self.token_env_name in os.environ: |
| 474 | del os.environ[self.token_env_name] |
| 475 | return True |
| 476 | return False |
| 477 | |
| 478 | def check_token(self) -> bool: |
| 479 | """ |
| 480 | Checks if the token exists in the environmental variables |
| 481 | :return bool: True if exists, False otherwise |
| 482 | """ |
| 483 | return self.token_env_name in os.environ |
| 484 |
no outgoing calls