【问题标题】:iOS 12 - AppAuth redirect URL not trigger AppDelegateiOS 12 - AppAuth 重定向 URL 不触发 AppDelegate
【发布时间】:2019-05-28 20:43:56
【问题描述】:

我在我的代码中使用AppAuth

我设法验证成功,但是当SFSafariViewController gets dismiss from my Controller 时,重定向url 不会触发AppDelegate func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool

重定向 URL 是我的 Bundle Identifier 名称:BundleIdentifier://authenticate

我在 info.plist 中设置了 url 方案和 url 标识符,它们具有相同的名称。

当我在这个函数上设置断点运行我的代码时,我可以看到我的重定向 URL 对 standarizedURLstandarizedRedirectURL 是正确的

- (BOOL)shouldHandleURL:(NSURL *)URL {
  NSURL *standardizedURL = [URL standardizedURL];
  NSURL *standardizedRedirectURL = [_request.redirectURL standardizedURL];

    return OIDIsEqualIncludingNil(standardizedURL.scheme, standardizedRedirectURL.scheme) &&
    OIDIsEqualIncludingNil(standardizedURL.user, standardizedRedirectURL.user) &&
    OIDIsEqualIncludingNil(standardizedURL.password, standardizedRedirectURL.password) &&
    OIDIsEqualIncludingNil(standardizedURL.host, standardizedRedirectURL.host) &&
    OIDIsEqualIncludingNil(standardizedURL.port, standardizedRedirectURL.port) &&
    OIDIsEqualIncludingNil(standardizedURL.path, standardizedRedirectURL.path);

但是当 AppAuth 完成身份验证并且我有一个 access token 时,func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool 不会被触发。

知道为什么吗?

这是我的代码

class func signInAuth(discoveryURLstr: String,presenter : UIViewController,completionHandler: @escaping ( (OIDAuthState?,Error?) -> () )){

        guard let discoveruURL = URL(string: discoveryURLstr) else{
            completionHandler(nil,AuthErrors.InvalidDiscoveryURL)
            return
        }

        appAuthDiscoverConfiguration(discoveryURL: discoveruURL) { (configurationFile, error) in

            guard let configurationFile = configurationFile else {
                completionHandler(nil,AuthErrors.InvalidConfigurationFile)
                return
            }

            let authRequest = appAuthRequest(configurationFile: configurationFile)

             self.appAuthenticationSession = OIDAuthState.authState(byPresenting: authRequest, presenting: presenter, callback: { (state, error) in

                if let error = error {
                    //self.authState = nil
                    completionHandler(nil,error)
                    return
                }

                if let state = state {
                    self.authState = state
                    completionHandler(state,nil)

                }else{
                    completionHandler(nil,AuthErrors.InvalideState)
                }
            })

        }


    }

    class func appAuthDiscoverConfiguration(discoveryURL : URL, completionHandler: @escaping ((OIDServiceConfiguration?,Error?) -> ())) {

        OIDAuthorizationService.discoverConfiguration(forDiscoveryURL: discoveryURL) { (configuration, error) in

            if let error = error {
                completionHandler(nil,error)
                return
            }else{
                guard let configurationFile = configuration else {
                    completionHandler(nil,AuthErrors.InvalidConfigurationFile)
                    return
                }
                completionHandler(configurationFile,nil)
            }

        }

    }

    class func appAuthRequest(configurationFile : OIDServiceConfiguration) -> OIDAuthorizationRequest{

        return OIDAuthorizationRequest(configuration: configurationFile, clientId: AppAuthConstants.clientId, clientSecret: nil, scope: AppAuthConstants.scope, redirectURL: AppAuthConstants.redirectURL, responseType: AppAuthConstants.responseType, state: nil, nonce: nil, codeVerifier: nil, codeChallenge: nil, codeChallengeMethod: nil, additionalParameters: AppAuthConstants.additionalParameters)
    }

【问题讨论】:

  • 您的服务器是否调用此 url:重定向 URL 是我的 Bundle Identifier 名称:BundleIdentifier://authenticate after authentication?
  • 服务器没有调用这个 url。这是重定向网址。如您所见,我在我的请求中设置了重定向网址。并且重定向 url 是 bundleidentifier://authenticate

标签: ios swift oauth-2.0 url-redirection appauth


【解决方案1】:

在 iOS 12 上,App-Auth 使用 ASWebAuthenticationSession,而在 iOS 11 上,它使用现已弃用的 SFAuthenticationSession,而不是要求应用支持手动处理重定向。要支持早期版本的 iOS,您仍然需要 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool 方法中的代码。

作为参考,您可以查看 AppAuth 在 here 的幕后所做的工作。此外,this 是一个很好的答案,它解释了如何在不使用 AppAuth 的情况下在 iOS 上通用地获取 OAuth 令牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-12
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多