1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: Support query parameter token

This commit is contained in:
Ce Gao 2021-10-11 15:10:30 +08:00
parent 5394349c73
commit 630090e38a

View File

@ -1,12 +1,13 @@
package middlewares package middlewares
import ( import (
"net/http"
"strings"
conf "github.com/muety/wakapi/config" conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/models" "github.com/muety/wakapi/models"
"github.com/muety/wakapi/services" "github.com/muety/wakapi/services"
"github.com/muety/wakapi/utils" "github.com/muety/wakapi/utils"
"net/http"
"strings"
) )
type AuthenticateMiddleware struct { type AuthenticateMiddleware struct {
@ -47,6 +48,9 @@ func (m *AuthenticateMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Reques
if err != nil { if err != nil {
user, err = m.tryGetUserByApiKey(r) user, err = m.tryGetUserByApiKey(r)
} }
if err != nil {
user, err = m.tryGetUserByQueryParameter(r)
}
if err != nil || user == nil { if err != nil || user == nil {
if m.isOptional(r.URL.Path) { if m.isOptional(r.URL.Path) {
@ -92,6 +96,18 @@ func (m *AuthenticateMiddleware) tryGetUserByApiKey(r *http.Request) (*models.Us
return user, nil return user, nil
} }
func (m *AuthenticateMiddleware) tryGetUserByQueryParameter(r *http.Request) (*models.User, error) {
key := r.URL.Query().Get("token")
var user *models.User
userKey := strings.TrimSpace(key)
user, err := m.userSrvc.GetUserByKey(userKey)
if err != nil {
return nil, err
}
return user, nil
}
func (m *AuthenticateMiddleware) tryGetUserByCookie(r *http.Request) (*models.User, error) { func (m *AuthenticateMiddleware) tryGetUserByCookie(r *http.Request) (*models.User, error) {
username, err := utils.ExtractCookieAuth(r, m.config) username, err := utils.ExtractCookieAuth(r, m.config)
if err != nil { if err != nil {