Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWSCognito and AWSS3 "Invalid login token." #5450

Closed
MarcelHaldimann opened this issue Oct 15, 2024 · 5 comments
Closed

AWSCognito and AWSS3 "Invalid login token." #5450

MarcelHaldimann opened this issue Oct 15, 2024 · 5 comments
Labels
auth question General question

Comments

@MarcelHaldimann
Copy link

MarcelHaldimann commented Oct 15, 2024

State your question

I'm trying to get AWS Cognito up and running here. We have been using Cognito for many years and used a very old library from the Objective-C library which no longer works. Maybe I'm just doing something wrong, or I don't understand something correctly.

Perhaps anyone can point me in the right direction.

In general, I would like to create temporary access credentials for the users in the app. The users are assigned a special authorisation which gives them access to certain functions in the S3.

We have implemented the ‘GetOpenIdTokenForDeveloperIdentity’ on our server. This works without any problems, we receive a correct response from Cognito with IdentityId and token.

{
   "IdentityId": "string",
   "Token": "string"
}

https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetOpenIdTokenForDeveloperIdentity.html

The ‘documentation’ now states that we have to implement our own IdentityProvider.

https://docs.aws.amazon.com/cognito/latest/developerguide/developer-authenticated-identities.html

I have done this and return the - (AWSTask <NSString*> *) token { my token which I receive from our backend.

- (AWSTask <NSString*> *) token {

    // My Backend implementation to get the token. 

    // Set the identity id and return the token
    self.identityId = response.identityId;
    return [AWSTask taskWithResult:response.token];
}

The rest is implemented like this:

    // this is a AWSCognitoCredentialsProviderHelper with the `token` method imeplementation
    ILPS3IdentityProvider *ilpIdentityProvider = [[ILPS3IdentityProvider alloc]
                                                  initWithRegionType: self.cognitoRegion
                                                  identityPoolId: self.apiKey // apiKey bad naming of var. it is: *-*-1:*-ddf0-*-9f8c-*
                                                  useEnhancedFlow:NO
                                                  identityProviderManager: nil ];
   
    
    self.ilpCredentialsProvider = [[AWSCognitoCredentialsProvider alloc]
                                                          initWithRegionType: self.cognitoRegion
                                                          identityPoolId: self.apiKey
                                                          unauthRoleArn: nil
                                                          authRoleArn: self.awsRole
                                                          identityProviderManager: ilpIdentityProvider];

I then instantiated an AWSS3TransferUtility:

    AWSServiceConfiguration *configuration = [[[AWSServiceConfiguration alloc] initWithRegion:self.s3Region credentialsProvider:     ilpCredentialsProvider] autorelease];
    
    AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
    
    [AWSS3 registerS3WithConfiguration: configuration forKey: S3_SERVICE_KEY];
    [AWSS3TransferUtility registerS3TransferUtilityWithConfiguration: configuration forKey: S3_SERVICE_KEY];
    
    self.transferManager = [AWSS3TransferUtility S3TransferUtilityForKey: S3_SERVICE_KEY];

And if we now try to upload something to the cloud with the AWSS3TransferUtility, the following error messages appear:

[AWSConnector] Could not retrieve CognitoIdentity: Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=8 "(null)" UserInfo={__type=NotAuthorizedException, message=Invalid login token. Can't pass in a Cognito token.}

Error fetching credentials: Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=8 "(null)" UserInfo={__type=NotAuthorizedException, message=Invalid login token. Can't pass in a Cognito token.}

Somehow I have the feeling that I have to log in again with the token to ‘cognito-identity.amazonaws.com’ with the token? Could that be? If so, can someone show me an example or a part of the documentation?

Which AWS Services are you utilizing?

AWSCognito
AWSS3

Environment(please complete the following information):

  • SDK Version: 2.37.2
  • Dependency Manager: Non, XCFramework setup
  • Swift Version : Objective-C

Device Information (please complete the following information):

  • Device: Simulator
  • iOS Version: iOS 17.5 and 18.0
  • Specific to simulators: iPad Pro 13" M4

I have done my best to find my way around the documentary but unfortunately it still doesn't work.

Thank you very much
Marcel

@github-actions github-actions bot added pending-triage Issue is pending triage pending-maintainer-response Issue is pending response from an Amplify team member labels Oct 15, 2024
@harsh62
Copy link
Member

harsh62 commented Oct 15, 2024

@MarcelHaldimann Would you be able to share verbose logs of the issue.

Verbose logging can be enabled using the following code. This would help us determine where the problem possibly could be.

            AWSDDLog.sharedInstance.add(AWSDDTTYLogger())
            AWSDDLog.sharedInstance.logLevel = .verbose

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending response from an Amplify team member label Oct 15, 2024
@ruisebas
Copy link
Member

Hey @MarcelHaldimann , it seems you're using the wrong initializer for AWSCognitoCredentialsProvider, which results in your custom identity provider being wrapped inside another AWSCognitoCredentialsProviderHelper instance and leading to unexpected behaviour.

Instead, you should just use AWSCognitoCredentialsProvider initWithRegionType:identityProvider:.

self.ilpCredentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType: self.cognitoRegion
                                                                       identityProvider: ilpIdentityProvider];

Give it a try and if it still doesn't work, please provide the verbose logs as requested above.

Thanks!

@ruisebas ruisebas added question General question auth and removed pending-triage Issue is pending triage labels Oct 18, 2024
@MarcelHaldimann
Copy link
Author

@ruisebas @harsh62

Thank you for your feedback. As soon as I have time I will test it and provide the logs!

@github-actions github-actions bot added the pending-maintainer-response Issue is pending response from an Amplify team member label Oct 21, 2024
@ruisebas ruisebas added pending-community-response Issue is pending response from the issue requestor and removed pending-maintainer-response Issue is pending response from an Amplify team member labels Oct 21, 2024
@MarcelHaldimann
Copy link
Author

@ruisebas @harsh62

I tested it this week. At first glance, it now seems to work. I will close the issue. If I run into problems I will open it again or create a new one.

Many thanks!

@github-actions github-actions bot removed the pending-community-response Issue is pending response from the issue requestor label Nov 1, 2024
Copy link

github-actions bot commented Nov 1, 2024

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth question General question
Projects
None yet
Development

No branches or pull requests

3 participants