【问题标题】:Athentication problems on iOS when using AppAuth and Okta使用 AppAuth 和 Okta 时 iOS 上的身份验证问题
【发布时间】:2017-09-14 20:54:14
【问题描述】:

我有一个基于 AppAuth-iOS 示例 (https://github.com/openid/AppAuth-iOS) 和 Okta OAuth 示例 (https://github.com/oktadeveloper/okta-openidconnect-appauth-ios) 的简单 iOS Swift 应用程序。我没有使用服务发现或自动令牌获取(即不使用 authStateByPresentingAuthorizationRequest)。

我的示例适用于 Azure AD,但不适用于 Okta。我能够登录并通过身份验证并重定向回我的移动应用程序 (AppDelegate.application()),但随后流程不会返回到我的 OIDAuthorizationService.present() 完成块。

这里有一些代码:

@IBAction func signInButton(_ sender: Any) {

    // select idp
    switch selectedIdentityProvider! {
    case "Azure AD":
        selectedAuthConfig = AzureAdAuthConfig()
    case "Okta":
        selectedAuthConfig = OktaAuthConfig();
    default:
        return
    }

    appAuthAuthorize(authConfig: selectedAuthConfig!)
}

func appAuthAuthorize(authConfig: AuthConfig) {
    let serviceConfiguration = OIDServiceConfiguration(
        authorizationEndpoint: NSURL(string: authConfig.authEndPoint)! as URL,
        tokenEndpoint: NSURL(string: authConfig.tokenEndPoint)! as URL)

    let request = OIDAuthorizationRequest(configuration: serviceConfiguration, clientId: authConfig.clientId, scopes: authConfig.scope, redirectURL: NSURL(string: authConfig.redirectUri)! as URL, responseType: OIDResponseTypeCode, additionalParameters: nil)

    doAppAuthAuthorization(authRequest: request)
}


func doAppAuthAuthorization(authRequest: OIDAuthorizationRequest) {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    appDelegate.currentAuthorizationFlow = OIDAuthorizationService.present(authRequest, presenting: self, callback: {
        (authorizationResponse, error) in
        if (authorizationResponse != nil) {
            self.authState = OIDAuthState(authorizationResponse: authorizationResponse!)
            self.logMessage(message: "Got authorization tokens. Access token: \(String(describing: self.authState?.lastAuthorizationResponse.authorizationCode))")
            self.doTokenRequest()
        } else {
            self.authState = nil
            self.logMessage(message: "Authorization error: \(String(describing: error?.localizedDescription))")
        }
    })
}

我可以重写代码以使用 authStateByPresentingAuthorizationRequest() 来查看它是否有效,但我有点怀疑,因为此代码适用于 Azure AD。有什么建议吗?

更新 1 我忘了提到我有一个可以运行的 Android/Java 示例,它与相同的 Okta 定义背道而驰,并且像魅力一样工作。

更新 2 我确实重写了代码以对 Okta 使用 authStateByPresentingAuthorizationRequest() 并得到相同的结果(即在重定向回我的应用程序后卡住)。我在 Azure AD 上对此进行了测试,它工作正常。

【问题讨论】:

  • 您的 AppDelegate.swift 文件是否正确处理了重定向?另外,您是否可以看到表明您收到了授权码的消息?
  • 感谢您指出这一点 - 我从未看过 AppDelegate 中发生了什么。尚无答案或解决方案,但是:
  • 感谢您指出这一点 - 我从未看过 AppDelegate 中发生了什么。尚无答案或解决方案,但 resumeAuthorizationFlow(with: url) 返回 false 导致挂起。返回的 URL 格式如下:com.oktapreview..oktaoauthdemo://oauth/redirect?code=UZmPTB?????Tz_F7bLOM&state=?????uW-Ab1E-3AZHI_no-????? OQhGvffHHuX2Dk-Q。在 Android 上运行类似的代码会产生类似的返回 URL,只是状态更短。

标签: ios swift okta appauth


【解决方案1】:

已解决。 我猜问题是 Okta 中定义的重定向 URL 是大小写混合的。 Android AppAuth 实现不介意,但 iOS AppAuth 实现可以。将 Okta 中的重定向 URL 更改为仅小写,将传入的重定向 Uri 参数更改为仅小写和 bing,一切都很好。感谢@jmelberg 为我指明了这个方向——通过调试 resumeAuthorizationFlow(with: url) 我能够看到确切的行为以及调用返回 False 的原因。

【讨论】:

  • 我收到“OAuth 错误:invalid_grant”,有什么解决方案吗?你遇到过类似的问题吗?
猜你喜欢
  • 1970-01-01
  • 2021-03-05
  • 2022-09-29
  • 2021-01-11
  • 2011-09-24
  • 2021-07-18
  • 1970-01-01
  • 2020-04-18
  • 2016-12-07
相关资源
最近更新 更多