Skip to content
This repository has been archived by the owner on Oct 12, 2018. It is now read-only.

iOS cognito credentials - expiration should be a string #39

Open
markusklems opened this issue Jan 29, 2017 · 2 comments
Open

iOS cognito credentials - expiration should be a string #39

markusklems opened this issue Jan 29, 2017 · 2 comments

Comments

@markusklems
Copy link

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
Copy link

Are you using v0.0.1? This seems to be working for me in v0.0.2 - see this change: 152deb6

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
Copy link

dsonara commented Mar 9, 2018

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 !!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants