Cognito Authentication working but errors show?

淺唱寂寞╮ 提交于 2019-12-01 06:40:52

问题


Basically I'm using a developer authenticated identity to authenticate my users. Even though this error is showing:

AWSiOSSDKv2 [Verbose] AWSURLResponseSerialization.m line:87 | -[AWSJSONResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response body: [{"__type":"InvalidParameterException","message":"Please provide a valid public provider"}] 2015-10-20 17:50:19.251 BusyTime[56549:7365231] AWSiOSSDKv2 [Error] AWSCredentialsProvider.m line:435 | __73-[AWSCognitoCredentialsProvider getCredentialsWithCognito:authenticated:]_block_invoke | GetCredentialsForIdentity failed. Error is [Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=7 "The operation couldn’t be completed. (com.amazonaws.AWSCognitoIdentityErrorDomain error 7.)" UserInfo=0x7f9d9342dde0 {__type=InvalidParameterException, message=Please provide a valid public provider}]

Though this error shows, I'm still returning my identityId and Token and successfully invoking a lambda method that requires authenticated privileges to call. I believe it has something to do with my refresh method but I'm not exactly sure. Please help me deal with this error.

my credentials provider code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [AWSLogger defaultLogger].logLevel = AWSLogLevelVerbose;
    id<AWSCognitoIdentityProvider> identityProvider = [[BusytimeAuthenticated alloc] initWithRegionType:AWSRegionUSEast1
                                                                                             identityId:nil
                                                                                         identityPoolId:@"EXAMPLEPOOLID"
                                                                                logins:@{@"login.busytimeapp": [defaults objectForKey:@"username"]}
                                                                                           providerName:@"login.busytimeapp"
                                                       ];

    credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1
                                                                   identityProvider:identityProvider
                                                                      unauthRoleArn:nil
                                                                        authRoleArn:nil];

    configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1
                                                credentialsProvider:self.credentialsProvider];
    AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
    [[credentialsProvider refresh] continueWithBlock:^id(BFTask *task){
        [self testAuth];
        return nil;
    }];

My Refresh method:

- (BFTask *)refresh {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![self authenticatedWithProvider]) {
        return [super getIdentityId];
    }else{
        NSDictionary *post = [[NSDictionary alloc] initWithObjectsAndKeys:
                              [defaults objectForKey:@"username"], @"username",
                              [defaults objectForKey:@"password"], @"password",
                              nil];
        NSError *error;
        NSData *postData = [NSJSONSerialization dataWithJSONObject:post options:0 error:&error];
        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:@"SOMEURL"]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody:postData];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        __block BOOL isLogged = false;
        [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSDictionary *newJSON = [NSJSONSerialization JSONObjectWithData:data
                                                                    options:0
                                                                      error:&error];
            isLogged = true;
            if(!newJSON){
                NSLog(@"DID NOT AUTHENTICATE");
            }else{

            self.identityId = [newJSON objectForKey:@"IdentityId" ];
            self.token = [newJSON objectForKey:@"Token" ];
            NSLog(@"Result: %@", newJSON);
            }

        }] resume];

        return [super getIdentityId];

    }
    return [super getIdentityId];
}

回答1:


The problem here is that you are passing the login provider name in the logins map as key. You can't do that for Developer Authenticated Identities.

Instead you should use the Cognito identity provider name, cognito-identity.amazonaws.com, in the map, like it says in the documentation. This should fix you exception.



来源:https://stackoverflow.com/questions/33228309/cognito-authentication-working-but-errors-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!