@@ -20,7 +20,12 @@ const (
20
20
21
21
type HtpasswdClient struct {
22
22
credMap credMap
23
- filepath string
23
+ credFile credFile
24
+ }
25
+
26
+ type credFile struct {
27
+ path string
28
+ rw * sync.RWMutex
24
29
}
25
30
26
31
type credMap struct {
@@ -30,7 +35,10 @@ type credMap struct {
30
35
31
36
func NewHtpasswdClient (filepath string ) * HtpasswdClient {
32
37
return & HtpasswdClient {
33
- filepath : filepath ,
38
+ credFile : credFile {
39
+ path : filepath ,
40
+ rw : & sync.RWMutex {},
41
+ },
34
42
credMap : credMap {
35
43
m : make (map [string ]string ),
36
44
rw : & sync.RWMutex {},
@@ -39,12 +47,12 @@ func NewHtpasswdClient(filepath string) *HtpasswdClient {
39
47
}
40
48
41
49
// Init initializes the HtpasswdClient.
42
- // It performs the file read using the filename specified in NewHtpasswdClient
50
+ // It performs the credFile read using the filename specified in NewHtpasswdClient
43
51
// and caches all user passwords.
44
52
func (hc * HtpasswdClient ) Init () error {
45
- credsFile , err := os .Open (hc .filepath )
53
+ credsFile , err := os .Open (hc .credFile . path )
46
54
if err != nil {
47
- return fmt .Errorf ("error occurred while opening creds-file : %w" , err )
55
+ return fmt .Errorf ("error occurred while opening creds-credFile : %w" , err )
48
56
}
49
57
defer credsFile .Close ()
50
58
@@ -63,7 +71,7 @@ func (hc *HtpasswdClient) Init() error {
63
71
}
64
72
65
73
if err := scanner .Err (); err != nil {
66
- return fmt .Errorf ("error occurred while reading creds-file : %w" , err )
74
+ return fmt .Errorf ("error occurred while reading creds-credFile : %w" , err )
67
75
}
68
76
69
77
return nil
@@ -121,7 +129,7 @@ func (hc *HtpasswdClient) ChangePassword(login, supposedOldPassword, newPassword
121
129
return zerr .ErrOldPasswordIsWrong
122
130
}
123
131
124
- // if passwords match, no need to update file and map, return nil as if operation is successful
132
+ // if passwords match, no need to update credFile and map, return nil as if operation is successful
125
133
if err := bcrypt .CompareHashAndPassword ([]byte (oldPassphrase ), []byte (newPassword )); err == nil {
126
134
return nil
127
135
}
@@ -132,9 +140,9 @@ func (hc *HtpasswdClient) ChangePassword(login, supposedOldPassword, newPassword
132
140
return fmt .Errorf ("error occurred while encrypting new password: %w" , err )
133
141
}
134
142
135
- file , err := os .ReadFile (hc .filepath )
143
+ file , err := os .ReadFile (hc .credFile . path )
136
144
if err != nil {
137
- return fmt .Errorf ("error occurred while reading creds-file : %w" , err )
145
+ return fmt .Errorf ("error occurred while reading creds-credFile : %w" , err )
138
146
}
139
147
140
148
// read passwords line by line to find the corresponding login
@@ -149,41 +157,39 @@ func (hc *HtpasswdClient) ChangePassword(login, supposedOldPassword, newPassword
149
157
}
150
158
}
151
159
152
- // write new content to temporary file
153
- // and replace the old file with temporary, so the operation is atomic
160
+ // write new content to temporary credFile
161
+ // and replace the old credFile with temporary, so the operation is atomic
154
162
output := []byte (strings .Join (lines , "\n " ))
155
163
156
- tmpfile , err := os .CreateTemp (filepath .Dir (hc .filepath ), "htpasswd-*.tmp" )
164
+ tmpfile , err := os .CreateTemp (filepath .Dir (hc .credFile . path ), "htpasswd-*.tmp" )
157
165
if err != nil {
158
- return fmt .Errorf ("error occurred when creating temp htpasswd file : %w" , err )
166
+ return fmt .Errorf ("error occurred when creating temp htpasswd credFile : %w" , err )
159
167
}
160
168
161
169
if _ , err := tmpfile .Write (output ); err != nil {
162
170
tmpfile .Close ()
163
171
os .Remove (tmpfile .Name ())
164
- return fmt .Errorf ("error occurred when writing to temp htpasswd file: %w" , err )
172
+
173
+ return fmt .Errorf ("error occurred when writing to temp htpasswd credFile: %w" , err )
165
174
}
166
175
167
176
if err := tmpfile .Close (); err != nil {
168
177
os .Remove (tmpfile .Name ())
169
- return fmt .Errorf ("error occurred when closing temp htpasswd file: %w" , err )
178
+
179
+ return fmt .Errorf ("error occurred when closing temp htpasswd credFile: %w" , err )
170
180
}
171
181
172
- if err := os .Rename (tmpfile .Name (), hc .filepath ); err != nil {
173
- return fmt .Errorf ("error occurred while replacing htpasswd file with new file : %w" , err )
182
+ if err := os .Rename (tmpfile .Name (), hc .credFile . path ); err != nil {
183
+ return fmt .Errorf ("error occurred while replacing htpasswd credFile with new credFile : %w" , err )
174
184
}
175
185
176
- err = os .WriteFile (hc .filepath , output , constants .DefaultDirPerms )
186
+ err = os .WriteFile (hc .credFile . path , output , constants .DefaultDirPerms )
177
187
if err != nil {
178
- return fmt .Errorf ("error occurred while writing to creds-file : %w" , err )
188
+ return fmt .Errorf ("error occurred while writing to creds-credFile : %w" , err )
179
189
}
180
190
181
- // set to credMap only if all file operations are successful to prevent collisions
182
- hc .credMap .rw .Lock ()
183
- hc .credMap .m [login ] = string (newPassphrase )
184
- hc .credMap .rw .Unlock ()
185
-
186
- return nil
191
+ // set to credMap only if all credFile operations are successful to prevent collisions
192
+ return hc .credMap .Set (login , string (newPassphrase ))
187
193
}
188
194
189
195
func (c credMap ) Set (login , passphrase string ) error {
0 commit comments