(client: OAuth2Client)
| 201 | } |
| 202 | |
| 203 | async function authWithUserCode(client: OAuth2Client): Promise<boolean> { |
| 204 | const redirectUri = 'https://codeassist.google.com/authcode'; |
| 205 | const codeVerifier = await client.generateCodeVerifierAsync(); |
| 206 | const state = crypto.randomBytes(32).toString('hex'); |
| 207 | const authUrl: string = client.generateAuthUrl({ |
| 208 | redirect_uri: redirectUri, |
| 209 | access_type: 'offline', |
| 210 | scope: OAUTH_SCOPE, |
| 211 | code_challenge_method: CodeChallengeMethod.S256, |
| 212 | code_challenge: codeVerifier.codeChallenge, |
| 213 | state, |
| 214 | }); |
| 215 | console.log('Please visit the following URL to authorize the application:'); |
| 216 | console.log(''); |
| 217 | console.log(authUrl); |
| 218 | console.log(''); |
| 219 | |
| 220 | const code = await new Promise<string>((resolve) => { |
| 221 | const rl = readline.createInterface({ |
| 222 | input: process.stdin, |
| 223 | output: process.stdout, |
| 224 | }); |
| 225 | rl.question('Enter the authorization code: ', (code) => { |
| 226 | rl.close(); |
| 227 | resolve(code.trim()); |
| 228 | }); |
| 229 | }); |
| 230 | |
| 231 | if (!code) { |
| 232 | console.error('Authorization code is required.'); |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | try { |
| 237 | const { tokens } = await client.getToken({ |
| 238 | code, |
| 239 | codeVerifier: codeVerifier.codeVerifier, |
| 240 | redirect_uri: redirectUri, |
| 241 | }); |
| 242 | client.setCredentials(tokens); |
| 243 | } catch (_error) { |
| 244 | return false; |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | async function authWithWeb(client: OAuth2Client): Promise<OauthWebLogin> { |
| 250 | const port = await getAvailablePort(); |
no test coverage detected