Repository navigation
fix(flutter): sign in with OAuth2 on Windows and Linux through a loopback token callback - #2093
Merged
Merged
Conversation
…back token callback
flutter_web_auth_2 only accepts an http://localhost:{port} callback on Windows and Linux, so the appwrite-callback scheme failed outright. A localhost success URL got past that, but the server only adds key and secret to its own default success page, so the session could never reach the app. The token flow returns userId and secret to any allowed success URL, so desktop now opens the token endpoint on a loopback callback (a free port when no success URL is given) and exchanges the result with createSession.
Saving the mobile session also waited on init() without waiting for an init already in flight, and did not await the cookie write.
🟢 Tier S · Ready to mergeThis change adds a loopback callback and token-to-session exchange for Flutter OAuth sign-in on Windows and Linux, while retaining the existing callback flow on mobile platforms. It also makes session initialization and cookie persistence awaitable, and registers tests for the new platform-specific behavior.
📂 Walkthrough · 3
Reviewed |
Closed
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
OAuth2 sign-in from the Flutter SDK could not work on Windows or Linux.
successURL,webAuthpassedappwrite-callback-<projectId>as the callback. On Windows and Linux,flutter_web_auth_2(5.1,useWebview: false) runs a local HTTP server and rejects anything that is nothttp://localhost:{port}(Callback url scheme must start with http://localhost:{port}).successURL, the browser did come back, butcreateOAuth2Sessiononly addskeyandsecretto the redirect when it lands on the server's own default success page (app/controllers/api/account.php, the$state['success']['host'] === $consoleHostname && path === $oauthDefaultSuccessbranch). Any other success URL gets cookies set in the browser only, so the SDK threwInvalid OAuth2 Response. Key and Secret not available.The token flow has no such restriction:
createOAuth2Tokenredirects to any allowed success URL withuserIdandsecret, andcreateSession(userId, secret)turns that into a session. No server change is needed.On Windows and Linux,
webAuthnow:successURL as the loopback callback, or binds a free loopback port and useshttp://localhost:<port>/when none is given;successto that callback, andfailuretoo unless one was given, so errors come back to the app instead of leaving the listener waiting for its timeout;createOAuth2Session, opens/account/tokens/oauth2/{provider}instead and exchanges the returneduserIdandsecretwithPOST /account/sessions/token, which stores the session cookie like any other sign-in.Android, iOS and macOS keep the
appwrite-callback-<projectId>flow. While there, saving that session now waits for aninit()that is already in flight (it used to callinit(), which returns at once in that case, leaving_cookieJarunset) and awaits the cookie write.The project still needs a Web platform for
localhost(any port matches) so the server accepts the loopback success URL.Test Plan
New
test/src/client_io_test.dartin the generated Flutter SDK (run byflutter testin validation). It mocks theflutter_web_auth_2andpath_providerchannels, points the client at a localHttpServer, and overridesdefaultTargetPlatform:http://localhost:<port>/, the URL opened is the token endpoint withsuccess/failureset andscopes[]kept, the client posts{"userId","secret"}to/v1/account/sessions/token, and the session cookie is stored.appwrite-callback-project, URL unchanged, the session cookie fromkey/secretis stored.Red/green: generated with the
mainclient_io.dart.twig, 5 of 7 fail (both loopback sign-in cases and both success/failure cases on each desktop platform, plus Android withLateInitializationError: Field '_cookieJar' has not been initialized). With this branch all 7 pass, and the full SDK suite passes (+523 ~1) four runs in a row.Also run:
dart format --output=none --set-exit-if-changedon the changed files,composer lint-twig,composer refactor:check,composer lint,max-line-length.sh . 1200 "*.twig", and the Generation suite.Related PRs and Issues
webAuthmethods are detected from the spec; it does not touch the Flutter runtime and does not overlap.templates/flutter/base/requests/oauth.twig(optional credentials in the OAuth URL); this PR leaves that template alone.Closes #2094