Refreshes login credentials, including saving them to the cache
(self)
| 2526 | return self._token_to_credentials(token) |
| 2527 | |
| 2528 | def refresh_credentials(self): |
| 2529 | """Refreshes login credentials, including saving them to the cache""" |
| 2530 | if self.feature_ids: |
| 2531 | register_feature_ids(self.feature_ids) |
| 2532 | # Reload the token from disk, we need the refresh info |
| 2533 | token = self._token_loader.load_token(self._session_name) |
| 2534 | private_key = self._load_private_key(token) |
| 2535 | |
| 2536 | # Check if token has already been refreshed and is still valid |
| 2537 | if ( |
| 2538 | token |
| 2539 | and 'accessToken' in token |
| 2540 | and 'expiresAt' in token['accessToken'] |
| 2541 | ): |
| 2542 | expiry_time = _parse_if_needed(token['accessToken']['expiresAt']) |
| 2543 | remaining_time = total_seconds(expiry_time - self._time_fetcher()) |
| 2544 | if remaining_time > self._REFRESH_THRESHOLD: |
| 2545 | return self._token_to_credentials(token) |
| 2546 | |
| 2547 | config = botocore.config.Config( |
| 2548 | signature_version=botocore.UNSIGNED, |
| 2549 | ) |
| 2550 | client = self._client_creator( |
| 2551 | 'signin', |
| 2552 | config=config, |
| 2553 | ) |
| 2554 | |
| 2555 | client.meta.events.register( |
| 2556 | 'before-call.signin.CreateOAuth2Token', |
| 2557 | build_add_dpop_header_handler(private_key), |
| 2558 | ) |
| 2559 | |
| 2560 | try: |
| 2561 | response = client.create_o_auth2_token( |
| 2562 | tokenInput={ |
| 2563 | 'clientId': token['clientId'], |
| 2564 | 'refreshToken': token['refreshToken'], |
| 2565 | 'grantType': 'refresh_token', |
| 2566 | }, |
| 2567 | ) |
| 2568 | except client.exceptions.AccessDeniedException as e: |
| 2569 | error_type = e.response.get('error', '') |
| 2570 | if error_type == 'TOKEN_EXPIRED': |
| 2571 | raise LoginRefreshTokenExpired() |
| 2572 | elif error_type == 'USER_CREDENTIALS_CHANGED': |
| 2573 | raise LoginRefreshPasswordChanged() |
| 2574 | elif error_type == 'INSUFFICIENT_PERMISSIONS': |
| 2575 | raise LoginInsufficientPermissions() |
| 2576 | raise LoginError from e |
| 2577 | |
| 2578 | if response is None or 'tokenOutput' not in response: |
| 2579 | raise LoginTokenLoadError( |
| 2580 | error_msg='Failed to refresh an access token.' |
| 2581 | ) |
| 2582 | |
| 2583 | output = response.get('tokenOutput') |
| 2584 | |
| 2585 | expires_timestamp = self._time_fetcher().astimezone( |