parseCaddyfile sets up the handler from Caddyfile tokens. Syntax: basic_auth [<matcher>] [<hash_algorithm> [<realm>]] { <username> <hashed_password> ... } If no hash algorithm is supplied, bcrypt will be assumed.
(h httpcaddyfile.Helper)
| 35 | // |
| 36 | // If no hash algorithm is supplied, bcrypt will be assumed. |
| 37 | func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { |
| 38 | h.Next() // consume directive name |
| 39 | |
| 40 | // "basicauth" is deprecated, replaced by "basic_auth" |
| 41 | if h.Val() == "basicauth" { |
| 42 | caddy.Log().Named("config.adapter.caddyfile").Warn("the 'basicauth' directive is deprecated, please use 'basic_auth' instead!") |
| 43 | } |
| 44 | |
| 45 | var ba HTTPBasicAuth |
| 46 | ba.HashCache = new(Cache) |
| 47 | |
| 48 | var cmp Comparer |
| 49 | args := h.RemainingArgs() |
| 50 | |
| 51 | var hashName string |
| 52 | switch len(args) { |
| 53 | case 0: |
| 54 | hashName = bcryptName |
| 55 | case 1: |
| 56 | hashName = args[0] |
| 57 | case 2: |
| 58 | hashName = args[0] |
| 59 | ba.Realm = args[1] |
| 60 | default: |
| 61 | return nil, h.ArgErr() |
| 62 | } |
| 63 | |
| 64 | switch hashName { |
| 65 | case bcryptName: |
| 66 | cmp = BcryptHash{} |
| 67 | case argon2idName: |
| 68 | cmp = Argon2idHash{} |
| 69 | default: |
| 70 | return nil, h.Errf("unrecognized hash algorithm: %s", hashName) |
| 71 | } |
| 72 | |
| 73 | ba.HashRaw = caddyconfig.JSONModuleObject(cmp, "algorithm", hashName, nil) |
| 74 | |
| 75 | for h.NextBlock(0) { |
| 76 | username := h.Val() |
| 77 | |
| 78 | var b64Pwd string |
| 79 | h.Args(&b64Pwd) |
| 80 | if h.NextArg() { |
| 81 | return nil, h.ArgErr() |
| 82 | } |
| 83 | |
| 84 | if username == "" || b64Pwd == "" { |
| 85 | return nil, h.Err("username and password cannot be empty or missing") |
| 86 | } |
| 87 | |
| 88 | ba.AccountList = append(ba.AccountList, Account{ |
| 89 | Username: username, |
| 90 | Password: b64Pwd, |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | return Authentication{ |