amazon-archives / aws-sdk-react-native

AWS SDK for React Native (developer preview)
Apache License 2.0
631 stars 68 forks source link

iOS cognito credentials - expiration should be a string #39

Open markusklems opened 7 years ago

markusklems commented 7 years ago

I think there is a bug in ios/Core/AWSRNCognitoCredentials.m. It seems like cred.expiration is a date and not a string. I looked up the expiration string format and eventually got it working with this change (I can't program native iOS, so maybe someone else is better suited to create a pull request):

RCT_EXPORT_METHOD(getCredentialsAsync:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
    [[credentialProvider credentials] continueWithBlock:^id(AWSTask *task) {
        if (task.error) {
            reject([NSString stringWithFormat:@"%ld",task.error.code],task.error.description,task.error);
        }
        else {
            AWSCredentials *cred = (AWSCredentials*) task.result;
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
            [dateFormat setDateFormat:@"yyyy-MM-ddTHH-mm-ss.SSZ"];
            NSString *expF = [dateFormat stringFromDate:cred.expiration];
            NSDictionary *dict = @{@"AccessKey":cred.accessKey,@"SecretKey":cred.secretKey,@"SessionKey":cred.sessionKey,@"Expiration":expF};
            resolve(dict);
        }
        return nil;
    }];
}
webmariner commented 7 years ago

Are you using v0.0.1? This seems to be working for me in v0.0.2 - see this change: https://github.com/awslabs/aws-sdk-react-native/commit/152deb6f0d7a50f61a882dcb346dcbe3dcfda715

That said, the date format returned in iOS seems to differ from that returned by the Android code. In iOS, I now get a string like "2017-02-27T14:20:00Z" but in Android the code uses credentialsProvider.getSessionCredentitalsExpiration().toString(), which returns something like "Mon Feb 27 14:20:00 GMT+00:00 2017".

dsonara commented 6 years ago

NSString AWSCognitoPoolID = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"AWSCognitoID"]; AWSCognitoCredentialsProvider credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AMAZON_COGNITO_REGION identityPoolId:AWSCognitoPoolID]; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider]; AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;

[[credentialsProvider credentials] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    } else {
        AWSCredentials *cred = (AWSCredentials*) task.result;
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
        [dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
        NSString *expF = [dateFormat stringFromDate:cred.expiration];
        NSLog(@"Credentials expire at: %@", expF);
        [NSTimer scheduledTimerWithTimeInterval:cred.expiration.timeIntervalSinceNow target:self selector:@selector(initializeAmazonCongnitoProvider) userInfo:nil repeats:NO];
    }
    return nil;
}];

[[credentialsProvider getIdentityId] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    } else {
        NSString *cognitoId = task.result;
        NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
        [standardDefaults setObject:cognitoId forKey:@"UserCognitoID"];
        [standardDefaults synchronize];
    }
    return nil;
}];

I have used this code for fetching expiration time. Default expiration time is 60 minutes. So you need to re-auth it. Thank you !!