Refactoring

pull/3666/head
Oleg Lobanov 2025-01-30 11:24:28 +01:00
parent c09fa9d5cf
commit f4ab8927ea
No known key found for this signature in database
1 changed files with 32 additions and 31 deletions

View File

@ -22,14 +22,16 @@ type ProxyAuth struct {
func (a ProxyAuth) Auth(r *http.Request, usr users.Store, setting *settings.Settings, srv *settings.Server) (*users.User, error) {
username := r.Header.Get(a.Header)
user, err := usr.Get(srv.Root, username)
if err != nil {
// Return the error unless it is a user does not exist error
if !errors.Is(err, fbErrors.ErrNotExist) {
return nil, err
if errors.Is(err, fbErrors.ErrNotExist) {
return a.createUser(usr, setting, srv, username)
}
return user, err
}
randomPasswordBytes := make([]byte, 32) //nolint:gomnd
_, err = rand.Read(randomPasswordBytes)
func (a ProxyAuth) createUser(usr users.Store, setting *settings.Settings, srv *settings.Server, username string) (*users.User, error) {
const passwordSize = 32
randomPasswordBytes := make([]byte, passwordSize)
_, err := rand.Read(randomPasswordBytes)
if err != nil {
return nil, err
}
@ -40,7 +42,7 @@ func (a ProxyAuth) Auth(r *http.Request, usr users.Store, setting *settings.Sett
return nil, err
}
user = &users.User{
user := &users.User{
Username: username,
Password: hashedRandomPassword,
LockPassword: true,
@ -58,7 +60,6 @@ func (a ProxyAuth) Auth(r *http.Request, usr users.Store, setting *settings.Sett
if err != nil {
return nil, err
}
}
return user, nil
}