2022-08-30 02:30:36 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2023-02-05 21:12:54 +00:00
|
|
|
"strings"
|
2022-09-27 23:52:13 +00:00
|
|
|
"time"
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
2022-10-30 02:15:35 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/pkgs/server"
|
2022-09-03 18:38:35 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
|
|
|
TokenResponse struct {
|
2022-12-03 19:55:00 +00:00
|
|
|
Token string `json:"token"`
|
|
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
|
|
AttachmentToken string `json:"attachmentToken"`
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LoginForm struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
// HandleAuthLogin godoc
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary User Login
|
|
|
|
// @Tags Authentication
|
|
|
|
// @Accept x-www-form-urlencoded
|
|
|
|
// @Accept application/json
|
|
|
|
// @Param username formData string false "string" example(admin@admin.com)
|
|
|
|
// @Param password formData string false "string" example(admin)
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} TokenResponse
|
|
|
|
// @Router /v1/users/login [POST]
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleAuthLogin() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-09-27 23:52:13 +00:00
|
|
|
loginForm := &LoginForm{}
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2022-10-09 17:23:21 +00:00
|
|
|
switch r.Header.Get("Content-Type") {
|
|
|
|
case server.ContentFormUrlEncoded:
|
2022-08-30 02:30:36 +00:00
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusBadRequest, server.Wrap(err))
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loginForm.Username = r.PostFormValue("username")
|
|
|
|
loginForm.Password = r.PostFormValue("password")
|
2022-10-09 17:23:21 +00:00
|
|
|
case server.ContentJSON:
|
2022-08-30 02:30:36 +00:00
|
|
|
err := server.Decode(r, loginForm)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to decode login form")
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
2022-10-09 17:23:21 +00:00
|
|
|
default:
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusBadRequest, errors.New("invalid content type"))
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if loginForm.Username == "" || loginForm.Password == "" {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewFieldErrors(
|
|
|
|
validate.FieldError{
|
|
|
|
Field: "username",
|
|
|
|
Error: "username or password is empty",
|
|
|
|
},
|
|
|
|
validate.FieldError{
|
|
|
|
Field: "password",
|
|
|
|
Error: "username or password is empty",
|
|
|
|
},
|
|
|
|
)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 21:12:54 +00:00
|
|
|
newToken, err := ctrl.svc.User.Login(r.Context(), strings.ToLower(loginForm.Username), loginForm.Password)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(errors.New("authentication failed"), http.StatusInternalServerError)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusOK, TokenResponse{
|
2022-12-03 19:55:00 +00:00
|
|
|
Token: "Bearer " + newToken.Raw,
|
|
|
|
ExpiresAt: newToken.ExpiresAt,
|
|
|
|
AttachmentToken: newToken.AttachmentToken,
|
2022-08-30 02:30:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleAuthLogout godoc
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary User Logout
|
|
|
|
// @Tags Authentication
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/users/logout [POST]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleAuthLogout() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-08-30 02:30:36 +00:00
|
|
|
token := services.UseTokenCtx(r.Context())
|
|
|
|
if token == "" {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(errors.New("no token within request context"), http.StatusUnauthorized)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := ctrl.svc.User.Logout(r.Context(), token)
|
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusNoContent, nil)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleAuthLogout godoc
|
2022-10-13 05:13:07 +00:00
|
|
|
// @Summary User Token Refresh
|
|
|
|
// @Description handleAuthRefresh returns a handler that will issue a new token from an existing token.
|
|
|
|
// @Description This does not validate that the user still exists within the database.
|
|
|
|
// @Tags Authentication
|
|
|
|
// @Success 200
|
|
|
|
// @Router /v1/users/refresh [GET]
|
|
|
|
// @Security Bearer
|
2022-10-30 02:15:35 +00:00
|
|
|
func (ctrl *V1Controller) HandleAuthRefresh() server.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-08-30 02:30:36 +00:00
|
|
|
requestToken := services.UseTokenCtx(r.Context())
|
|
|
|
if requestToken == "" {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(errors.New("no token within request context"), http.StatusUnauthorized)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newToken, err := ctrl.svc.User.RenewToken(r.Context(), requestToken)
|
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewUnauthorizedError()
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return server.Respond(w, http.StatusOK, newToken)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
}
|