Refactoring
parent
c09fa9d5cf
commit
f4ab8927ea
|
@ -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) {
|
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)
|
username := r.Header.Get(a.Header)
|
||||||
user, err := usr.Get(srv.Root, username)
|
user, err := usr.Get(srv.Root, username)
|
||||||
if err != nil {
|
if errors.Is(err, fbErrors.ErrNotExist) {
|
||||||
// Return the error unless it is a user does not exist error
|
return a.createUser(usr, setting, srv, username)
|
||||||
if !errors.Is(err, fbErrors.ErrNotExist) {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
return user, err
|
||||||
|
}
|
||||||
|
|
||||||
randomPasswordBytes := make([]byte, 32) //nolint:gomnd
|
func (a ProxyAuth) createUser(usr users.Store, setting *settings.Settings, srv *settings.Server, username string) (*users.User, error) {
|
||||||
_, err = rand.Read(randomPasswordBytes)
|
const passwordSize = 32
|
||||||
|
randomPasswordBytes := make([]byte, passwordSize)
|
||||||
|
_, err := rand.Read(randomPasswordBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -40,7 +42,7 @@ func (a ProxyAuth) Auth(r *http.Request, usr users.Store, setting *settings.Sett
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
user = &users.User{
|
user := &users.User{
|
||||||
Username: username,
|
Username: username,
|
||||||
Password: hashedRandomPassword,
|
Password: hashedRandomPassword,
|
||||||
LockPassword: true,
|
LockPassword: true,
|
||||||
|
@ -58,7 +60,6 @@ func (a ProxyAuth) Auth(r *http.Request, usr users.Store, setting *settings.Sett
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue