File size: 6,233 Bytes
26abd99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import assert from 'assert';
import * as fs from 'fs';
import * as http from 'http';
import * as path from 'path';
import { OAuth2Client } from 'google-auth-library';
import vscode from 'vscode';
import { CONFIG } from '../../colab-config';
import { log } from '../../common/logging';
import { LoopbackHandler, LoopbackServer } from '../../common/loopback-server';
import { CodeManager } from '../code-manager';
import {
  DEFAULT_AUTH_URL_OPTS,
  OAuth2Flow,
  OAuth2TriggerOptions,
  FlowResult,
} from './flows';

/**
 * An OAuth2 flow that uses a local server to handle the redirect URI.
 *
 * Since the flow when triggered spins up a local server to handle the redirect,
 * the {@link FlowResult} returns a `dispose` method. This is needed since the
 * endpoint should continue to serve assets like the favicon after the initial
 * trigger. Only when the login flow is complete are we "done" with the server.
 *
 * Since it's possible we'd want to dispose this class while there are in-flight
 * triggered flows, the {@link LocalServerFlow} is disposable and will clean-up
 * any owned servers from outstanding flows.
 */
export class LocalServerFlow implements OAuth2Flow, vscode.Disposable {
  private readonly codeManager = new CodeManager();
  private readonly handler: Handler;
  private readonly activeServers = new Set<vscode.Disposable>();

  constructor(
    private readonly vs: typeof vscode,
    private readonly serveRoot: string,
    private readonly oAuth2Client: OAuth2Client,
    extensionUri: string,
  ) {
    this.handler = new Handler(
      vs,
      this.serveRoot,
      this.codeManager,
      extensionUri,
    );
  }

  dispose() {
    this.codeManager.dispose();
    for (const disposable of this.activeServers) {
      disposable.dispose();
    }
    this.activeServers.clear();
  }

  /**
   * Trigger the OAuth2 flow by opening a local server to listen for the
   * redirect URI. Callers are expected to dispose the returned disposable when
   * the full flow is complete. It is their responsibility since this flow is
   * expected to serve assets until fully completed (for e.g., the favicon).
   */
  async trigger(options: OAuth2TriggerOptions): Promise<FlowResult> {
    const server = new LoopbackServer(this.handler);
    this.activeServers.add(server);
    try {
      const code = this.codeManager.waitForCode(options.nonce, options.cancel);
      options.cancel.onCancellationRequested(server.dispose.bind(server));
      const port = await server.start();
      const address = `http://127.0.0.1:${port.toString()}`;
      const authUrl = this.oAuth2Client.generateAuthUrl({
        ...DEFAULT_AUTH_URL_OPTS,
        redirect_uri: address,
        state: `nonce=${options.nonce}`,
        scope: options.scopes,
        code_challenge: options.pkceChallenge,
      });

      await this.vs.env.openExternal(this.vs.Uri.parse(authUrl));
      // TODO: We can't dispose of the server immediately since the favicon is
      // loaded asynchronously. Following a successful flow result (here), we
      // should TTL disposing of the server. It'll get cleaned up when the flow
      // is disposed, so not crucial to add now.
      return {
        code: await code,
        redirectUri: address,
      };
    } catch (err: unknown) {
      server.dispose();
      this.activeServers.delete(server);
      throw err;
    }
  }
}

class Handler implements LoopbackHandler {
  constructor(
    private readonly vs: typeof vscode,
    private readonly serveRoot: string,
    private readonly codeProvider: CodeManager,
    private readonly extensionUri: string,
  ) {}

  handleRequest(req: http.IncomingMessage, res: http.ServerResponse): void {
    // URL and Host are only missing on malformed requests.
    assert(req.url);
    assert(req.headers.host);
    const url = new URL(req.url, `http://${req.headers.host}`);
    if (req.method !== 'GET') {
      res.writeHead(405, { Allow: 'GET' });
      res.end('Method Not Allowed');
      return;
    }
    switch (url.pathname) {
      case '/': {
        const state = url.searchParams.get('state');
        if (!state) {
          throw new Error('Missing state in redirect URL');
        }
        const parsedState = new URLSearchParams(state);
        const nonce = parsedState.get('nonce');
        const code = url.searchParams.get('code');
        if (!nonce || !code) {
          throw new Error('Missing nonce or code in redirect URI');
        }
        this.codeProvider.resolveCode(nonce, code);

        void this.redirectSuccessfulAuth(res).catch((err: unknown) => {
          log.error('Unable to redirect the successful auth request', err);
        });
        break;
      }
      case '/favicon.ico': {
        const assetPath = url.pathname.substring(1);
        sendFile(res, path.join(this.serveRoot, assetPath));
        break;
      }
      default: {
        log.warn('Received unhandled request', req);
        res.writeHead(404);
        res.end('Not Found');
        break;
      }
    }
  }

  async redirectSuccessfulAuth(res: http.ServerResponse): Promise<void> {
    const authSuccessUri = await this.vs.env.asExternalUri(
      this.vs.Uri.parse(`${this.extensionUri}/auth-success`),
    );
    const successState = encodeURIComponent(authSuccessUri.toString());
    const redirectUri = `${CONFIG.ColabApiDomain}/vscode/auth-success?state=${successState}`;
    // Since we need to handle the request asynchronously, it's technically
    // possible that the response has already been closed by time we get here.
    // This is not foreseen to ever happen, under normal network conditions. In
    // that case, just no-op.
    if (res.headersSent) {
      return;
    }
    res.writeHead(302, { Location: redirectUri });
    res.end();
  }
}

function sendFile(res: http.ServerResponse, filepath: string): void {
  fs.readFile(filepath, (err, body) => {
    if (err) {
      log.error(`Unable to read file: ${filepath}`, err);
      res.writeHead(500);
      res.end('Internal Server Error');
    } else {
      res.setHeader('content-length', body.length);
      res.writeHead(200);
      res.end(body);
    }
  });
}