使用AppAuth和Okta时,iOS上的身份验证问题

时间:2017-09-14 20:54:15

标签: ios swift okta appauth

我有一个简单的iOS Swift应用程序松散地基于AppAuth-iOS示例(https://github.com/openid/AppAuth-iOS)以及Okta OAuth示例(https://github.com/oktadeveloper/okta-openidconnect-appauth-ios)。我没有使用Service Discovery或authomatic令牌获取(即不使用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 我确实重写了代码以使用 authStateByPresentingAuthorizationRequest()对抗Okta并获得相同的结果(即重定向到我的应用程序后卡住了)。我针对Azure AD进行了测试,它运行正常。

1 个答案:

答案 0 :(得分:1)

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