stream-chat-swift icon indicating copy to clipboard operation
stream-chat-swift copied to clipboard

Passing an Invalid Token during connection triggers an infinity loop.

Open fceldeiro opened this issue 1 year ago • 2 comments

What did you do?

By accident I passed a token provider which returned a token generated by another secret and this triggered an infinite error loop that cannot be stopped.

What did you expect to happen?

It should fail once and stop.

What happened instead?

My testing code is pretty straightforward

 private func connectUser() async throws {
        
        
        let url = "https://xxx.ngrok-free.app/v1/chat/token"
        let result: TokenResponse = try await network.request(method: .GET(queryItems: nil), fullURL: url, authenticated: true)
        
        return try await withCheckedThrowingContinuation { continuation in
            
            chatClient.connectUser(userInfo: .init(id: user.internalId)) { tokenProviderCallback in
              
                    Task {
                        do {
                            let result: TokenResponse = try await network.request(method: .GET(queryItems: nil), fullURL: url, authenticated: true)
                            tokenProviderCallback(.success(.init(stringLiteral: result.token)))
                        } catch {
                            tokenProviderCallback(.failure(error))
                        }
                    }
            } completion: { error in
                if let error = error {
                    continuation.resume(throwing: error)
                } else {
                    continuation.resume()
                }
            }
        }
    }

Here is an example of my logs. It keeps going forever.

Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse

2024-02-15 16:20:31.612 [ERROR] [io.getstream.logger] [AuthenticationRepository.swift:215] [prepareEnvironment(userInfo:newToken:)] > Assert failed: The id of the retrieved token should match the user information passed to connect
Connection 7: received failure notification
nw_read_request_report [C7] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C7 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C7 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C7] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 8: received failure notification
nw_read_request_report [C8] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C8 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C8 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C8] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 9: received failure notification
nw_read_request_report [C9] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C9 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C9 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C9] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 10: received failure notification
nw_read_request_report [C10] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C10 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C10 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C10] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 11: received failure notification
nw_read_request_report [C11] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C11 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C11 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C11] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 12: received failure notification
nw_read_request_report [C12] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C12 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C12 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C12] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 13: received failure notification
nw_read_request_report [C13] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C13 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C13 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C13] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 14: received failure notification
nw_read_request_report [C14] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C14 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C14 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C14] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 15: received failure notification
nw_read_request_report [C15] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C15 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C15 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C15] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 16: received failure notification
nw_read_request_report [C16] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C16 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C16 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C16] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 17: received failure notification
nw_read_request_report [C17] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C17 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C17 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C17] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 18: received failure notification
nw_read_request_report [C18] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C18 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C18 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C18] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 19: received failure notification
nw_flow_add_write_request [C19 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C19] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 20: received failure notification
nw_read_request_report [C20] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C20 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C20 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C20] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 21: received failure notification
nw_read_request_report [C21] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C21 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C21 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C21] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 22: received failure notification
nw_read_request_report [C22] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C22 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C22 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C22] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 23: received failure notification
nw_read_request_report [C23] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C23 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C23 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C23] Send failed with error "Socket is not connected"
Request: get https://xxx.ngrok-free.app/v1/chat/token isAuthenticated: true
Request get https://xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 24: received failure notification
nw_read_request_report [C24] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C24 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C24 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C24] Send failed with error "Socket is not connected"
Request: get https://xxx/v1/chat/token isAuthenticated: true
2024-02-15 16:20:41.892 [ERROR] [io.getstream.logger] [AuthenticationRepository.swift:320] [getToken(isRetry:userInfo:tokenProvider:completion:)] > Error when getting token: Error ConnectionNotSuccessful in stream-chat-swift/Sources/StreamChat/Repositories/ConnectionRepository.swift:73 -> Connection to the API has failed.
You can read more about making a successful connection in our docs:
https://getstream.io/chat/docs/sdk/ios/uikit/getting-started/


API Error: Optional("Error WebSocket in SourcePackages/checkouts/stream-chat-swift/Sources/StreamChat/WebSocketClient/WebSocketClient.swift:212 -> ServerErrorPayload(code: 43, message: \"JWTAuth error: signature is not valid. Make sure the token is created using the secret for API key \"xxx\"\", statusCode: 401)).")
Request get https://0xxx.ngrok-free.app/v1/chat/token completed. TokenResponse
Connection 25: received failure notification
nw_read_request_report [C25] Receive failed with error "Socket is not connected"
nw_flow_service_reads [C25 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] No output handler
nw_flow_add_write_request [C25 52.29.247.237:443 failed parent-flow (satisfied (Path is satisfied), interface: en0[802.11], uses wifi)] cannot accept write requests
nw_write_request_report [C25] Send failed with error "Socket is not connected"
2024-02-15 16:20:42.398 [ERROR] [io.getstream.logger] [AuthenticationRepository.swift:320] [getToken(isRetry:userInfo:tokenProvider:completion:)] > Error when getting token: Error ConnectionNotSuccessful in SourcePackages/checkouts/stream-chat-swift/Sources/StreamChat/Repositories/ConnectionRepository.swift:73 -> Connection to the API has failed.
You can read more about making a successful connection in our docs:
https://getstream.io/chat/docs/sdk/ios/uikit/getting-started/


API Error: Optional("Error WebSocket in WebSocketClient.swift:212 -> ServerErrorPayload(code: 43, message: \"JWTAuth error: signature is not valid. Make sure the token is created using the secret for API key \"xxxx\"\", statusCode: 401)).")

GetStream Environment

GetStream Chat version: 4.48.0 GetStream Chat frameworks: StreamChat, StreamChatSwiftUI iOS version: 17.2 Swift version: 5.x Xcode version: 15.2 Device: iOS Simulator 15 Pro

fceldeiro avatar Feb 15 '24 15:02 fceldeiro

Hi @fceldeiro,

Thank you for the report, we will investigate as soon as possible 👍

Best, Nuno

nuno-vieira avatar Feb 15 '24 16:02 nuno-vieira

Hello @fceldeiro !

Just to let you know, the fix will come on the next release. We will let you know once it is published.

Thanks for the report!

Best, Nuno

nuno-vieira avatar Mar 04 '24 15:03 nuno-vieira

Hey @fceldeiro,

This fix has been released as part of 4.50.0

Best, Alexey

testableapple avatar Mar 12 '24 13:03 testableapple