aws-amplify / aws-sdk-ios

AWS SDK for iOS. For more information, see our web site:
https://aws-amplify.github.io/docs
Other
1.68k stars 885 forks source link

Cannot make any API request after 3 days #2229

Closed F9 closed 4 years ago

F9 commented 4 years ago

Hi, I'm currently using:

Currently everything works perfect at first day:

After 3 days (not exactly this time, but it's to give some advice) the app stops to make auth request and every response in 403.

The strange thing is that I can perform a getUser correctly and get a new token using the getSession method.

I've tried everything but I cannot solve this problem.

Currently using 2.12.5 version. The bug seems related to the fact that with social login (hostedUI) after some days the login is requested back.

royjit commented 4 years ago

Can you provide some code sample on how you are authenticating a user? Are you using AWSMobileClient?

F9 commented 4 years ago

Yes I'm using AWSMobileClient.

For login with email I use this code:

AWSMobileClient.default().signIn(username:user, password:password) { (signInResult, error) in
            if let error = error  {
                print("\(error.localizedDescription)")
            } else if let signInResult = signInResult {

            }
        }

Next for making for calls I use the AWSAPIgateway:

let serviceConfiguration = AWSServiceConfiguration(region: AWSRegionType.EUWest1,
                                                           endpoint: AWSEndpoint(region: .EUWest1, service: .APIGateway, url: URL(string: baseURLString)),
                                                           credentialsProvider: AWSMobileClient.default().getCredentialsProvider())

        let configuration = serviceConfiguration!.copy() as! AWSServiceConfiguration

        let signer: AWSSignatureV4Signer = AWSSignatureV4Signer(credentialsProvider: configuration.credentialsProvider, endpoint: configuration.endpoint)

        if let endpoint = configuration.endpoint {
            configuration.baseURL = endpoint.url
        }
        configuration.requestInterceptors = [AWSNetworkingRequestInterceptor(), signer]

        invocationClient.configuration = configuration

I've also tried to use Amplify plugins:

        let apiPlugin = AWSAPIPlugin()
        try! Amplify.add(plugin: apiPlugin)

        try! Amplify.configure()

And make the call in this way:

 let call = Amplify.API.get(request: RESTRequest(apiName: "Default", path: "/XXXXX", headers: nil, queryParameters: nil, body: nil)) { event in

                  switch event {
                  case .completed(let data):
                      print(data)
                  case .failed(let error):
                      print("error \(error)" )
                      switch error {
                      case .httpStatusError(let statuscode, let response):
                          print("res  \(response)")
                      default:
                          print("BOoo")
                      }

                  default:
                      print("MMMMM")
                  }

              }

The fact is that all this code works for some hours (or days, I've not found a precise moment) and next stop working for authorisation problems.

The other strange things is that the initizialize method of AWSMobileClient says "loggedIn"

        AWSMobileClient.default().initialize { (userState, error) in
            if let userState = userState {

                print("UserState: \(userState.rawValue)")
            } else if let error = error {
                print("error: \(error.localizedDescription)")
            }
        }

My issues looks similar to this: https://github.com/aws-amplify/aws-sdk-ios/issues/1548

Thanks for time!

F9 commented 4 years ago

I've partially managed to solve this bug doing this before the first call: credentialsProvider.clearCredentials() It works but I think it's not normal to perform this kind of things. It should be managed directly by the AWS SDK.

allanweir commented 4 years ago

I'm still seeing this issue, after 24 hours of inactivity sometimes calls still work and other times they don't.

F9 commented 4 years ago

Still no solution? I should say stay away from cognito with AmplifySDK to others? it's 2 week from my issue and several week from @allanweir issues!

royjit commented 4 years ago

Apologies for the delayed response. I was not able to reproduce this issue in my side. I am guessing that your credentials expired and you have to sign In again. Are you listening to user state changes -

AWSMobileClient.default().addUserStateListener(self) { (userState, info) in
   switch (userState) {
        ....
        case .signedOut:
                // Ask user to signIn again.
        ....
    }
 }

Also try to use AWSMobileClient.default() as the credentials provider as mentioned here.

allanweir commented 4 years ago

At least on my side, 100% the local credentials have expired but should the library be automatically using the refresh token? On the Cognito setup we have it as a longer expiration time than 3 days for refresh tokens. Our current workaround for persisting login is listening for the AuthState update and in the background processing a login if details are persisted, with Facebook we refresh using their API so for the most part the user isn't noticing a difference now but because we have our own layer of logged in/out it involves a bit of hacky behaviour to keep things smooth.

palpatim commented 4 years ago

Thanks for the update @allanweir. As I'm sure you've already encountered, auth is a subtle and highly interconnected system, so any one of the layers (Cognito, Facebook, your own layer) could introduce behaviors that affect the overall system behavior in surprising ways.

@F9 Please do try to add the state change listener as @royjit mentioned above (https://github.com/aws-amplify/aws-sdk-ios/issues/2229#issuecomment-583602603) and let us know if that solves your issue, or provide additional details about how you are responding to state changes.

stale[bot] commented 4 years ago

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

F9 commented 4 years ago

Hi, sorry for the delay but we are working hard to deliver this project. We already have the state change listener, and doesn't look working as you are telling. It's always says "loggedIn", also if the API requests are failing.

We are also trying to implement facebook login using HostedUI, but after a day asks the user to login back automatically. I think it's the same problem about refreshing.