Basic authentication with http and websocket

1,174 views
Skip to first unread message

Norbert Roos

unread,
Jul 13, 2011, 12:09:50 PM7/13/11
to golan...@googlegroups.com
Hi,

it's me again with another authentication problem.

While i have seen that basic authentication in http is implemented, i'm
having problems to use it.

First step is to create a go http server which requests basic
authentication of the client. As far as i've seen there is no switch or
something like that to enable that. I experimented with calling
http.Error(rw, http.StatusUnauthorized), but this didn't help. Is basic
authentication actually supported on the server side?

Second step is to try this with a websocket connection, client as well
as server. Could someone tell me if experimenting with this could lead
to a success, before i put time into the experiments?

Another thing is generating http request where the server requires basic
authentication. There are these handy functions like http.Get(), but i'm
afraid i can't use them and i have to create a request on my own and set
the authentication with SetBasicAuth()?

bye
Norbert

Brad Fitzpatrick

unread,
Jul 13, 2011, 12:21:35 PM7/13/11
to Norbert Roos, golan...@googlegroups.com
On Wed, Jul 13, 2011 at 9:09 AM, Norbert Roos <nr...@webware-experts.de> wrote:
Hi,

it's me again with another authentication problem.

While i have seen that basic authentication in http is implemented, i'm having problems to use it.

First step is to create a go http server which requests basic authentication of the client. As far as i've seen there is no switch or something like that to enable that. I experimented with calling http.Error(rw, http.StatusUnauthorized), but this didn't help. Is basic authentication actually supported on the server side?

It's not automatic, but it's two lines to trigger the client getting the prompt, and a couple more to check the password.  I did it here:

 
That was some of my first Go code and I should probably clean it up and use the new base64 decoding helpers, but you can see the basics there.

Another thing is generating http request where the server requires basic authentication. There are these handy functions like http.Get(), but i'm afraid i can't use them and i have to create a request on my own and set the authentication with SetBasicAuth()?

just:

req, err := http.NewRequest("GET", url, nil)
req.SetBasicAuth(...)
res, err := http.DefaultClient.Do(req)


Norbert Roos

unread,
Jul 13, 2011, 12:53:15 PM7/13/11
to golan...@googlegroups.com
On 07/13/2011 06:21 PM, Brad Fitzpatrick wrote:

> It's not automatic, but it's two lines to trigger the client getting the
> prompt, and a couple more to check the password. I did it here:

Oh great, this is useful help!

Norbert

Reply all
Reply to author
Forward
0 new messages