Logs in by prompting the user to enter the auth code, intended for when the user is unable to open a web browser on the same device where the CLI is running.
| 264 | |
| 265 | |
| 266 | class CrossDeviceLoginTokenFetcher(BaseLoginTokenFetcher): |
| 267 | """ |
| 268 | Logs in by prompting the user to enter the auth code, |
| 269 | intended for when the user is unable to open a web browser |
| 270 | on the same device where the CLI is running. |
| 271 | """ |
| 272 | |
| 273 | def fetch_token(self): |
| 274 | register_feature_id('LOGIN_CROSS_DEVICE') |
| 275 | redirect_uri = f'{self._base_endpoint}/v1/sessions/confirmation' |
| 276 | |
| 277 | authorization_uri = self._get_authorization_uri( |
| 278 | client_id=CLIENT_ID[LoginType.CROSS_DEVICE], |
| 279 | expected_state=self._expected_state, |
| 280 | code_challenge=self._code_challenge, |
| 281 | redirect_uri=redirect_uri, |
| 282 | ) |
| 283 | |
| 284 | self._on_pending_authorization( |
| 285 | **self._get_browser_handler_args(authorization_uri) |
| 286 | ) |
| 287 | |
| 288 | verification_code = self._prompt( |
| 289 | '\nEnter the authorization code displayed in your browser' |
| 290 | ) |
| 291 | |
| 292 | auth_code, state = self.parse_verification_code(verification_code) |
| 293 | |
| 294 | if auth_code is None: |
| 295 | raise LoginAuthorizationCodeError( |
| 296 | error_msg='Failed to retrieve an authorization code.' |
| 297 | ) |
| 298 | |
| 299 | if state != str(self._expected_state): |
| 300 | raise LoginAuthorizationCodeError( |
| 301 | error_msg=f'State parameter {state} does not match expected value {self._expected_state}.' |
| 302 | ) |
| 303 | |
| 304 | return self._exchange_auth_code_for_access_token( |
| 305 | client_id=CLIENT_ID[LoginType.CROSS_DEVICE], |
| 306 | auth_code=auth_code, |
| 307 | redirect_uri=redirect_uri, |
| 308 | ) |
| 309 | |
| 310 | @staticmethod |
| 311 | def parse_verification_code(verification_code): |
| 312 | """ |
| 313 | Parse the verification code that the user pastes from the browser, |
| 314 | which is expected to be base64-encoded 'state={state}&auth_code={code}' |
| 315 | """ |
| 316 | try: |
| 317 | query_string = base64.b64decode(verification_code).decode('utf-8') |
| 318 | except (UnicodeDecodeError, binascii.Error): |
| 319 | raise ValueError('Failed to decode the verification code.') |
| 320 | |
| 321 | params_dict = dict(parse_qsl(query_string)) |
| 322 | |
| 323 | if 'state' not in params_dict or 'code' not in params_dict: |
no outgoing calls