@@ -6,18 +6,14 @@ import (
6
6
"sync"
7
7
)
8
8
9
- /*
10
- SecretProvider is used by authenticators. Takes user name and realm
11
- as an argument, returns secret required for authentication (HA1 for
12
- digest authentication, properly encrypted password for basic).
13
-
14
- Returning an empty string means failing the authentication.
15
- */
9
+ // SecretProvider is used by authenticators. Takes user name and realm
10
+ // as an argument, returns secret required for authentication (HA1 for
11
+ // digest authentication, properly encrypted password for basic).
12
+ //
13
+ // Returning an empty string means failing the authentication.
16
14
type SecretProvider func (user , realm string ) string
17
15
18
- /*
19
- Common functions for file auto-reloading
20
- */
16
+ // File handles automatic file reloading on changes.
21
17
type File struct {
22
18
Path string
23
19
Info os.FileInfo
@@ -26,6 +22,11 @@ type File struct {
26
22
mu sync.Mutex
27
23
}
28
24
25
+ // ReloadIfNeeded checks file Stat and calls Reload() if any changes
26
+ // were detected. File mutex is Locked for the duration of Reload()
27
+ // call.
28
+ //
29
+ // This function will panic() if Stat fails.
29
30
func (f * File ) ReloadIfNeeded () {
30
31
info , err := os .Stat (f .Path )
31
32
if err != nil {
@@ -39,27 +40,26 @@ func (f *File) ReloadIfNeeded() {
39
40
}
40
41
}
41
42
42
- /*
43
- Structure used for htdigest file authentication. Users map realms to
44
- maps of users to their HA1 digests.
45
- */
43
+ // HtdigestFile is a File holding htdigest authentication data.
46
44
type HtdigestFile struct {
45
+ // File is used for automatic reloading of the authentication data.
47
46
File
47
+ // Users is a map of realms to users to HA1 digests.
48
48
Users map [string ]map [string ]string
49
49
mu sync.RWMutex
50
50
}
51
51
52
- func reload_htdigest (hf * HtdigestFile ) {
52
+ func reloadHTDigest (hf * HtdigestFile ) {
53
53
r , err := os .Open (hf .Path )
54
54
if err != nil {
55
55
panic (err )
56
56
}
57
- csv_reader := csv .NewReader (r )
58
- csv_reader .Comma = ':'
59
- csv_reader .Comment = '#'
60
- csv_reader .TrimLeadingSpace = true
57
+ reader := csv .NewReader (r )
58
+ reader .Comma = ':'
59
+ reader .Comment = '#'
60
+ reader .TrimLeadingSpace = true
61
61
62
- records , err := csv_reader .ReadAll ()
62
+ records , err := reader .ReadAll ()
63
63
if err != nil {
64
64
panic (err )
65
65
}
@@ -76,14 +76,12 @@ func reload_htdigest(hf *HtdigestFile) {
76
76
}
77
77
}
78
78
79
- /*
80
- SecretProvider implementation based on htdigest-formated files. Will
81
- reload htdigest file on changes. Will panic on syntax errors in
82
- htdigest files.
83
- */
79
+ // HtdigestFileProvider is a SecretProvider implementation based on
80
+ // htdigest-formated files. It will automatically reload htdigest file
81
+ // on changes. It panics on syntax errors in htdigest files.
84
82
func HtdigestFileProvider (filename string ) SecretProvider {
85
83
hf := & HtdigestFile {File : File {Path : filename }}
86
- hf .Reload = func () { reload_htdigest (hf ) }
84
+ hf .Reload = func () { reloadHTDigest (hf ) }
87
85
return func (user , realm string ) string {
88
86
hf .ReloadIfNeeded ()
89
87
hf .mu .RLock ()
@@ -100,27 +98,27 @@ func HtdigestFileProvider(filename string) SecretProvider {
100
98
}
101
99
}
102
100
103
- /*
104
- Structure used for htdigest file authentication. Users map users to
105
- their salted encrypted password
106
- */
101
+ // HtpasswdFile is a File holding basic authentication data.
107
102
type HtpasswdFile struct {
103
+ // File is used for automatic reloading of the authentication data.
108
104
File
105
+ // Users is a map of users to their secrets (salted encrypted
106
+ // passwords).
109
107
Users map [string ]string
110
108
mu sync.RWMutex
111
109
}
112
110
113
- func reload_htpasswd (h * HtpasswdFile ) {
111
+ func reloadHTPasswd (h * HtpasswdFile ) {
114
112
r , err := os .Open (h .Path )
115
113
if err != nil {
116
114
panic (err )
117
115
}
118
- csv_reader := csv .NewReader (r )
119
- csv_reader .Comma = ':'
120
- csv_reader .Comment = '#'
121
- csv_reader .TrimLeadingSpace = true
116
+ reader := csv .NewReader (r )
117
+ reader .Comma = ':'
118
+ reader .Comment = '#'
119
+ reader .TrimLeadingSpace = true
122
120
123
- records , err := csv_reader .ReadAll ()
121
+ records , err := reader .ReadAll ()
124
122
if err != nil {
125
123
panic (err )
126
124
}
@@ -133,14 +131,13 @@ func reload_htpasswd(h *HtpasswdFile) {
133
131
}
134
132
}
135
133
136
- /*
137
- SecretProvider implementation based on htpasswd-formated files. Will
138
- reload htpasswd file on changes. Will panic on syntax errors in
139
- htpasswd files. Realm argument of the SecretProvider is ignored.
140
- */
134
+ // HtpasswdFileProvider is a SecretProvider implementation based on
135
+ // htpasswd-formated files. It will automatically reload htpasswd file
136
+ // on changes. It panics on syntax errors in htpasswd files. Realm
137
+ // argument of the SecretProvider is ignored.
141
138
func HtpasswdFileProvider (filename string ) SecretProvider {
142
139
h := & HtpasswdFile {File : File {Path : filename }}
143
- h .Reload = func () { reload_htpasswd (h ) }
140
+ h .Reload = func () { reloadHTPasswd (h ) }
144
141
return func (user , realm string ) string {
145
142
h .ReloadIfNeeded ()
146
143
h .mu .RLock ()
0 commit comments