amplify-swift: Unexpected error occurred with message: Could not retreive AWS credentials, credential value is nil or empty.

Describe the bug

Hello,

Currently in our app we are receiving errors logged from users who seem to be running into this very random auth issue:

APIError: Failed to retrieve authorization token.
Caused by:
AuthError: Unexpected error occurred with message: Could not retreive AWS credentials, credential value is nil or empty.
Recovery suggestion: This should not happen. There is a possibility that there is a bug if this error persists. Please take a look at https://github.com/aws-amplify/amplify-ios/issues to see if there are any existing issues that match your scenario, and file an issue with the details of the bug if there isn't. Issue encountered at:
file: /Users/username/Library/Developer/Xcode/DerivedData/Serenity-ammhovqgzfuogudxbhivmwhannha/SourcePackages/checkouts/amplify-ios/Amplify/Categories/Auth/Error/AuthError.swift
function: recoverySuggestion
line: 80

This is effecting 1/3rd of our user base and these users are unable to make calls using the API category while logged in through the Auth category. Our Auth is using Cognito user pools and hasn’t given us much of a headache until this issue started to pop up. From the error’s being returned in our error reporting service (Sentry) an API call isn’t even made I assume toAmplifyAWSCredentials() throws and stops execution when trying to make the network call.

I’ve tried countless times to reproduce this issue myself but have been unable to do so. After looking through the Amplify swift package the issue might have to do with the toAmplifyAWSCredentials() function as accessKey and secretKey seem to be nil in this specific case. What I couldn’t figure out is if the code path to toAmplifyAWSCredentials() lies within AuthorizationProviderAdapter+SignedInSession or AuthorizationProviderAdapter+SignedOutSession. What is the difference between these two? Any additional information here would be great so I can figure out how the accessKey and secretKey aren’t getting set. If there’s any additional information I can provide to help track down this issue please do let me know!

As asked for in the Discord:

Configuration File (NOTE: Sign in with Apple is currently included in our awsconfiguration but is not currently in use):

{
    "UserAgent": "aws-amplify/cli",
    "Version": "0.1.0",
    "IdentityManager": {
        "Default": {}
    },
    "CredentialsProvider": {
        "CognitoIdentity": {
            "Default": {
                "PoolId": "us-west-1:{REDACTED}",
                "Region": "us-west-1"
            }
        }
    },
    "CognitoUserPool": {
        "Default": {
            "PoolId": "us-west-1_{REDACTED}",
            "AppClientId": "{REDACTED}",
            "Region": "us-west-1"
        }
    },
    "Auth": {
        "Default": {
            "OAuth": {
                "WebDomain": "{REDACTED}",
                "AppClientId": "{REDACTED}",
                "SignInRedirectURI": "{REDACTED}",
                "SignOutRedirectURI": "{REDACTED}",
                "Scopes": [
                    "phone",
                    "email",
                    "openid",
                    "profile",
                    "aws.cognito.signin.user.admin"
                ]
            },
            "authenticationFlowType": "USER_SRP_AUTH",
            "socialProviders": [
                "APPLE"
            ],
            "usernameAttributes": [
                "EMAIL"
            ],
            "signupAttributes": [
                "EMAIL"
            ],
            "passwordProtectionSettings": {
                "passwordPolicyMinLength": 8,
                "passwordPolicyCharacters": []
            },
            "mfaConfiguration": "OFF",
            "mfaTypes": [
                "SMS"
            ],
            "verificationMechanisms": [
                "EMAIL"
            ]
        }
    },
    "AppSync": {
        "Default": {
            "ApiUrl": "{REDACTED}",
            "Region": "us-west-1",
            "AuthMode": "AMAZON_COGNITO_USER_POOLS",
            "ClientDatabasePrefix": "serenity_AMAZON_COGNITO_USER_POOLS"
        },
        "serenity_AWS_IAM": {
            "ApiUrl": "{REDACTED}",
            "Region": "us-west-1",
            "AuthMode": "AWS_IAM",
            "ClientDatabasePrefix": "serenity_AWS_IAM"
        }
    },
    "S3TransferUtility": {
        "Default": {
            "Bucket": "{REDACTED}",
            "Region": "us-west-1"
        }
    },
    "PinpointAnalytics": {
        "Default": {
            "AppId": "{REDACTED}",
            "Region": "us-west-2"
        }
    },
    "PinpointTargeting": {
        "Default": {
            "Region": "us-west-2"
        }
    }
}

Steps To Reproduce

N/A

Expected behavior

User is able to make API calls through the API Category while authenticated with the Auth Category

Amplify Framework Version

1.28.2

Amplify Categories

API, Auth

Dependency manager

Swift PM

Swift version

5.6

CLI version

10.4.0

Xcode version

14.0

Relevant log output

APIError: Failed to retrieve authorization token.
Caused by:
AuthError: Unexpected error occurred with message: Could not retreive AWS credentials, credential value is nil or empty.
Recovery suggestion: This should not happen. There is a possibility that there is a bug if this error persists. Please take a look at https://github.com/aws-amplify/amplify-ios/issues to see if there are any existing issues that match your scenario, and file an issue with the details of the bug if there isn't. Issue encountered at:
file: /Users/ethanthomas/Library/Developer/Xcode/DerivedData/Serenity-ammhovqgzfuogudxbhivmwhannha/SourcePackages/checkouts/amplify-ios/Amplify/Categories/Auth/Error/AuthError.swift
function: recoverySuggestion
line: 80

Is this a regression?

No

Regression additional context

No response

Device

iPhone SE (2nd generation)

iOS Version

16.0.3

Specific to simulators

No response

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

Also after some searching it looks like this is the exact problem I’m running into: https://github.com/aws-amplify/amplify-swift/issues/1501 but this seems to be only for the Storage category and not Auth/API