Skip to content

Commit

Permalink
caddyauth: Rename basicauth to basic_auth
Browse files Browse the repository at this point in the history
This allows `basic_auth` to work as well in the Caddyfile, and emits a deprecation warning when `basicauth` is used.
  • Loading branch information
francislavoie committed Feb 10, 2024
1 parent c78ebb3 commit ba290d3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
8 changes: 4 additions & 4 deletions caddyconfig/caddyfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ func TestImportedFilesIgnoreNonDirectiveImportTokens(t *testing.T) {
fileName := writeStringToTempFileOrDie(t, `
http://example.com {
# This isn't an import directive, it's just an arg with value 'import'
basicauth / import password
basic_auth / import password
}
`)
// Parse the root file that imports the other one.
Expand All @@ -812,12 +812,12 @@ func TestImportedFilesIgnoreNonDirectiveImportTokens(t *testing.T) {
}
auth := blocks[0].Segments[0]
line := auth[0].Text + " " + auth[1].Text + " " + auth[2].Text + " " + auth[3].Text
if line != "basicauth / import password" {
if line != "basic_auth / import password" {
// Previously, it would be changed to:
// basicauth / import /path/to/test/dir/password
// basic_auth / import /path/to/test/dir/password
// referencing a file that (probably) doesn't exist and changing the
// password!
t.Errorf("Expected basicauth tokens to be 'basicauth / import password' but got %#q", line)
t.Errorf("Expected basic_auth tokens to be 'basic_auth / import password' but got %#q", line)
}
}

Expand Down
3 changes: 2 additions & 1 deletion caddyconfig/httpcaddyfile/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ var directiveOrder = []string{
"try_files",

// middleware handlers; some wrap responses
"basicauth",
"basicauth", // TODO: deprecated, renamed to basic_auth
"basic_auth",
"forward_auth",
"request_header",
"encode",
Expand Down
10 changes: 8 additions & 2 deletions modules/caddyhttp/caddyauth/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
)

func init() {
httpcaddyfile.RegisterHandlerDirective("basicauth", parseCaddyfile)
httpcaddyfile.RegisterHandlerDirective("basicauth", parseCaddyfile) // deprecated
httpcaddyfile.RegisterHandlerDirective("basic_auth", parseCaddyfile)
}

// parseCaddyfile sets up the handler from Caddyfile tokens. Syntax:
//
// basicauth [<matcher>] [<hash_algorithm> [<realm>]] {
// basic_auth [<matcher>] [<hash_algorithm> [<realm>]] {
// <username> <hashed_password_base64> [<salt_base64>]
// ...
// }
Expand All @@ -36,6 +37,11 @@ func init() {
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
h.Next() // consume directive name

// "basicauth" is deprecated, replaced by "basic_auth"
if h.Val() == "basicauth" {
caddy.Log().Named("config.adapter.caddyfile").Warn("the 'basicauth' directive is deprecated, please use 'basic_auth' instead!")
}

var ba HTTPBasicAuth
ba.HashCache = new(Cache)

Expand Down

0 comments on commit ba290d3

Please sign in to comment.