Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions caddytest/integration/authenticate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package integration

import (
"encoding/base64"
"net/http"
"testing"

"github.com/caddyserver/caddy/v2/caddytest"
)

func TestAuthentication(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
"admin": {
"listen": "localhost:2999"
},
"apps": {
"pki": {
"certificate_authorities": {
"local": {
"install_trust": false
}
}
},
"http": {
"http_port": 9080,
"https_port": 9443,
"servers": {
"srv0": {
"listen": [
":9080"
],
"routes": [
{
"match": [
{
"path": [
"/basic"
]
}
],
"handle": [
{
"handler": "authentication",
"providers": {
"http_basic": {
"hash_cache": {},
"accounts": [
{
"username": "Aladdin",
"password": "$2a$14$U5nG2p.Ac09gzn9oo5aRe.YnsXn30UdXA6pRUn45KFqADG636dRHa"
}
]
}
}
}
]
},
{
"match": [
{
"path": [
"/proxy"
]
}
],
"handle": [
{
"handler": "authentication",
"status_code": 407,
"providers": {
"http_basic": {
"hash_cache": {},
"authorization_header": "Proxy-Authorization",
"authenticate_header": "Proxy-Authenticate",
"realm": "HTTP proxy",
"accounts": [
{
"username": "Aladdin",
"password": "$2a$14$U5nG2p.Ac09gzn9oo5aRe.YnsXn30UdXA6pRUn45KFqADG636dRHa"
}
]
}
}
}
]
}
]
}
}
}
}
}
`, "json")

assertHeader := func(tb testing.TB, resp *http.Response, header, want string) {
if actual := resp.Header.Get(header); actual != want {
tb.Errorf("expected %s header to be %s, but was %s", header, want, actual)
}
}

resp, _ := tester.AssertGetResponse("http://localhost:9080/basic", http.StatusUnauthorized, "")
assertHeader(t, resp, "WWW-Authenticate", `Basic realm="restricted"`)

tester.AssertGetResponse("http://Aladdin:open%20sesame@localhost:9080/basic", http.StatusOK, "")

tester.AssertGetResponse("http://localhost:9080/proxy", http.StatusProxyAuthRequired, "")

resp, _ = tester.AssertGetResponse("http://Aladdin:open%20sesame@localhost:9080/proxy", http.StatusProxyAuthRequired, "")
assertHeader(t, resp, "Proxy-Authenticate", `Basic realm="HTTP proxy"`)

req, err := http.NewRequest(http.MethodGet, "http://localhost:9080/proxy", nil)
if err != nil {
t.Fatalf("unable to create request %v", err)
}
req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("Aladdin:open sesame")))
tester.AssertResponseCode(req, http.StatusOK)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
https://example.com
basic_auth bcrypt {
Aladdin $2a$14$U5nG2p.Ac09gzn9oo5aRe.YnsXn30UdXA6pRUn45KFqADG636dRHa
}

----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"example.com"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "authentication",
"providers": {
"http_basic": {
"accounts": [
{
"password": "$2a$14$U5nG2p.Ac09gzn9oo5aRe.YnsXn30UdXA6pRUn45KFqADG636dRHa",
"username": "Aladdin"
}
],
"hash": {
"algorithm": "bcrypt"
},
"hash_cache": {}
}
}
}
]
}
]
}
],
"terminal": true
}
]
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
https://example.com
basic_auth

----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"example.com"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "authentication",
"providers": {
"http_basic": {
"hash": {
"algorithm": "bcrypt"
},
"hash_cache": {}
}
}
}
]
}
]
}
],
"terminal": true
}
]
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
https://example.com {
basic_auth proxy bcrypt {
Aladdin $2a$14$U5nG2p.Ac09gzn9oo5aRe.YnsXn30UdXA6pRUn45KFqADG636dRHa
}
# Alternatively, use https://github.com/caddyserver/forwardproxy instead
# of external forward proxy.
reverse_proxy https://localhost:54321
}

----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"example.com"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "authentication",
"providers": {
"http_basic": {
"accounts": [
{
"password": "$2a$14$U5nG2p.Ac09gzn9oo5aRe.YnsXn30UdXA6pRUn45KFqADG636dRHa",
"username": "Aladdin"
}
],
"authenticate_header": "Proxy-Authenticate",
"authorization_header": "Proxy-Authorization",
"hash": {
"algorithm": "bcrypt"
},
"hash_cache": {}
}
},
"status_code": 407
},
{
"handler": "reverse_proxy",
"transport": {
"protocol": "http",
"tls": {}
},
"upstreams": [
{
"dial": "localhost:54321"
}
]
}
]
}
]
}
],
"terminal": true
}
]
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
https://example.com
basic_auth bcrypt "my realm" {
Aladdin $2a$14$U5nG2p.Ac09gzn9oo5aRe.YnsXn30UdXA6pRUn45KFqADG636dRHa
}

----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"example.com"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "authentication",
"providers": {
"http_basic": {
"accounts": [
{
"password": "$2a$14$U5nG2p.Ac09gzn9oo5aRe.YnsXn30UdXA6pRUn45KFqADG636dRHa",
"username": "Aladdin"
}
],
"hash": {
"algorithm": "bcrypt"
},
"hash_cache": {},
"realm": "my realm"
}
}
}
]
}
]
}
],
"terminal": true
}
]
}
}
}
}
}
Loading