0

I am creating a java desktop app that needs access to Google APIs.

I authorize the application like this:

Details credentialDetails=...;
List<String> scopes=...;
GoogleClientSecrets clientSecrets=new GoogleClientSecrets();
clientSecrets.setInstalled(credentialDetails);
LocalServerReceiver receiver=new LocalServerReceiver.Builder().setPort(8888).build();
NetHttpTransport httpTransport=GoogleNetHttpTransport.newTrustedTransport();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JacksonFactory.getDefaultInstance(), clientSecrets, scopes)
    .setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver)
    .authorize("user");

authorize() then waits until the authorization finishes using LocalServerReceiver#waitForCode that uses Condition#awaitUninterruptibly.

The problem is that the user could e.g. close the browser window and ignore the authorization and I need to cancel it (from another thread).

I tried using receiver.stop(); but while stop() is executed, the Condition is not signal()ed and the thread doing the authorization stays stuck in Condition#awaitUninterruptibly.

Normal interruption also doesn't work as the LocalServerReceiver#waitForCode uses Condition#awaitUninterruptibly.

How can I cancel the OAuth2 authorization from another thread?

I am using com.google.oauth-client:google-oauth-client-jetty:1.22.0.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • 1
    Past answer that doesn't address how to cancel, but explains why it behaves like this - https://stackoverflow.com/a/38475819/775715 – Joakim Erdfelt Jun 30 '21 at 12:39
  • I am already calling `authorize` asynchronously but the user might never authorize the application. – dan1st Jun 30 '21 at 12:42
  • Perhaps upgrade? The newest versions of `google-oauth-client-jetty` (version 1.31.5) uses a `Semaphore` (instead of a `Condition`) which is specifically released on `LocalServerReceiver.stop()` now. – Joakim Erdfelt Jun 30 '21 at 12:50
  • I originally tried using that but it caused other problems. But I'll try again. Thank you. – dan1st Jun 30 '21 at 12:51

1 Answers1

1

With version 1.22.0 you cannot use LocalServerReceiver#stop() to cancel the pending authorize().

This is a bug, and was opened as an issue at https://github.com/googleapis/google-oauth-java-client/issues/127

Which was addressed in version 1.23.0

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136