(flow: GithubDeviceFlow)
| 600 | } |
| 601 | |
| 602 | private async pollGithubDeviceFlow(flow: GithubDeviceFlow): Promise<void> { |
| 603 | while (!flow.cancelled) { |
| 604 | try { |
| 605 | const tokenResult = await this.pollGithubAccessToken(flow); |
| 606 | if (flow.cancelled) { |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | if (!tokenResult.success) { |
| 611 | this.finishGithubDeviceFlow(flow.flowId, Err(tokenResult.error)); |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | if (tokenResult.data.type === "pending") { |
| 616 | // Continue polling. |
| 617 | } else if (tokenResult.data.type === "slow_down") { |
| 618 | flow.intervalSeconds = tokenResult.data.intervalSeconds; |
| 619 | } else if (tokenResult.data.type === "authorized") { |
| 620 | const loginResult = await this.fetchGithubLogin(tokenResult.data.accessToken); |
| 621 | if (flow.cancelled) { |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | if (!loginResult.success) { |
| 626 | this.finishGithubDeviceFlow(flow.flowId, Err(loginResult.error)); |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | const allowedOwner = this.getAllowedGithubOwner(); |
| 631 | if (!allowedOwner) { |
| 632 | this.finishGithubDeviceFlow( |
| 633 | flow.flowId, |
| 634 | Err("GitHub owner login is no longer configured") |
| 635 | ); |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | if (loginResult.data.toLowerCase() !== allowedOwner.toLowerCase()) { |
| 640 | this.finishGithubDeviceFlow( |
| 641 | flow.flowId, |
| 642 | Err(`GitHub user '${loginResult.data}' is not authorized for this server`) |
| 643 | ); |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | if (flow.cancelled) { |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | const sessionResult = await this.createSessionLocked({ |
| 652 | userAgent: flow.userAgent, |
| 653 | ipAddress: flow.ipAddress, |
| 654 | }); |
| 655 | |
| 656 | if (flow.cancelled) { |
| 657 | // The flow may be canceled while a session write is in progress. |
| 658 | // Defensive cleanup avoids orphan sessions that have no delivered token. |
| 659 | if (sessionResult.success) { |
no test coverage detected