fix: tests

This commit is contained in:
Ferdinand Mütsch 2022-12-01 11:11:45 +01:00
parent 964405f349
commit d4945c982f
4 changed files with 919 additions and 940 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,13 +2,23 @@ package helpers
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/muety/wakapi/config" "github.com/muety/wakapi/config"
"github.com/muety/wakapi/utils" "github.com/muety/wakapi/models"
"net/http" "net/http"
) )
func ExtractCookieAuth(r *http.Request) (username *string, err error) { func ExtractCookieAuth(r *http.Request, config *config.Config) (username *string, err error) {
return utils.ExtractCookieAuth(r, config.Get().Security.SecureCookie) cookie, err := r.Cookie(models.AuthCookieKey)
if err != nil {
return nil, errors.New("missing authentication")
}
if err := config.Security.SecureCookie.Decode(models.AuthCookieKey, cookie.Value, &username); err != nil {
return nil, errors.New("cookie is invalid")
}
return username, nil
} }
func RespondJSON(w http.ResponseWriter, r *http.Request, status int, object interface{}) { func RespondJSON(w http.ResponseWriter, r *http.Request, status int, object interface{}) {

View File

@ -122,7 +122,7 @@ func (m *AuthenticateMiddleware) tryGetUserByApiKeyQuery(r *http.Request) (*mode
} }
func (m *AuthenticateMiddleware) tryGetUserByCookie(r *http.Request) (*models.User, error) { func (m *AuthenticateMiddleware) tryGetUserByCookie(r *http.Request) (*models.User, error) {
username, err := helpers.ExtractCookieAuth(r) username, err := helpers.ExtractCookieAuth(r, m.config)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -3,8 +3,6 @@ package utils
import ( import (
"encoding/base64" "encoding/base64"
"errors" "errors"
"github.com/gorilla/securecookie"
"github.com/muety/wakapi/models"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"net/http" "net/http"
"regexp" "regexp"
@ -44,19 +42,6 @@ func ExtractBearerAuth(r *http.Request) (key string, err error) {
return string(keyBytes), err return string(keyBytes), err
} }
func ExtractCookieAuth(r *http.Request, secureCookie *securecookie.SecureCookie) (username *string, err error) {
cookie, err := r.Cookie(models.AuthCookieKey)
if err != nil {
return nil, errors.New("missing authentication")
}
if err := secureCookie.Decode(models.AuthCookieKey, cookie.Value, &username); err != nil {
return nil, errors.New("cookie is invalid")
}
return username, nil
}
func CompareBcrypt(wanted, actual, pepper string) bool { func CompareBcrypt(wanted, actual, pepper string) bool {
plainPassword := []byte(strings.TrimSpace(actual) + pepper) plainPassword := []byte(strings.TrimSpace(actual) + pepper)
err := bcrypt.CompareHashAndPassword([]byte(wanted), plainPassword) err := bcrypt.CompareHashAndPassword([]byte(wanted), plainPassword)