我使用AsWebAuthenticationsession从另一个应用程序到我的应用程序进行身份验证。我打开AsWebAuthenticationsession,它重定向我的应用程序的通用链接。问题是当它重定向我的应用程序通用链接时,它要求打开App。当它重定向时,我想关闭会话。但是AsWebAuthenticationsession只使用自定义URL方案。如何安全地处理它(因为自定义的URL方案不安全:RFC8252 7.1)
发布于 2022-02-18 22:39:06
我可以确认,在iOS 14或更高版本时,还没有在早期版本上进行测试。
初始化ASWebAuthenticationSession时,可以传入callbackURLScheme: "https"。
当身份验证提供者重定向到您的通用链接时,应用程序委托的application(_:continue:restorationHandler:)将使用正确的重定向url触发,但是ASWebAuthenticationSession的完成处理程序不会触发,因此身份验证对话框将保留在屏幕上。
您需要手动保存对ASWebAuthenticationSession和cancel()的引用,从而将其排除在外。
发布于 2022-09-06 02:26:30
您可以尝试使用此方法。
添加单例类以处理此回调
SceneDelegate.swift
@available(iOS 13.0, *)
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
// handle here
OAuthManager.instance.callBackUserActivity(userActivity: userActivity)
}OAuthManager.swift
import Foundation
import AuthenticationServices
protocol UserActivityListener {
func callBackUserActivity( userActivity : NSUserActivity )
}
class OAuthManager {
public static let instance = OAuthManager()
var asWebSession: ASWebAuthenticationSession?
}
extension OAuthManager : UserActivityListener {
func callBackUserActivity(userActivity: NSUserActivity) {
// Get URL components from the incoming user activity.
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
return
}
// Check for specific URL components that you need.
guard let path = components.path,
let params = components.queryItems else {
return
}
// cancel your ASWebAuthenticationSession
asWebSession?.cancel()
print("path = \(userActivity.webpageURL)")
if let token = params.first(where: { $0.name == "token" })?.value {
print("token = \(token)")
}
}
}YourViewController.swift
class YourViewController: UIViewController {
// set your instance
var oauthManager = OAuthManager.instance
private func startSignInAsWebAuthSession() {
let callbackURLScheme = "YOURAPP"
guard let authURL = URL(string: "YOUR URL LINK") else { return }
self.oauthManager.asWebSession = ASWebAuthenticationSession.init(url: authURL, callbackURLScheme: callbackURLScheme,completionHandler: { callbackURL, error in
// we dont listen to the call back, this web authentication session only open for login only
})
oauthManager.asWebSession?.prefersEphemeralWebBrowserSession = true
oauthManager.asWebSession?.presentationContextProvider = self
oauthManager.asWebSession?.start()
}
}
extension YourViewController: ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
ASPresentationAnchor()
}
}https://stackoverflow.com/questions/61748589
复制相似问题