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

Can't connect to websocket with authorization header #39

Closed
cleverClosure opened this issue Apr 24, 2019 · 21 comments
Closed

Can't connect to websocket with authorization header #39

cleverClosure opened this issue Apr 24, 2019 · 21 comments

Comments

@cleverClosure
Copy link

cleverClosure commented Apr 24, 2019

Hi. I get an error while trying to connect to websocket, here is the code:

let request = NSURLRequest(url: url)
socketClient.openSocketWithURLRequest(request: request, delegate: self, connectionHeaders:["Authorization": "Bearer \(token)"])

Response:

received bad response code from server 401

Do you have any ideas why that might be the case?

@WrathChaos
Copy link
Owner

Hello @cleverClosure ,
I need an example to test it out. Can you provide it?

@cleverClosure
Copy link
Author

Can I send it to you privately somehow? I don't want the url to be public

@WrathChaos
Copy link
Owner

You can send me an email : [email protected]

@cleverClosure
Copy link
Author

I've sent you an email

@WrathChaos
Copy link
Owner

Hello again @cleverClosure,
As I mentioned on the mail, I got this error from your server:
didFailWithError: Optional(Error Domain=SRWebSocketErrorDomain Code=2133 "Invalid Sec-WebSocket-Accept response" UserInfo={NSLocalizedDescription=Invalid Sec-WebSocket-Accept response})

Can you provide a working authentication token or please check your server part :)

@cleverClosure
Copy link
Author

cleverClosure commented Apr 27, 2019

Yes, that is exactly what I get when I use ws instead of wss or https. Here is the new token: 451ac7a5-ad93-4859-8c62-b41e96d6a34d

@cleverClosure
Copy link
Author

The server side works, because I am able to connect to it from the web client

@WrathChaos
Copy link
Owner

@cleverClosure
Have you checked these issues?
#30 and #32 ?

@cleverClosure
Copy link
Author

The webserver works on sockJS but adding /websocket to the url didn't help

@WrathChaos
Copy link
Owner

@cleverClosure I'm really sorry but I could not find the problem about your issue :(
I've tried another websocket with authentication and it works without any issue. I have only left is that your websocket backend might have an issue about it.

@cleverClosure
Copy link
Author

Can I ask you which websocket with auth did you try (the one you were able to connect successfully)? Is that local webserver or something?

@WrathChaos
Copy link
Owner

I've tried on a very simple local webserver. Unfortunately, I do not have any other options.

@said13
Copy link

said13 commented May 17, 2019

Someone solved this problem?
I have the same issue.

@cedb777
Copy link

cedb777 commented Jul 18, 2019

Same issue! Someone solved this ?

@WrathChaos
Copy link
Owner

@said13 and @cedb777, Can you guys provide me with an example of the issue? Because I could not reproduce it on my own simple WebSocket server.

@iOSDias
Copy link

iOSDias commented Jul 22, 2019

@cedb777 @cleverClosure
I also can not to send headers through connectionHeaders, but I send it in request, like this

    var socketRequest: NSURLRequest {
        let webSocket = URL(string: urlString)!
        var request = URLRequest(url: webSocket)
            request.setValue(("Bearer " + token).toBase64, forHTTPHeaderField: "Authorization")
        return request as NSURLRequest
    }

@said13
Copy link

said13 commented Jul 22, 2019

@iOSDias Hi! It works?

@cleverClosure
Copy link
Author

cleverClosure commented Jul 22, 2019

It worked for me with this code:
` let url = URL(string: "wss://(defaultSocketBase)/push?access_token=(token)")!

    let request = NSURLRequest(url: url)

    let headers: [String: String] = ["heart-beat": "20,20"]

    socketClient.openSocketWithURLRequest(request: request, delegate: self, connectionHeaders: headers)`

@iOSDias
Copy link

iOSDias commented Jul 22, 2019

@said13 yes

@WrathChaos
Copy link
Owner

Finally, I closed the issue :) Please open a new one if you still have a problem.

@jbarros35
Copy link

@cedb777 @cleverClosure
I also can not to send headers through connectionHeaders, but I send it in request, like this

    var socketRequest: NSURLRequest {
        let webSocket = URL(string: urlString)!
        var request = URLRequest(url: webSocket)
            request.setValue(("Bearer " + token).toBase64, forHTTPHeaderField: "Authorization")
        return request as NSURLRequest
    }

For the ones who could not make it using headers on connection, try this, that approach works on my environment.

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

No branches or pull requests

6 participants