mirror of
https://github.com/hanzoai/oxy.git
synced 2026-09-30 23:42:31 +00:00
Add parsing basic auth
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type BasicAuth struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (ba *BasicAuth) String() string {
|
||||
encoded := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", ba.Username, ba.Password)))
|
||||
return fmt.Sprintf("Basic %s", encoded)
|
||||
}
|
||||
|
||||
func ParseAuthHeader(header string) (*BasicAuth, error) {
|
||||
values := strings.Fields(header)
|
||||
if len(values) != 2 {
|
||||
return nil, fmt.Errorf(fmt.Sprintf("Failed to parse header '%s'", header))
|
||||
}
|
||||
|
||||
auth_type := strings.ToLower(values[0])
|
||||
if auth_type != "basic" {
|
||||
return nil, fmt.Errorf("Expected basic auth type, got '%s'", auth_type)
|
||||
}
|
||||
|
||||
encoded_string := values[1]
|
||||
decoded_string, err := base64.StdEncoding.DecodeString(encoded_string)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to parse header '%s', base64 failed: %s", header, err)
|
||||
}
|
||||
|
||||
values = strings.SplitN(string(decoded_string), ":", 2)
|
||||
if len(values) != 2 {
|
||||
return nil, fmt.Errorf("Failed to parse header '%s', expected separator ':'", header)
|
||||
}
|
||||
return &BasicAuth{Username: values[0], Password: values[1]}, nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
type AuthSuite struct {
|
||||
}
|
||||
|
||||
var _ = Suite(&AuthSuite{})
|
||||
|
||||
//Just to make sure we don't panic, return err and not
|
||||
//username and pass and cover the function
|
||||
func (s *AuthSuite) TestParseBadHeaders(c *C) {
|
||||
headers := []string{
|
||||
//just empty string
|
||||
"",
|
||||
//missing auth type
|
||||
"justplainstring",
|
||||
//unknown auth type
|
||||
"Whut justplainstring",
|
||||
//invalid base64
|
||||
"Basic Shmasic",
|
||||
//random encoded string
|
||||
"Basic YW55IGNhcm5hbCBwbGVhcw==",
|
||||
}
|
||||
for _, h := range headers {
|
||||
_, err := ParseAuthHeader(h)
|
||||
c.Assert(err, NotNil)
|
||||
}
|
||||
}
|
||||
|
||||
//Just to make sure we don't panic, return err and not
|
||||
//username and pass and cover the function
|
||||
func (s *AuthSuite) TestParseSuccess(c *C) {
|
||||
headers := []struct {
|
||||
Header string
|
||||
Expected BasicAuth
|
||||
}{
|
||||
{
|
||||
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
|
||||
BasicAuth{Username: "Aladdin", Password: "open sesame"},
|
||||
},
|
||||
// Make sure that String() produces valid header
|
||||
{
|
||||
(&BasicAuth{Username: "Alice", Password: "Here's bob"}).String(),
|
||||
BasicAuth{Username: "Alice", Password: "Here's bob"},
|
||||
},
|
||||
//empty pass
|
||||
{
|
||||
"Basic QWxhZGRpbjo=",
|
||||
BasicAuth{Username: "Aladdin", Password: ""},
|
||||
},
|
||||
}
|
||||
for _, h := range headers {
|
||||
request, err := ParseAuthHeader(h.Header)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(request.Username, Equals, h.Expected.Username)
|
||||
c.Assert(request.Password, Equals, h.Expected.Password)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user