我正在构建一个 iOS (Swift) 应用程序,使用 AWS 作为后端并带有开发人员身份验证。一切正常,直到我关闭应用程序,离开一段时间然后重新启动。在这种情况下,我经常(但并非总是)在尝试从 AWS 检索数据时收到 ExpiredTokenException 错误。
这是我的代码:
class DeveloperAuthenticatedIdentityProvider: AWSAbstractCognitoIdentityProvider {
var _token: String!
var _logins: [ NSObject : AnyObject ]!
override var token: String {
get {
return _token
}
}
override var logins: [ NSObject : AnyObject ]! {
get {
return _logins
}
set {
_logins = newValue
}
}
override func getIdentityId() -> AWSTask! {
if self.identityId != nil {
return AWSTask(result: self.identityId)
} else {
return AWSTask(result: nil).continueWithBlock({ (task) -> AnyObject! in
if self.identityId == nil {
return self.refresh()
}
return AWSTask(result: self.identityId)
})
}
}
override func refresh() -> AWSTask! {
let apiUrl = "https://url-goes-here" // call my server to retrieve an OpenIdToken
request.GET(apiUrl, parameters: nil, progress: nil,
success: {
(task: NSURLSessionDataTask, response: AnyObject?) -> Void in
let tmp = NSMutableDictionary()
tmp.setObject("temp", forKey: "ExampleApp")
self.logins = tmp as [ NSObject : AnyObject ]
let jsonDictionary = response as! NSDictionary
self.identityId = jsonDictionary["identityId"] as! String
self._token = jsonDictionary["token"] as! String
awstask.setResult(response)
},
failure: {
(task: NSURLSessionDataTask?, error: NSError) -> Void in
awstask.setError(error)
}
)
return awstask.task
}
}
在 AppDelegate 中:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let identityProvider = DeveloperAuthenticatedIdentityProvider()
// set default service configuration
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: cognitoRegion, identityProvider: identityProvider, unauthRoleArn: unauthRole, authRoleArn: authRole)
let configuration = AWSServiceConfiguration(region: defaultServiceRegion, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
// set service configuration for S3 (my bucket is located in a different region to my Cognito and Lambda service)
let credentialsProviderForS3 = AWSCognitoCredentialsProvider(regionType: cognitoRegion, identityProvider: identityProvider, unauthRoleArn: unauthRole, authRoleArn: unauthRole)
let awsConfigurationForS3 = AWSServiceConfiguration(region: s3ServiceRegion, credentialsProvider: credentialsProviderForS3)
AWSS3TransferUtility.registerS3TransferUtilityWithConfiguration(awsConfigurationForS3, forKey: "S3")
return true
}
这个 post表明 Cognito token 已过期,由开发人员手动刷新。这似乎过于复杂,因为它需要设置一个定期刷新的计时器,处理应用程序关闭和重新启动,以及处理刷新时发生的 AWS 请求。有没有更简单的方法?例如,是否可以让 AWS 开发工具包在尝试使用过期 token 查询服务器时自动调用刷新?
任何帮助将不胜感激。我正在使用适用于 iOS 的 AWS 开发工具包 2.3.5 版。
适用于 iOS 2.4.x 的 AWS 移动开发工具包有一个名为 AWSIdentityProviderManager
的新协议(protocol).它有以下方法:
/**
* Each entry in logins represents a single login with an identity provider.
* The key is the domain of the login provider (e.g. 'graph.facebook.com') and the value is the
* OAuth/OpenId Connect token that results from an authentication with that login provider.
*/
- (AWSTask<NSDictionary<NSString *, NSString *> *> *)logins;
符合此协议(protocol)的对象的职责是在请求时返回有效的logins
字典。由于此方法是异步的,因此如果缓存的 token 过期,您可以在其中进行网络调用。实现取决于您,但在许多情况下,AWSIdentityProviderManager
管理多个 AWSIdentityProvider
s,聚合它们并返回 logins
字典。
关于ios - 应用程序重新启动后的 AWS ExpiredTokenException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37127831/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |