Skip to content

Commit 59424aa

Browse files
Fix iOS redirection when scheme is https (#117)
* fix: fix ios redirects * feat: enable ASWebAuthenticationSession.Callback.https support * Revert some style changes * Also allow universal links on macOS * fix: apply some fixes --------- Co-authored-by: Nico Mexis <nico.mexis@kabelmail.de>
1 parent 1d03860 commit 59424aa

5 files changed

Lines changed: 72 additions & 4 deletions

File tree

flutter_web_auth_2/example/macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import Foundation
77

88
import desktop_webview_window
99
import flutter_web_auth_2
10-
import path_provider_foundation
1110
import url_launcher_macos
1211
import window_to_front
1312

1413
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
1514
DesktopWebviewWindowPlugin.register(with: registry.registrar(forPlugin: "DesktopWebviewWindowPlugin"))
1615
FlutterWebAuth2Plugin.register(with: registry.registrar(forPlugin: "FlutterWebAuth2Plugin"))
17-
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
1816
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
1917
WindowToFrontPlugin.register(with: registry.registrar(forPlugin: "WindowToFrontPlugin"))
2018
}

flutter_web_auth_2/ios/Classes/SwiftFlutterWebAuth2Plugin.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,21 @@ public class SwiftFlutterWebAuth2Plugin: NSObject, FlutterPlugin {
6464
if #available(iOS 12, *) {
6565
var _session: ASWebAuthenticationSession? = nil
6666
if #available(iOS 17.4, *) {
67-
_session = ASWebAuthenticationSession(url: url, callback: ASWebAuthenticationSession.Callback.customScheme(callbackURLScheme), completionHandler: completionHandler!)
67+
if (callbackURLScheme == "https") {
68+
guard let host = options["httpsHost"] as? String else {
69+
result(FlutterError.invalidHttpsHostError)
70+
return
71+
}
72+
73+
guard let path = options["httpsPath"] as? String else {
74+
result(FlutterError.invalidHttpsPathError)
75+
return
76+
}
77+
78+
_session = ASWebAuthenticationSession(url: url, callback: ASWebAuthenticationSession.Callback.https(host: host, path: path), completionHandler: completionHandler!)
79+
} else {
80+
_session = ASWebAuthenticationSession(url: url, callback: ASWebAuthenticationSession.Callback.customScheme(callbackURLScheme), completionHandler: completionHandler!)
81+
}
6882
} else {
6983
_session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme, completionHandler: completionHandler!)
7084
}
@@ -151,4 +165,12 @@ fileprivate extension FlutterError {
151165
static var acquireRootViewControllerFailed: FlutterError {
152166
return FlutterError(code: "ACQUIRE_ROOT_VIEW_CONTROLLER_FAILED", message: "Failed to acquire root view controller", details: nil)
153167
}
168+
169+
static var invalidHttpsHostError: FlutterError {
170+
return FlutterError(code: "INVALID_HTTPS_HOST_ERROR", message: "Failed to retrieve host for https scheme", details: nil)
171+
}
172+
173+
static var invalidHttpsPathError: FlutterError {
174+
return FlutterError(code: "INVALID_HTTPS_PATH_ERROR", message: "Failed to retrieve path for https scheme", details: nil)
175+
}
154176
}

flutter_web_auth_2/lib/flutter_web_auth_2.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class FlutterWebAuth2 {
5555
///
5656
/// [callbackUrlScheme] should be a string specifying the scheme of the URL
5757
/// that the page will redirect to upon successful authentication.
58+
/// If it is `https`, you also need to specify
59+
/// [FlutterWebAuth2Options.httpsHost] and [FlutterWebAuth2Options.httpsPath]
60+
/// on Apple devices running iOS >= 17.4 or macOS >= 14.4. This allows for
61+
/// easy integration of Universal links.
5862
///
5963
/// [options] can be used to specify either both general and
6064
/// platform-specific settings.

flutter_web_auth_2/lib/src/options.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class FlutterWebAuth2Options {
5656
String? landingPageHtml,
5757
bool? silentAuth,
5858
bool? useWebview,
59+
this.httpsHost,
60+
this.httpsPath,
5961
this.customTabsPackageOrder,
6062
}) : preferEphemeral = preferEphemeral ?? false,
6163
intentFlags = intentFlags ?? defaultIntentFlags,
@@ -75,6 +77,8 @@ class FlutterWebAuth2Options {
7577
landingPageHtml: json['landingPageHtml'],
7678
silentAuth: json['silentAuth'],
7779
useWebview: json['useWebview'],
80+
httpsHost: json['httpsHost'],
81+
httpsPath: json['httpsPath'],
7882
customTabsPackageOrder: json['customTabsPackageOrder'],
7983
);
8084

@@ -137,6 +141,20 @@ class FlutterWebAuth2Options {
137141
/// described in https://github.com/ThexXTURBOXx/flutter_web_auth_2/issues/25
138142
final bool useWebview;
139143

144+
/// **Only has an effect on iOS and MacOS!**
145+
/// String specifying the **host** of the URL that the page will redirect to
146+
/// upon successful authentication (callback URL).
147+
/// When `callbackUrlScheme` is `https`, this **must** be specified on
148+
/// Apple devices running iOS >= 17.4 or macOS >= 14.4.
149+
final String? httpsHost;
150+
151+
/// **Only has an effect on iOS and MacOS!**
152+
/// String specifying the **path** of the URL that the page will redirect to
153+
/// upon successful authentication (callback URL).
154+
/// When `callbackUrlScheme` is `https`, this **must** be specified on
155+
/// Apple devices running iOS >= 17.4 or macOS >= 14.4.
156+
final String? httpsPath;
157+
140158
/// **Only has an effect on Android!**
141159
/// Sets the Android browser priority for opening custom tabs.
142160
/// Needs to be a list of packages providing a custom tabs
@@ -155,5 +173,7 @@ class FlutterWebAuth2Options {
155173
'silentAuth': silentAuth,
156174
'useWebview': useWebview,
157175
'customTabsPackageOrder': customTabsPackageOrder,
176+
'httpsHost': httpsHost,
177+
'httpsPath': httpsPath,
158178
};
159179
}

flutter_web_auth_2/macos/Classes/FlutterWebAuth2Plugin.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,21 @@ public class FlutterWebAuth2Plugin: NSObject, FlutterPlugin, ASWebAuthentication
4242

4343
var _session: ASWebAuthenticationSession? = nil
4444
if #available(macOS 14.4, *) {
45-
_session = ASWebAuthenticationSession(url: url, callback: ASWebAuthenticationSession.Callback.customScheme(callbackURLScheme), completionHandler: completionHandler)
45+
if (callbackURLScheme == "https") {
46+
guard let host = options["httpsHost"] as? String else {
47+
result(FlutterError.invalidHttpsHostError)
48+
return
49+
}
50+
51+
guard let path = options["httpsPath"] as? String else {
52+
result(FlutterError.invalidHttpsPathError)
53+
return
54+
}
55+
56+
_session = ASWebAuthenticationSession(url: url, callback: ASWebAuthenticationSession.Callback.https(host: host, path: path), completionHandler: completionHandler)
57+
} else {
58+
_session = ASWebAuthenticationSession(url: url, callback: ASWebAuthenticationSession.Callback.customScheme(callbackURLScheme), completionHandler: completionHandler)
59+
}
4660
} else {
4761
_session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme, completionHandler: completionHandler)
4862
}
@@ -68,3 +82,13 @@ public class FlutterWebAuth2Plugin: NSObject, FlutterPlugin, ASWebAuthentication
6882
return NSApplication.shared.windows.first { $0.isKeyWindow } ?? ASPresentationAnchor()
6983
}
7084
}
85+
86+
fileprivate extension FlutterError {
87+
static var invalidHttpsHostError: FlutterError {
88+
return FlutterError(code: "INVALID_HTTPS_HOST_ERROR", message: "Failed to retrieve host for https scheme", details: nil)
89+
}
90+
91+
static var invalidHttpsPathError: FlutterError {
92+
return FlutterError(code: "INVALID_HTTPS_PATH_ERROR", message: "Failed to retrieve path for https scheme", details: nil)
93+
}
94+
}

0 commit comments

Comments
 (0)