Skip to content

Commit

Permalink
Added Proxy Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
rhinoman committed Jan 28, 2016
1 parent e867fc7 commit f70a097
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
32 changes: 31 additions & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"
"net/http"
"strings"
)

//Basic interface for Auth
Expand Down Expand Up @@ -36,6 +37,13 @@ type CookieAuth struct {
UpdatedAuthToken string
}

//Proxy authentication
type ProxyAuth struct {
Username string
Roles []string
AuthToken string
}

//Adds Basic Authentication headers to an http request
func (ba *BasicAuth) AddAuthHeaders(req *http.Request) {
authString := []byte(ba.Username + ":" + ba.Password)
Expand All @@ -55,6 +63,15 @@ func (ca *CookieAuth) AddAuthHeaders(req *http.Request) {
req.Header.Set("X-CouchDB-WWW-Authenticate", "Cookie")
}

func (pa *ProxyAuth) AddAuthHeaders(req *http.Request) {
req.Header.Set("X-Auth-CouchDB-Username", pa.Username)
rolesString := strings.Join(pa.Roles, ",")
req.Header.Set("X-Auth-CouchDB-Roles", rolesString)
if pa.AuthToken != "" {
req.Header.Set("X-Auth-CouchDB-Token", pa.AuthToken)
}
}

//Update Auth Data
//If couchdb generates a new token, place it in a separate field so that
//it is available to an application
Expand All @@ -74,6 +91,9 @@ func (ca *CookieAuth) updateAuth(resp *http.Response) {
//do nothing for pass through
func (pta *PassThroughAuth) updateAuth(resp *http.Response) {}

//do nothing for proxy auth
func (pa *ProxyAuth) updateAuth(resp *http.Response) {}

//Get Updated Auth
//Does nothing for BasicAuth
func (ba *BasicAuth) GetUpdatedAuth() map[string]string {
Expand All @@ -94,6 +114,11 @@ func (ca *CookieAuth) GetUpdatedAuth() map[string]string {
return am
}

//do nothing for Proxy Auth
func (pa *ProxyAuth) GetUpdatedAuth() map[string]string {
return nil
}

//Return a Debug string

func (ba *BasicAuth) DebugString() string {
Expand All @@ -109,5 +134,10 @@ func (ca *CookieAuth) DebugString() string {
ca.AuthToken, ca.UpdatedAuthToken)
}

func (pa *ProxyAuth) DebugString() string {
return fmt.Sprintf("Username: %v, Roles: %v, AuthToken: %v",
pa.Username, pa.Roles, pa.AuthToken)
}

//TODO: Add support for other Authentication methods supported by Couch:
//OAuth, Proxy, etc.
//OAuth, etc.
21 changes: 20 additions & 1 deletion connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,26 @@ func TestBasicAuth(t *testing.T) {
} else if resp == nil {
t.Logf("Response was nil")
t.Fail()
} else {
}
}

func TestProxyAuth(t *testing.T) {
client := &http.Client{}
pAuth := ProxyAuth{
Username: "adminuser",
Roles: []string{"admin", "master", "_admin"},
}
c := connection{
url: serverUrl,
client: client,
}
resp, err := c.request("GET", "/", nil, nil, &pAuth)
if err != nil {
t.Logf("Error: %v", err)
t.Fail()
} else if resp == nil {
t.Logf("Response was nil")
t.Fail()
}
}

Expand Down

0 comments on commit f70a097

Please sign in to comment.