Is there any way to achieve 2FA with Network Extension? I'm getting a challenge from the server on initial authentication, after which I need to show a screen to get the OTP from user. But how to keep the tunnel in waiting/deferred state and also update the state to UI layer where I can ask the user for OTP?
Thanks in advance!
But, is there any way to show UI before returning from
startTunnel(options:)
?
There is no way to show UI directly from your tunnel provider. I mentioned that up front.
The question of “before returning” is a subtle one. Prior to the advent of Swift concurrency the answer is clearly “No.” That’s because interacting with the user is an async process and you can’t block inside startTunnel(options:completionHandler:)
waiting for that process to complete.
Having said that, it’s not normally a problem because returning from startTunnel(options:completionHandler:)
is pretty much irrelevant. The system doesn’t consider the start operation done until you call the supplied completion handling.
If you take advantage of Swift concurrency then this whole completion handler stuff goes away. In that model you’re allowed to wait for async functions inside startTunnel(options:)
, and interacting with the user is one such operation.
Coming back to the user interaction issue, the standard approach here is for your tunnel provider to post a local notification requesting that the user run your container app. That app can present UI, including the two-factor authentication UI. On receiving that notification the app:
-
Checks in with the provider to see what authentication requests are pending.
-
Displays those to the user.
-
Sends the results back to the provider.
To run the IPC with the provider, use the app messaging system. In the app, call the sendProviderMessage(_:responseHandler:)
method to send messages to the provider. In the provider, override the handleAppMessage(_:completionHandler:)
method to learn about these messages from the app.
To notify the user, use the User Notifications framework.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"