VDog
VDog

Reputation: 1173

Refresh Token AWS Cognito User Pool, Code Block Not Even Running

I am attempting to refresh a users tokens, and I am doing this in a "child" app that only has access to idToken, accessToken, refreshToken, and the user's information aside for their password. It is sandboxed and cannot communicate to main app.

I have attempted the following code to run initiateAuth

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
}

Shockingly enough, the continueWith block of code doesn't even run at all, and the print statement "I'm not running" is not called. I'm at a loss of what to do, as this seems to be doing what they do in their SDK: https://github.com/aws/aws-sdk-ios/blob/master/AWSCognitoIdentityProvider/AWSCognitoIdentityUser.m#L173

Upvotes: 2

Views: 970

Answers (1)

martian111
martian111

Reputation: 593

The comment from @behrooziAWS solved this same issue for me.

Specifically, even though the initializer for AWSCognitoIdentityProvider(forKey:) is documented in XCode's quick help as:

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

it can (and it will) return nil if the CognitoIdentityProvider for the provided key is not found. One case this happens is when AWSCognitoIdentityProvider.register(with:forKey:) was never called for the provided key.

It's even more confusing when if(provider != nil) produces a compiler warning that it will always be true. I had to use the following code snippet to get things working without warnings:

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

P.S. I hope someone with more experience in Swift+ObjC can comment on why the initializer shows up or is translated as nonnull instancetype even though the Objective C source code only has instancetype. Edit: The reason why this initializer (and pretty much everything else in the AWSCognitoIdentityProvider/AWSCognitoIdentityUser.h file) is annotated with nonnull automatically is because of the NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END macros. In the case of CognitoIdentityProviderForKey, this non-null assumption should not be valid.

Upvotes: 2

Related Questions