Debian Codegraph @

modules/caddyhttp/caddyauth/caddyfile.go

raw symbols SCIP
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 // 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{
95 ProvidersRaw: caddy.ModuleMap{
96 "http_basic": caddyconfig.JSON(ba, nil),
97 },
98 }, nil
99 }