Skip to content

Commit

Permalink
Return WWW-Authenticate header
Browse files Browse the repository at this point in the history
On invalide credentials return WWW-Authenticate the same way btcd does.

Pointed out by @davec in #14
  • Loading branch information
jcvernaleo committed Feb 1, 2016
1 parent 515cbc6 commit aec660c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions rpc/legacyrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ type Server struct {
requestShutdownChan chan struct{}
}

// jsonAuthFail sends a message back to the client if the http auth is rejected.
func jsonAuthFail(w http.ResponseWriter) {
w.Header().Add("WWW-Authenticate", `Basic realm="btcwallet RPC"`)
http.Error(w, "401 Unauthorized.", http.StatusUnauthorized)
}

// NewServer creates a new server for serving legacy RPC client connections,
// both HTTP POST and websocket.
func NewServer(opts *Options, walletLoader *wallet.Loader, listeners []net.Listener) *Server {
Expand Down Expand Up @@ -162,7 +168,7 @@ func NewServer(opts *Options, walletLoader *wallet.Loader, listeners []net.Liste

if err := server.checkAuthHeader(r); err != nil {
log.Warnf("Unauthorized client connection attempt")
http.Error(w, "401 Unauthorized.", http.StatusUnauthorized)
jsonAuthFail(w)
return
}
server.wg.Add(1)
Expand All @@ -183,7 +189,7 @@ func NewServer(opts *Options, walletLoader *wallet.Loader, listeners []net.Liste
// being missing, immediately terminate the connection.
log.Warnf("Disconnecting improperly authorized " +
"websocket client")
http.Error(w, "401 Unauthorized.", http.StatusUnauthorized)
jsonAuthFail(w)
return
}

Expand Down

0 comments on commit aec660c

Please sign in to comment.