dart-lang / tools

This repository is home to tooling related Dart packages.
BSD 3-Clause "New" or "Revised" License
30 stars 23 forks source link

Listen for a redirect OAuth Windows / MacOS #365

Open Nealsoni00 opened 3 years ago

Nealsoni00 commented 3 years ago

Hey all! I'm trying to implement OAuth2 ID Token Grant Flow on a Windows desktop app. I noticed the OAuth2 plugin supports Windows and MacOS but in the documentation on listening for the redirect using uni_links. However uni_links only supports iOS/Android/Web.

Launch a browser using url_launcher and listen for a redirect using uni_links.

Does anyone have examples of how they can register a windows app to receive the OAuth redirect?

vanceism7 commented 2 years ago

Just figured this out. Its actually ridiculously simple. You just spin up an http server on localhost and listen for the redirect. This technique should probably work for everything but web.

Technique described here: https://stackoverflow.com/questions/68716993/google-microsoft-oauth2-login-flow-flutter-desktop-macos-windows-linux

TLDR: Implement a listen function like this:

static Future<Uri> listenForAuth() async {
    HttpServer server = await HttpServer.bind("localhost", 80);
    final req = await server.first;
    final result = req.uri;
    req.response.statusCode = 200;
    req.response.headers.set("content-type", "text/plain");
    req.response.writeln("Authentication successful! You can close this tab.");
    await req.response.close();
    await server.close();
    return result;
  }
ebarcsa commented 2 years ago

Here is my working solution for now: oauth2.txt Replace these , I use it as:

final file = File("...\\client_credentials.json");
late DesktopOath2 auth;
if (file.existsSync()) {
  auth = DesktopOath2.fromCredentials(file.readAsStringSync(), GoogleAppSecret(), 
                                      GoogleOath2Endpoint(), GDrive.getScopes());
} else {
  auth = DesktopOath2(GoogleAppSecret(), GoogleOath2Endpoint(), GDrive.getScopes());
}

await auth.login();
file.writeAsString(auth.toJsonCredentials());
final drive = DriveApi(auth.authClient);
final files = await drive.files.list(...)

I used this approach as _google_signin does not yet support windows desktop. I might write a ticket to them.

jonathanMNg commented 2 months ago

Just figured this out. Its actually ridiculously simple. You just spin up an http server on localhost and listen for the redirect. This technique should probably work for everything but web.

Technique described here: https://stackoverflow.com/questions/68716993/google-microsoft-oauth2-login-flow-flutter-desktop-macos-windows-linux

TLDR: Implement a listen function like this:

static Future<Uri> listenForAuth() async {
    HttpServer server = await HttpServer.bind("localhost", 80);
    final req = await server.first;
    final result = req.uri;
    req.response.statusCode = 200;
    req.response.headers.set("content-type", "text/plain");
    req.response.writeln("Authentication successful! You can close this tab.");
    await req.response.close();
    await server.close();
    return result;
  }

This answer helped me, but I think it's worth to point out that you need to add your localhost url to Azure in order for it to work: image