Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyauth |
| 16 | |
| 17 | import ( |
| 18 | "github.com/caddyserver/caddy/v2" |
| 19 | "github.com/caddyserver/caddy/v2/caddyconfig" |
| 20 | "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" |
| 21 | "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
| 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | httpcaddyfile.RegisterHandlerDirective("basicauth", parseCaddyfile) // deprecated |
| 26 | httpcaddyfile.RegisterHandlerDirective("basic_auth", parseCaddyfile) |
| 27 | } |
| 28 | |
| 29 | // parseCaddyfile sets up the handler from Caddyfile tokens. Syntax: |
| 30 | // |
| 31 | // basic_auth [<matcher>] [<hash_algorithm> [<realm>]] { |
| 32 | // <username> <hashed_password> |
| 33 | // ... |
| 34 | // } |
| 35 | // |
| 36 | // The hash algorithm is the name of any installed module in the |
| 37 | // http.authentication.hashes namespace, such as bcrypt or argon2id. |
| 38 | // If no hash algorithm is supplied, bcrypt will be assumed. |
| 39 | func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { |
| 40 | h.Next() // consume directive name |
| 41 | |
| 42 | // "basicauth" is deprecated, replaced by "basic_auth" |
| 43 | if h.Val() == "basicauth" { |
| 44 | caddy.Log().Named("config.adapter.caddyfile").Warn("the 'basicauth' directive is deprecated, please use 'basic_auth' instead!") |
| 45 | } |
| 46 | |
| 47 | var ba HTTPBasicAuth |
| 48 | ba.HashCache = new(Cache) |
| 49 | |
| 50 | args := h.RemainingArgs() |
| 51 | |
| 52 | var hashName string |
| 53 | switch len(args) { |
| 54 | case 0: |
| 55 | hashName = bcryptName |
| 56 | case 1: |
| 57 | hashName = args[0] |
| 58 | case 2: |
| 59 | hashName = args[0] |
| 60 | ba.Realm = args[1] |
| 61 | default: |
| 62 | return nil, h.ArgErr() |
| 63 | } |
| 64 | |
| 65 | modInfo, err := hashModuleInfo(hashName) |
| 66 | if err != nil { |
| 67 | return nil, h.WrapErr(err) |
| 68 | } |
| 69 | cmp, ok := modInfo.New().(Comparer) |
| 70 | if !ok { |
| 71 | return nil, h.Errf("hash module %s is not a password comparer", modInfo.ID) |
| 72 | } |
| 73 | |
| 74 | ba.HashRaw = caddyconfig.JSONModuleObject(cmp, "algorithm", hashName, nil) |
| 75 | |
| 76 | for h.NextBlock(0) { |
| 77 | username := h.Val() |
| 78 | |
| 79 | var b64Pwd string |
| 80 | h.Args(&b64Pwd) |
| 81 | if h.NextArg() { |
| 82 | return nil, h.ArgErr() |
| 83 | } |
| 84 | |
| 85 | if username == "" || b64Pwd == "" { |
| 86 | return nil, h.Err("username and password cannot be empty or missing") |
| 87 | } |
| 88 | |
| 89 | ba.AccountList = append(ba.AccountList, Account{ |
| 90 | Username: username, |
| 91 | Password: b64Pwd, |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | return Authentication{ |
| 96 | ProvidersRaw: caddy.ModuleMap{ |
| 97 | "http_basic": caddyconfig.JSON(ba, nil), |
| 98 | }, |
| 99 | }, nil |
| 100 | } |