2019-05-06 01:40:41 +03:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-24 18:39:19 +03:00
|
|
|
"fmt"
|
2020-10-16 17:11:14 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2021-01-22 01:19:17 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/services"
|
2020-05-24 14:41:19 +03:00
|
|
|
"github.com/muety/wakapi/utils"
|
2019-05-06 01:40:41 +03:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthenticateMiddleware struct {
|
2020-10-16 17:11:14 +03:00
|
|
|
config *conf.Config
|
2020-11-08 12:12:49 +03:00
|
|
|
userSrvc services.IUserService
|
2020-05-24 14:41:19 +03:00
|
|
|
whitelistPaths []string
|
2019-05-21 15:23:41 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:12:49 +03:00
|
|
|
func NewAuthenticateMiddleware(userService services.IUserService, whitelistPaths []string) *AuthenticateMiddleware {
|
2020-05-24 14:41:19 +03:00
|
|
|
return &AuthenticateMiddleware{
|
2020-10-16 17:11:14 +03:00
|
|
|
config: conf.Get(),
|
2020-05-24 14:41:19 +03:00
|
|
|
userSrvc: userService,
|
|
|
|
whitelistPaths: whitelistPaths,
|
2019-05-21 15:23:41 +03:00
|
|
|
}
|
2019-05-06 01:40:41 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 15:50:04 +03:00
|
|
|
func (m *AuthenticateMiddleware) Handler(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m.ServeHTTP(w, r, h.ServeHTTP)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AuthenticateMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
2020-05-24 14:41:19 +03:00
|
|
|
for _, p := range m.whitelistPaths {
|
2020-04-08 22:29:11 +03:00
|
|
|
if strings.HasPrefix(r.URL.Path, p) || r.URL.Path == p {
|
|
|
|
next(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 23:40:59 +03:00
|
|
|
var user *models.User
|
2020-05-24 14:41:19 +03:00
|
|
|
user, err := m.tryGetUserByCookie(r)
|
2019-05-21 23:40:59 +03:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-24 14:41:19 +03:00
|
|
|
user, err = m.tryGetUserByApiKey(r)
|
2019-05-21 23:40:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-05-24 14:41:19 +03:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api") {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
} else {
|
2020-11-22 00:30:56 +03:00
|
|
|
http.SetCookie(w, m.config.GetClearCookie(models.AuthCookieKey, "/"))
|
2020-10-04 11:37:38 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/?error=unauthorized", m.config.Server.BasePath), http.StatusFound)
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
2019-05-06 01:40:41 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-21 23:40:59 +03:00
|
|
|
ctx := context.WithValue(r.Context(), models.UserKey, user)
|
|
|
|
next(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
func (m *AuthenticateMiddleware) tryGetUserByApiKey(r *http.Request) (*models.User, error) {
|
|
|
|
key, err := utils.ExtractBearerAuth(r)
|
2019-05-06 01:40:41 +03:00
|
|
|
if err != nil {
|
2020-05-24 14:41:19 +03:00
|
|
|
return nil, err
|
2019-05-06 01:40:41 +03:00
|
|
|
}
|
|
|
|
|
2019-05-21 15:23:41 +03:00
|
|
|
var user *models.User
|
2020-05-24 14:41:19 +03:00
|
|
|
userKey := strings.TrimSpace(key)
|
2021-01-22 01:19:17 +03:00
|
|
|
user, err = m.userSrvc.GetUserByKey(userKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-06 01:40:41 +03:00
|
|
|
}
|
2020-05-24 14:41:19 +03:00
|
|
|
return user, nil
|
2019-05-21 23:40:59 +03:00
|
|
|
}
|
2019-05-06 01:40:41 +03:00
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
func (m *AuthenticateMiddleware) tryGetUserByCookie(r *http.Request) (*models.User, error) {
|
2021-01-31 16:34:54 +03:00
|
|
|
username, err := utils.ExtractCookieAuth(r, m.config)
|
2019-05-21 23:40:59 +03:00
|
|
|
if err != nil {
|
2020-05-24 14:41:19 +03:00
|
|
|
return nil, err
|
2019-05-21 23:40:59 +03:00
|
|
|
}
|
|
|
|
|
2021-01-31 16:34:54 +03:00
|
|
|
user, err := m.userSrvc.GetUserById(*username)
|
2020-05-25 23:24:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:34:54 +03:00
|
|
|
// no need to check password here, as securecookie decoding will fail anyway,
|
|
|
|
// if cookie is not properly signed
|
2020-05-25 23:24:29 +03:00
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
return user, nil
|
2019-05-06 01:40:41 +03:00
|
|
|
}
|