vendor: switch to using go1.11 modules

This commit is contained in:
Nick Craig-Wood 2018-08-28 15:27:07 +01:00
parent 5c75453aba
commit da1682a30e
6142 changed files with 390 additions and 5155875 deletions

View file

@ -1,40 +0,0 @@
package auth
import (
"encoding/base64"
"net/http"
"testing"
)
func TestAuthBasic(t *testing.T) {
secrets := HtpasswdFileProvider("test.htpasswd")
a := &BasicAuth{Realm: "example.com", Secrets: secrets}
r := &http.Request{}
r.Method = "GET"
if a.CheckAuth(r) != "" {
t.Fatal("CheckAuth passed on empty headers")
}
r.Header = http.Header(make(map[string][]string))
r.Header.Set("Authorization", "Digest blabla ololo")
if a.CheckAuth(r) != "" {
t.Fatal("CheckAuth passed on bad headers")
}
r.Header.Set("Authorization", "Basic !@#")
if a.CheckAuth(r) != "" {
t.Fatal("CheckAuth passed on bad base64 data")
}
data := [][]string{
{"test", "hello"},
{"test2", "hello2"},
{"test3", "hello3"},
{"test16", "topsecret"},
}
for _, tc := range data {
auth := base64.StdEncoding.EncodeToString([]byte(tc[0] + ":" + tc[1]))
r.Header.Set("Authorization", "Basic "+auth)
if a.CheckAuth(r) != tc[0] {
t.Fatalf("CheckAuth failed for user '%s'", tc[0])
}
}
}

View file

@ -1,76 +0,0 @@
package auth
import (
"net/http"
"net/url"
"testing"
"time"
)
func TestAuthDigest(t *testing.T) {
secrets := HtdigestFileProvider("test.htdigest")
da := &DigestAuth{Opaque: "U7H+ier3Ae8Skd/g",
Realm: "example.com",
Secrets: secrets,
clients: map[string]*digest_client{}}
r := &http.Request{}
r.Method = "GET"
if u, _ := da.CheckAuth(r); u != "" {
t.Fatal("non-empty auth for empty request header")
}
r.Header = http.Header(make(map[string][]string))
r.Header.Set("Authorization", "Digest blabla")
if u, _ := da.CheckAuth(r); u != "" {
t.Fatal("non-empty auth for bad request header")
}
r.Header.Set("Authorization", `Digest username="test", realm="example.com", nonce="Vb9BP/h81n3GpTTB", uri="/t2", cnonce="NjE4MTM2", nc=00000001, qop="auth", response="ffc357c4eba74773c8687e0bc724c9a3", opaque="U7H+ier3Ae8Skd/g", algorithm="MD5"`)
if u, _ := da.CheckAuth(r); u != "" {
t.Fatal("non-empty auth for unknown client")
}
r.URL, _ = url.Parse("/t2")
da.clients["Vb9BP/h81n3GpTTB"] = &digest_client{nc: 0, last_seen: time.Now().UnixNano()}
if u, _ := da.CheckAuth(r); u != "test" {
t.Fatal("empty auth for legitimate client")
}
// our nc is now 0, client nc is 1
if u, _ := da.CheckAuth(r); u != "" {
t.Fatal("non-empty auth for outdated nc")
}
// try again with nc checking off
da.IgnoreNonceCount = true
if u, _ := da.CheckAuth(r); u != "test" {
t.Fatal("empty auth for outdated nc even though nc checking is off")
}
da.IgnoreNonceCount = false
r.URL, _ = url.Parse("/")
da.clients["Vb9BP/h81n3GpTTB"] = &digest_client{nc: 0, last_seen: time.Now().UnixNano()}
if u, _ := da.CheckAuth(r); u != "" {
t.Fatal("non-empty auth for bad request path")
}
r.URL, _ = url.Parse("/t3")
da.clients["Vb9BP/h81n3GpTTB"] = &digest_client{nc: 0, last_seen: time.Now().UnixNano()}
if u, _ := da.CheckAuth(r); u != "" {
t.Fatal("non-empty auth for bad request path")
}
da.clients["+RbVXSbIoa1SaJk1"] = &digest_client{nc: 0, last_seen: time.Now().UnixNano()}
r.Header.Set("Authorization", `Digest username="test", realm="example.com", nonce="+RbVXSbIoa1SaJk1", uri="/", cnonce="NjE4NDkw", nc=00000001, qop="auth", response="c08918024d7faaabd5424654c4e3ad1c", opaque="U7H+ier3Ae8Skd/g", algorithm="MD5"`)
if u, _ := da.CheckAuth(r); u != "test" {
t.Fatal("empty auth for valid request in subpath")
}
}
func TestDigestAuthParams(t *testing.T) {
const authorization = `Digest username="test", realm="", nonce="FRPnGdb8lvM1UHhi", uri="/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro", algorithm=MD5, response="fdcdd78e5b306ffed343d0ec3967f2e5", opaque="lEgVjogmIar2fg/t", qop=auth, nc=00000001, cnonce="e76b05db27a3b323"`
params := DigestAuthParams(authorization)
want := "/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro"
if params["uri"] != want {
t.Fatalf("failed to parse uri with embedded commas, got %q want %q", params["uri"], want)
}
}

View file

@ -1,35 +0,0 @@
// +build ignore
/*
Example application using Basic auth
Build with:
go build basic.go
*/
package main
import (
auth ".."
"fmt"
"net/http"
)
func Secret(user, realm string) string {
if user == "john" {
// password is "hello"
return "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1"
}
return ""
}
func handle(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
fmt.Fprintf(w, "<html><body><h1>Hello, %s!</h1></body></html>", r.Username)
}
func main() {
authenticator := auth.NewBasicAuthenticator("example.com", Secret)
http.HandleFunc("/", authenticator.Wrap(handle))
http.ListenAndServe(":8080", nil)
}

View file

@ -1,60 +0,0 @@
// +build ignore
/*
Example application using NewContext/FromContext
Build with:
go build context.go
*/
package main
import (
"fmt"
"net/http"
auth ".."
"golang.org/x/net/context"
)
func Secret(user, realm string) string {
if user == "john" {
// password is "hello"
return "b98e16cbc3d01734b264adba7baa3bf9"
}
return ""
}
type ContextHandler interface {
ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request)
}
type ContextHandlerFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request)
func (f ContextHandlerFunc) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) {
f(ctx, w, r)
}
func handle(ctx context.Context, w http.ResponseWriter, r *http.Request) {
authInfo := auth.FromContext(ctx)
authInfo.UpdateHeaders(w.Header())
if authInfo == nil || !authInfo.Authenticated {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
fmt.Fprintf(w, "<html><body><h1>Hello, %s!</h1></body></html>", authInfo.Username)
}
func authenticatedHandler(a auth.AuthenticatorInterface, h ContextHandler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := a.NewContext(context.Background(), r)
h.ServeHTTP(ctx, w, r)
})
}
func main() {
authenticator := auth.NewDigestAuthenticator("example.com", Secret)
http.Handle("/", authenticatedHandler(authenticator, ContextHandlerFunc(handle)))
http.ListenAndServe(":8080", nil)
}

View file

@ -1,35 +0,0 @@
// +build ignore
/*
Example application using Digest auth
Build with:
go build digest.go
*/
package main
import (
auth ".."
"fmt"
"net/http"
)
func Secret(user, realm string) string {
if user == "john" {
// password is "hello"
return "b98e16cbc3d01734b264adba7baa3bf9"
}
return ""
}
func handle(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
fmt.Fprintf(w, "<html><body><h1>Hello, %s!</h1></body></html>", r.Username)
}
func main() {
authenticator := auth.NewDigestAuthenticator("example.com", Secret)
http.HandleFunc("/", authenticator.Wrap(handle))
http.ListenAndServe(":8080", nil)
}

View file

@ -1,36 +0,0 @@
// +build ignore
/*
Example demonstrating how to wrap an application which is unaware of
authenticated requests with a "pass-through" authentication
Build with:
go build wrapped.go
*/
package main
import (
auth ".."
"fmt"
"net/http"
)
func Secret(user, realm string) string {
if user == "john" {
// password is "hello"
return "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1"
}
return ""
}
func regular_handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<html><body><h1>This application is unaware of authentication</h1></body></html>")
}
func main() {
authenticator := auth.NewBasicAuthenticator("example.com", Secret)
http.HandleFunc("/", auth.JustCheck(authenticator, regular_handler))
http.ListenAndServe(":8080", nil)
}

View file

@ -1,19 +0,0 @@
package auth
import "testing"
func Test_MD5Crypt(t *testing.T) {
test_cases := [][]string{
{"apache", "$apr1$J.w5a/..$IW9y6DR0oO/ADuhlMF5/X1"},
{"pass", "$1$YeNsbWdH$wvOF8JdqsoiLix754LTW90"},
{"topsecret", "$apr1$JI4wh3am$AmhephVqLTUyAVpFQeHZC0"},
}
for _, tc := range test_cases {
e := NewMD5Entry(tc[1])
result := MD5Crypt([]byte(tc[0]), e.Salt, e.Magic)
if string(result) != tc[1] {
t.Fatalf("MD5Crypt returned '%s' instead of '%s'", string(result), tc[1])
}
t.Logf("MD5Crypt: '%s' (%s%s$) -> %s", tc[0], e.Magic, e.Salt, result)
}
}

View file

@ -1,37 +0,0 @@
package auth
import (
"reflect"
"testing"
)
func TestH(t *testing.T) {
const hello = "Hello, world!"
const hello_md5 = "6cd3556deb0da54bca060b4c39479839"
h := H(hello)
if h != hello_md5 {
t.Fatal("Incorrect digest for test string:", h, "instead of", hello_md5)
}
}
func TestParsePairs(t *testing.T) {
const header = `username="\test", realm="a \"quoted\" string", nonce="FRPnGdb8lvM1UHhi", uri="/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro", algorithm=MD5, response="fdcdd78e5b306ffed343d0ec3967f2e5", opaque="lEgVjogmIar2fg/t", qop=auth, nc=00000001, cnonce="e76b05db27a3b323"`
want := map[string]string{
"username": "test",
"realm": `a "quoted" string`,
"nonce": "FRPnGdb8lvM1UHhi",
"uri": "/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro",
"algorithm": "MD5",
"response": "fdcdd78e5b306ffed343d0ec3967f2e5",
"opaque": "lEgVjogmIar2fg/t",
"qop": "auth",
"nc": "00000001",
"cnonce": "e76b05db27a3b323",
}
got := ParsePairs(header)
if !reflect.DeepEqual(got, want) {
t.Fatalf("failed to correctly parse pairs, got %v, want %v\ndiff: %s", got, want)
}
}

View file

@ -1,45 +0,0 @@
package auth
import (
"os"
"testing"
"time"
)
func TestHtdigestFile(t *testing.T) {
secrets := HtdigestFileProvider("test.htdigest")
digest := secrets("test", "example.com")
if digest != "aa78524fceb0e50fd8ca96dd818b8cf9" {
t.Fatal("Incorrect digest for test user:", digest)
}
digest = secrets("test", "example1.com")
if digest != "" {
t.Fatal("Got digest for user in non-existant realm:", digest)
}
digest = secrets("test1", "example.com")
if digest != "" {
t.Fatal("Got digest for non-existant user:", digest)
}
}
func TestHtpasswdFile(t *testing.T) {
secrets := HtpasswdFileProvider("test.htpasswd")
passwd := secrets("test", "blah")
if passwd != "{SHA}qvTGHdzF6KLavt4PO0gs2a6pQ00=" {
t.Fatal("Incorrect passwd for test user:", passwd)
}
passwd = secrets("nosuchuser", "blah")
if passwd != "" {
t.Fatal("Got passwd for non-existant user:", passwd)
}
}
// TestConcurrent verifies potential race condition in users reading logic
func TestConcurrent(t *testing.T) {
secrets := HtpasswdFileProvider("test.htpasswd")
os.Chtimes("test.htpasswd", time.Now(), time.Now())
go func() {
secrets("test", "blah")
}()
secrets("test", "blah")
}