【问题标题】:Refresh Token AWS Cognito User Pool, Code Block Not Even Running刷新令牌 AWS Cognito 用户池,代码块甚至没有运行
【发布时间】:2018-02-22 21:06:12
【问题描述】:

我正在尝试刷新用户令牌,并且我正在一个“子”应用程序中执行此操作,该应用程序只能访问 idToken、accessToken、refreshToken 以及用户的密码信息。它是沙盒的,无法与主应用程序通信。

我已尝试使用以下代码运行initialAuth

let authParams = [
    "REFRESH_TOKEN": user.refreshToken, 
    "SECRET_HASH": config.getClientSecret()
]

let provider = AWSCognitoIdentityProvider(forKey: config.getClientSecret())

let req = AWSCognitoIdentityProviderInitiateAuthRequest()
req?.authFlow = AWSCognitoIdentityProviderAuthFlowType.refreshToken
req?.clientId = config.getClientId()
req?.authParameters = authParams

provider.initiateAuth(req!).continueWith() {
    resp in
    print("I'm not running")
    if (resp != nil) {
        print(resp.error.debugDescription)
    } else {
        print(resp.result.debugDescription)
    }
    return nil
}

令人震惊的是, continueWith 代码块甚至根本没有运行,并且没有调用 print 语句“我没有运行”。我不知道该怎么做,因为这似乎正在做他们在 SDK 中所做的事情:https://github.com/aws/aws-sdk-ios/blob/master/AWSCognitoIdentityProvider/AWSCognitoIdentityUser.m#L173

【问题讨论】:

  • 您之前是否注册了一个 AWSCognitoIdentityProvider 对象,其中 clientSecret 作为密钥?即,您可以确认提供者!= nil 吗?

标签: ios swift amazon-web-services aws-sdk amazon-cognito


【解决方案1】:

@behrooziAWS 的评论为我解决了同样的问题。

具体来说,即使 AWSCognitoIdentityProvider(forKey:) 的初始化程序在 XCode 的快速帮助中记录为:

+ (nonnull instancetype)CognitoIdentityProviderForKey:(nonnull NSString *)key;

如果未找到提供的keyCognitoIdentityProvider,它可以(并且它将)返回nil。发生这种情况的一种情况是从未为提供的key 调用AWSCognitoIdentityProvider.register(with:forKey:)

if(provider != nil) 产生一个编译器警告它永远是true 时,这更加令人困惑。我必须使用以下代码 sn-p 才能让事情在没有警告的情况下正常工作:

    let provider: AWSCognitoIdentityProvider? = AWSCognitoIdentityProvider(
        forKey: config.getClientSecret())
    guard provider != nil else {
        fatalError("AWSCognitoIdentityProvider not registered!")
    }

P.S.我希望在 Swift+ObjC 方面有更多经验的人可以评论为什么初始化程序出现或被翻译为 nonnull instancetype,即使 Objective C 源代码只有 instancetype。 编辑:这个初始化程序(以及AWSCognitoIdentityProvider/AWSCognitoIdentityUser.h 文件中的几乎所有其他内容)自动用nonnull 注释的原因是因为NS_ASSUME_NONNULL_BEGINNS_ASSUME_NONNULL_END 宏。对于CognitoIdentityProviderForKey,这个非空假设应该是无效的。

【讨论】:

    猜你喜欢
    • 2017-11-22
    • 2018-08-14
    • 2016-09-23
    • 1970-01-01
    • 2018-07-02
    • 2016-12-30
    • 2018-05-06
    • 2021-12-07
    • 2018-01-21
    相关资源
    最近更新 更多