Skip to content

Commit

Permalink
couchdb: fix proxy auth
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl committed May 22, 2014
1 parent 1f204db commit 1ead3a3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions auth.go
@@ -1,9 +1,11 @@
package couchdb

import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"
)
Expand Down Expand Up @@ -34,9 +36,9 @@ type proxyauth struct {
func ProxyAuth(username string, roles []string, secret string) Auth {
pa := &proxyauth{username, strings.Join(roles, ","), ""}
if secret != "" {
hash := sha1.New()
hash.Write([]byte(secret + username))
pa.tok = fmt.Sprintf("%x", hash.Sum(nil))
mac := hmac.New(sha1.New, []byte(secret))
io.WriteString(mac, username)
pa.tok = fmt.Sprintf("%x", mac.Sum(nil))
}
return pa
}
Expand Down
2 changes: 1 addition & 1 deletion auth_test.go
Expand Up @@ -44,7 +44,7 @@ func TestProxyAuthWithToken(t *testing.T) {
expected := http.Header{
"X-Auth-Couchdb-Username": {"user"},
"X-Auth-Couchdb-Roles": {"role1,role2"},
"X-Auth-Couchdb-Token": {"0eefaf44991ac21b04262ada13c1ec5f01b71596"},
"X-Auth-Couchdb-Token": {"027da48c8c642ca4c58eb982eec81915179e77a3"},
}
check(t, "req headers", expected, req.Header)
}

0 comments on commit 1ead3a3

Please sign in to comment.