vendredi 24 juin 2016

Using DynamoDB With Cognito: Token is not from a supported provider of this identity pool


I am in the process of implementing registration and login for my iOS app, using this project as an example: https://github.com/awslabs/aws-sdk-ios-samples/tree/75ada5b6283b7c04c1214b2e1e0a6394377e3f2b/CognitoYourUserPools-Sample/Objective-C/CognitoYourUserPoolsSample Previously, my app was able to access DynamoDB resources by using a credentials provider set up in my AppDelegate's didFinishLaunchingWithOptions method. However, after changing my project to include logging in and function like the example, I see the error: "__type":"NotAuthorizedException","message":"Token is not from a supported provider of this identity pool." The code setting the credentialsProviderin AppDelegate currently looks like this: let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil) let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId:APP_CLIENT_ID, clientSecret: APP_CLIENT_SECRET, poolId: USER_POOL_ID) AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: USER_POOL_NAME) let pool = AWSCognitoIdentityUserPool(forKey:USER_POOL_NAME) pool.delegate = self self.storyboard = UIStoryboard(name: "Main", bundle: nil) let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: IDENTITY_POOL_ID, identityProviderManager:pool) let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider) I also cannot access any DynamoDB data through my app. Based on the console output, the registration process seems to work correctly, although I'm unsure about the sign-in process. It occurred to me that I had changed the region from EU-West-1, where the DynamoDB resources were stored, to US-East-1. In order to account for this change, I repeated the same steps I had intially taken to allow my app to access DynamoDB: I created Auth and Unauth roles, both with access to the same actions as the role which had previously worked, but for the EU-West-1 resources instead. I set these roles to the user pool I created when setting up registration under "unauthenticated role" and "authenticated role". In case it makes a difference, I should note that I did not use the exact same sign-in process outlined in the example project I linked. Instead, I used the explicit sign in process, like so: let name = usernameField.text! let user = pool!.getUser(name) lock() user.getSession(name, password: passwordField.text!, validationData: nil, scopes: nil).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task:AWSTask!) -> AnyObject! in if task.error != nil { self.sendErrorPopup("ERROR: Unable to sign in. Error description: " + task.error!.description) } else { print("Successful Login") dispatch_async(dispatch_get_main_queue()){ self.performSegueWithIdentifier("mainViewControllerSegue", sender: self) } } self.unlock() return nil }) The methods lock(), unlock(), and sendErrorPopup() are strictly UI-related methods that I made so that the beginning and end of the sign-in process would be more visually clear. The console output always reads "successful login", but I am wondering if this code actually signs the user in correctly, since the error message makes it sound like the user might not be properly authorized. It occurred to me that the US-West tables might not have been set up correctly, but I experience the same problem even when trying to create new tables, so I don't think that's the issue. Are there steps I might have missed as far as giving the user access to DynamoDB? Has the process changed with AWS Cognito's new beta user pool system? EDIT: I looked up more information about registration and login for developer authenticated identities and found another example: https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoSync-Sample/Swift/CognitoSyncDemo I also saw an example of an explicit sign-in method which led me to believe I might need to call getIdentitiyID() in order to sign in: Integrating Congnito User Pools with Amazon Cognito Identity Based on these examples, I changed my login method like so: func attemptLogin(){ if locked { return } trimRegistrationValues() let name = usernameField.text! let user = pool!.getUser(name) lock() user.getSession(name, password: passwordField.text!, validationData: nil, scopes: nil).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task:AWSTask!) -> AnyObject! in if task.error != nil { self.sendErrorPopup("ERROR: Unable to sign in. Error description: " + task.error!.description) } else { print("Successful Login") var logins = [NSObject : AnyObject]() if let prevLogins = self.credentialsProvider!.logins{ logins = prevLogins } logins[USER_POOL_NAME] = name self.credentialsProvider!.logins = logins //self.credentialsProvider!.refresh() //this method doesn't exist self.credentialsProvider!.getIdentityId().continueWithBlock { (task: AWSTask!) -> AnyObject! in if (task.error != nil) { print("ERROR: Unable to get ID. Error description: " + task.error!.description) } else { print("Signed in user with the following ID:") print(task.result) } return nil } dispatch_async(dispatch_get_main_queue()){ self.performSegueWithIdentifier("mainViewControllerSegue", sender: self) } } self.unlock() return nil }) } However, based on my breakpoints, the program doesn't seem to advance past the line: self.credentialsProvider!.logins = logins and the app just hangs. The value of USER_POOL_NAME is the "App name" value from my user pools, and I'm unsure if this is the correct value. I also noticed that the way I set logins is deprecated, though I would like to have a working version of my app that can access the database before I go about fixing deprecation warnings. How can I fix the new issue of the app hanging? Is the place or the way in which I set the logins value incorrect?

Aucun commentaire:

Enregistrer un commentaire