问题描述
我已经初始化每这个AWS开发者指南。我不知道,如果它的工作,以及如何检查。我似乎无法找到如何使用Cognito与斯威夫特的任何文件。我运行它作为一个单元测试,并且测试通过,该行打印(identityId,identityId)
输出:
identityId< AWSTask:0x17d5fde0;完成= NO;取消= NO;故障= NO;>
不过,调试属性中 identityProvider.identityId
为零。
下面是我的文件:
// MyAuth.swift
进口基金会
进口AWSCognito
类MyAuth {
FUNC getUnauthCognitoId() - >布尔{
让identityProvider = MyIdentityProvider()
让credentialsProvider = AWSCognitoCredentialsProvider(regionType:AWSRegionType.USEast1,identityProvider:identityProvider,unauthRoleArn:Constants.ARNUnauth.value,authRoleArn:Constants.ARNAuth.value)
让defaultServiceConfiguration = AWSServiceConfiguration(地区:.USEast1,credentialsProvider:credentialsProvider)
AWSServiceManager.defaultServiceManager()。defaultServiceConfiguration = defaultServiceConfiguration
如果让identityId = identityProvider.getIdentityId(){
打印(identityId,identityId)
返回true
} 其他 {
返回false
}
}
}
和
// MyIdentityProvider.swift
进口基金会
进口AWSCognito
类MyIdentityProvider:AWSAbstractCognitoIdentityProvider {
VAR _token:字符串!
VAR _logins:[NSObject的:AnyObject]!
//头的东西,你可能并不需要,但我的身份验证使用我的服务器
/ *让acceptHeader =应用/ vnd.exampleapp-API + JSON;版本= 1;
让authHeader =令牌记号=
让userDefaults = NSUserDefaults.standardUserDefaults()
我们的authToken = self.userDefaults.valueForKey(authentication_token)作为字符串* /
//终点,我的服务器给亚马逊identityId和令牌授权用户
让URL =https://api.myapp.com/api/amazon_id/
FUNC authenticatedWithProvider() - >布尔{
如果让登录= _logins {
返回登录[的ProviderName] ==零
}
其他 {
返回false
}
}
覆盖VAR令牌:字符串{
得到 {
返回_token
}
}
覆盖VAR登录:[NSObject的:AnyObject]! {
得到 {
返回_logins
}
组 {
_logins =为newValue
}
}
覆盖FUNC getIdentityId() - GT; AWSTask! {
如果self.identityId!=零{
返回AWSTask(结果:self.identityId)
}
否则如果(!self.authenticatedWithProvider()){
返回super.getIdentityId()
}
其他{
返回AWSTask(结果:无)!.continueWithBlock({(任务) - > AnyObject在
如果self.identityId ==零{
返回self.refresh()
}
返回AWSTask(结果:self.identityId)
})
}
}
覆盖FUNC刷新() - GT; AWSTask! {
让工作= AWSTaskCompletionSource()
如果(!self.authenticatedWithProvider()){
返回super.getIdentityId()
}
其他 {
// TODO:验证与开发商
返回task.task
}
/ *让请求= AFHTT prequestOperationManager()
request.requestSerializer.setValue(self.acceptHeader,forHTTPHeaderField:接受)
request.requestSerializer.setValue(self.authHeader +的authToken,forHTTPHeaderField:授权)
request.GET中(self.url,参数:无,成功:{(要求:AFHTT prequestOperation!回应:AnyObject) - >太虚了!
//需要以下3条线为参考这里:http://stackoverflow.com/a/26741208/535363
VAR TMP = NSMutableDictionary的()
tmp.setObject(温度,forKey:为ExampleApp)
self.logins = TMP
//从我的服务器响应获取的属性
让属性:NSDictionary的= response.objectForKey(物业)作为NSDictionary的
让amazonId = properties.objectForKey(amazon_identity)作为字符串
让amazonToken = properties.objectForKey(令牌)为String
//设置identityId和令牌的ExampleAppIdentityProvider
self.identityId = amazonId
self._token = amazonToken
task.setResult(响应)
},失败:{(要求:AFHTT prequestOperation!错误:NSError) - >虚空中
task.setError(错误)
})* /
返回task.task
}
}
和
进口XCTest
@testable导入我的
类MyTests:XCTestCase {
覆盖FUNC设置(){
super.setUp()
//将设置code在这里。这种方法类中的每个测试方法的调用之前被调用。
}
覆盖FUNC tearDown的(){
//把拆卸code在这里。这种方法类中的每个测试方法的调用之后被调用。
super.tearDown()
}
FUNC testExample(){
//这是一个功能测试情况下的一个例子。
//使用XCTAssert和相关功能来验证你的测试产生正确的结果。
}
FUNC testPerformanceExample(){
//这是一个性能测试情况下的一个例子。
self.measureBlock {
//把code要衡量这里的时间。
}
}
FUNC testGetUnauthCognitoId(){
让myAuth = MyAuth()
XCTAssertTrue(myAuth.getUnauthCognitoId())
}
}
getIdentityId 返回 AWSTask
。由于 AWSTask
基本 BFTask
使用不同的名称,可以使用的显示语法。是这样的:
credentialProvider.getIdentityId()。continueWithBlock {
(任务:AWSTask!) - > AWSTask在
如果task.error(){
//无法检索identityId。
} 其他 {
打印(identityId,task.result())
}
I have initialized the credentials provider per this AWS Developer Guide. I'm not sure if it worked, and how to check. I can't seem to find any documentation on how to use Cognito with Swift. I'm running it as a unit test, and the test passes and the line print("identityId", identityId)
outputs:
identityId <AWSTask: 0x17d5fde0; completed = NO; cancelled = NO; faulted = NO;>
However, during debug the property identityProvider.identityId
is nil.
Here are my files:
// MyAuth.swift
import Foundation
import AWSCognito
class MyAuth {
func getUnauthCognitoId()->Bool {
let identityProvider = MyIdentityProvider()
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1, identityProvider: identityProvider, unauthRoleArn: Constants.ARNUnauth.value, authRoleArn: Constants.ARNAuth.value)
let defaultServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration
if let identityId = identityProvider.getIdentityId() {
print("identityId", identityId)
return true
} else {
return false
}
}
}
And
// MyIdentityProvider.swift
import Foundation
import AWSCognito
class MyIdentityProvider: 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/"
func authenticatedWithProvider()->Bool {
if let logins = _logins {
return logins["ProviderName"] == nil
}
else {
return false
}
}
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 if(!self.authenticatedWithProvider()) {
return super.getIdentityId()
}
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 task = AWSTaskCompletionSource()
if(!self.authenticatedWithProvider()) {
return super.getIdentityId()
}
else {
// TODO: Authenticate with developer
return task.task
}
/*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
}
}
And
import XCTest
@testable import My
class MyTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
// Put the code you want to measure the time of here.
}
}
func testGetUnauthCognitoId() {
let myAuth = MyAuth()
XCTAssertTrue(myAuth.getUnauthCognitoId())
}
}
getIdentityId returns an AWSTask
. Since AWSTask
is essentially BFTask
with a different name, you can get the identityId using the continueWithBlock syntax shown on the BFTask page. Something like:
credentialProvider.getIdentityId().continueWithBlock {
(task: AWSTask!) -> AWSTask in
if task.error() {
// failed to retrieve identityId.
} else {
print("identityId", task.result())
}
这篇关于如何使用SWIFT获取未认证身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!