如何关闭ASWebAuthenticationSession并重定向到外部网站而不是应用通用链接。

huangapple go评论68阅读模式
英文:

How to close ASWebAuthenticationSession with redirect to external website not to app universal link

问题

我有一个授权流程,最后重定向到我们的API端点,而不是应用的通用链接,比如 https://api.example.com/redirect 而不是 app://redirect

目前我能够使用 WKWebView 来检测重定向是否完成,通过比较URL,如果匹配则关闭WebView。

问题在于,通过这种方式,我无法在此流程中使用谷歌登录(WebView被拒绝)。

我尝试使用 ASWebAuthenticationSession,但在重定向后,我无法检测到该重定向是否完成(因为它命中API而不是应用程序),以便自动关闭AuthenticationSession视图。

在这种情况下是否可能实现,或者唯一关闭AS的方法是重定向到应用的通用链接 app:// 而不是 https://

非常感谢任何帮助。

英文:

I have an authorisation flow that at the end redirects to our API endpoint not to the app universal link like https://api.example.com/redirect not the app://redirect

For now I am able to use WKWebView to detect that redirect was done by comparing urls and if match to close WebView.

The problem is that in this approach I cannot use google login (WebView is rejected) during this flow.

I tried to use ASWebAuthenticationSession but after redirect I am not able to detect that this redirect was done (as it hits API not the app) to close AuthenticationSession view automatically.

Is it possible at all in such case or the only way to close AS is to redirect to app universal link app:// not to the https://?

Any help really appreciated

答案1

得分: 1

你需要使用 ASWebAuthenticationSession 窗口与 AppAuth 模式,如RFC8252中所述。这是一种系统浏览器的形式,因此不会被供应商(如Google登录)阻止。

这种登录方式可以使用自定义URI方案,例如com.mycompany.myapp:/callback,也可以使用需要配置iOS通用链接的HTTPS回调URI。然后,当登录完成、取消或失败时,您会收到通知。

我的一个示例应用程序通过此代码使用通用链接进行登录,该代码适应了AppAuth类以更符合[Swift async await风格。如果您是AppAuth库的新手,还可以查看我的入门博客文章

func login(viewController: UIViewController) async throws {

    try await self.authenticator.getMetadata()

    try await MainActor.run {
        try self.authenticator.startLoginRedirect(viewController: viewController)
    }

    let response = try await self.authenticator.handleLoginResponse()

    try await self.authenticator.finishLogin(authResponse: response)
}
英文:

You need to use the ASWebAuthenticationSession window with the AppAuth pattern, as described in RFC8252. This is a form of the system browser so will not be blocked by providers such as Sign in with Google.

This form of login can be used with either custom URI schemes, such as com.mycompany.myapp:/callback or with HTTPS callback URIs that require iOS universal links to be configured. You are then notified when login conpletes, or is cancelled, or fails.

A sample app of mine does logins with universal links via this code, which adapts AppAuth classes to more of a [Swift async await style. If you are new to AppAuth libraries, see also my introductory blog post.

func login(viewController: UIViewController) async throws {

    try await self.authenticator.getMetadata()

    try await MainActor.run {
        try self.authenticator.startLoginRedirect(viewController: viewController)
    }

    let response = try await self.authenticator.handleLoginResponse()

    try await self.authenticator.finishLogin(authResponse: response)
}

huangapple
  • 本文由 发表于 2023年2月10日 16:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定