gpt4 book ai didi

ios - AWS iOS SDK Cognito 开发人员身份验证 (Swift)

转载 作者:可可西里 更新时间:2023-11-01 04:15:28 26 4
gpt4 key购买 nike

我很难弄清楚如何将我的服务器(通过 AWS)提供的开发人员凭据返回给我的示例身份提供商。

看来我需要在 ExampleIdentityProvider 类的 refresh 方法中同步执行此操作。我正在使用 AFNetworking 发出请求,但它是一个异步 GET 请求。如何为我的 IdentityProvider 上的刷新方法同步执行此操作?

以下是 Swift 中的内容:

class ExampleIdentityProvider: AWSAbstractIdentityProvider  {
var newToken: String!

override var token: String {
get {
return newToken
}
set {
newToken = newValue
}
}

override func getIdentityId() -> BFTask! {
if self.identityId != nil {
return BFTask(result: self.identityId)
}else{
return BFTask(result: nil).continueWithBlock({ (task) -> AnyObject! in
if self.identityId == nil {
return self.refresh()
}
return BFTask(result: self.identityId)
})
}
}

override func refresh() -> BFTask! {
return BFTask(result: nil).continueWithBlock({ (task) -> AnyObject! in
let result = AFNETWORKING REQUEST FOR CREDENTIALS TO MY SERVER
self.identityId = result.identityId
self.token = result.token

return BFTask(result: self.identityId)
})
}
}

最佳答案

我相信我已经弄明白了。我需要利用 BFTask,它是为处理后台任务而构建的。

对于使用 Cognito 快速实现开发人员身份验证的人来说,他们可能与我有类似的设置,这就是我完成它的方式:

class ExampleAppIdentityProvider: AWSAbstractCognitoIdentityProvider {
var _token: String!
var _logins: [ NSObject : AnyObject ]!

// Header stuff you may not need but I use for auth with my server
let acceptHeader = "application/vnd.exampleapp-api+json;version=1;"
let authHeader = "Token token="
let userDefaults = NSUserDefaults.standardUserDefaults()
let authToken = self.userDefaults.valueForKey("authentication_token") as String

// End point that my server gives amazon identityId and tokens to authorized users
let url = "https://api.myapp.com/api/amazon_id/"

override var token: String {
get {
return _token
}
}

override var logins: [ NSObject : AnyObject ]! {
get {
return _logins
}
set {
_logins = newValue
}
}

override func getIdentityId() -> BFTask! {
if self.identityId != nil {
return BFTask(result: self.identityId)
}else{
return BFTask(result: nil).continueWithBlock({ (task) -> AnyObject! in
if self.identityId == nil {
return self.refresh()
}
return BFTask(result: self.identityId)
})
}
}

override func refresh() -> BFTask! {
let task = BFTaskCompletionSource()
let request = AFHTTPRequestOperationManager()
request.requestSerializer.setValue(self.acceptHeader, forHTTPHeaderField: "ACCEPT")
request.requestSerializer.setValue(self.authHeader+authToken, forHTTPHeaderField: "AUTHORIZATION")
request.GET(self.url, parameters: nil, success: { (request: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
// The following 3 lines are required as referenced here: http://stackoverflow.com/a/26741208/535363
var tmp = NSMutableDictionary()
tmp.setObject("temp", forKey: "ExampleApp")
self.logins = tmp

// Get the properties from my server response
let properties: NSDictionary = response.objectForKey("properties") as NSDictionary
let amazonId = properties.objectForKey("amazon_identity") as String
let amazonToken = properties.objectForKey("token") as String

// Set the identityId and token for the ExampleAppIdentityProvider
self.identityId = amazonId
self._token = amazonToken

task.setResult(response)
}, failure: { (request: AFHTTPRequestOperation!, error: NSError!) -> Void in
task.setError(error)
})
return task.task
}
}

并通过以下操作初始化 ExampleAppIdentityProvider:

    let identityProvider = ExampleAppIdentityProvider()
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1, identityProvider: identityProvider, unauthRoleArn: GlobalVariables.cognitoUnauthRoleArn, authRoleArn: GlobalVariables.cognitoAuthRoleArn)
let defaultServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration

let transferManager = AWSS3TransferManager.defaultS3TransferManager()

let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = GlobalVariables.awsBucket
uploadRequest.key = "\(GlobalVariables.environment)/uploads/users/\(userId)/\(type)/\(timestamp)/original.jpg"
uploadRequest.ACL = .AuthenticatedRead
uploadRequest.body = tmpFileUrl

// Upload file
let task = transferManager.upload(uploadRequest)

我创建了一个名为 GlobalVariablesstruct,其中包含保存 bucketunAuthRoleArnauthRoleArn 等。当然你不必这样做,但我提到它以防有人感到困惑。

关于ios - AWS iOS SDK Cognito 开发人员身份验证 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28511256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com