【发布时间】: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,只是状态更短。