now mw parse token from query and from header

This commit is contained in:
Pavel 2024-06-08 15:50:59 +03:00
parent e6b3646cc7
commit 28e724dbad
4 changed files with 20 additions and 11 deletions

@ -828,9 +828,9 @@ paths:
summary: Получение изменений аккаунта через SSE summary: Получение изменений аккаунта через SSE
operationId: accountPipe operationId: accountPipe
parameters: parameters:
- name: userID - name: token
in: query in: query
description: id пользователя description: токен пользователя
required: true required: true
responses: responses:
'200': '200':

@ -246,8 +246,8 @@ func (receiver *AccountController) Pagination(ctx *fiber.Ctx) error {
} }
func (receiver *AccountController) AccountPipe(ctx *fiber.Ctx) error { func (receiver *AccountController) AccountPipe(ctx *fiber.Ctx) error {
userID := ctx.Query("userID") userID, ok := receiver.middleWare.ExtractUserID(ctx)
if userID == "" { if !ok || userID == "" {
return receiver.middleWare.NoAuth(ctx) return receiver.middleWare.NoAuth(ctx)
} }

@ -29,9 +29,6 @@ func NewAuthenticator(jwtUtil *JWT) fiber.Handler {
} }
func authenticate(jwtUtil *JWT, c *fiber.Ctx) error { func authenticate(jwtUtil *JWT, c *fiber.Ctx) error {
if c.Path() == "/account/pipe" {
return c.Next()
}
jws, err := parseJWSFromRequest(c) jws, err := parseJWSFromRequest(c)
if err != nil { if err != nil {
return err return err
@ -51,12 +48,17 @@ func authenticate(jwtUtil *JWT, c *fiber.Ctx) error {
func parseJWSFromRequest(c *fiber.Ctx) (string, error) { func parseJWSFromRequest(c *fiber.Ctx) (string, error) {
header := c.Get("Authorization") header := c.Get("Authorization")
if header == "" || !strings.HasPrefix(header, prefix) { if header != "" && strings.HasPrefix(header, prefix) {
return strings.TrimPrefix(header, prefix), nil
}
token := c.Query("token")
if token == "" {
return "", errors.New( return "", errors.New(
fmt.Errorf("failed to parse jws from request header: %s", header), fmt.Errorf("failed to parse jws from request: no valid token found"),
errors.ErrNoAccess, errors.ErrNoAccess,
) )
} }
return strings.TrimPrefix(header, prefix), nil return token, nil
} }

@ -5,12 +5,19 @@ import (
"fmt" "fmt"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"net/http" "net/http"
"penahub.gitlab.yandexcloud.net/pena-services/customer/tests/helpers"
"testing" "testing"
"time" "time"
) )
func TestAccountPipe(t *testing.T) { func TestAccountPipe(t *testing.T) {
url := "http://localhost:8082/account/pipe?userID=64e53ed187392e122e5d3d50" jwtUtil := helpers.InitializeJWT()
token, tokenErr := jwtUtil.Create("64ebda4387392e122e5d411f")
if !assert.NoError(t, tokenErr) {
return
}
url := fmt.Sprintf("http://localhost:8082/account/pipe?token=%s", token)
client := &http.Client{ client := &http.Client{
Timeout: 100 * time.Second, Timeout: 100 * time.Second,
} }