mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-29 20:15:41 +00:00
0c535aa8d8
* extract auth into provider
* bump go version
* use pointer
* rebase
Former-commit-id: 8538877f52
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package providers
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
|
"github.com/hay-kot/httpkit/server"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type LoginForm struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
StayLoggedIn bool `json:"stayLoggedIn"`
|
|
}
|
|
|
|
func getLoginForm(r *http.Request) (LoginForm, error) {
|
|
loginForm := LoginForm{}
|
|
|
|
switch r.Header.Get("Content-Type") {
|
|
case "application/x-www-form-urlencoded":
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return loginForm, errors.New("failed to parse form")
|
|
}
|
|
|
|
loginForm.Username = r.PostFormValue("username")
|
|
loginForm.Password = r.PostFormValue("password")
|
|
loginForm.StayLoggedIn = r.PostFormValue("stayLoggedIn") == "true"
|
|
case "application/json":
|
|
err := server.Decode(r, &loginForm)
|
|
if err != nil {
|
|
log.Err(err).Msg("failed to decode login form")
|
|
return loginForm, errors.New("failed to decode login form")
|
|
}
|
|
default:
|
|
return loginForm, errors.New("invalid content type")
|
|
}
|
|
|
|
if loginForm.Username == "" || loginForm.Password == "" {
|
|
return loginForm, validate.NewFieldErrors(
|
|
validate.FieldError{
|
|
Field: "username",
|
|
Error: "username or password is empty",
|
|
},
|
|
validate.FieldError{
|
|
Field: "password",
|
|
Error: "username or password is empty",
|
|
},
|
|
)
|
|
}
|
|
|
|
return loginForm, nil
|
|
}
|