some update
This commit is contained in:
parent
968cb49c7f
commit
63460dc78d
@ -11,9 +11,9 @@ import (
|
|||||||
"penahub.gitlab.yandexcloud.net/backend/penahub_common/privilege"
|
"penahub.gitlab.yandexcloud.net/backend/penahub_common/privilege"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/healthchecks"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/healthchecks"
|
||||||
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/clients/auth"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/clients/auth"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/middleware"
|
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/service"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co
|
|||||||
privilegeController := privilege.NewPrivilege(clientData, fiberClient)
|
privilegeController := privilege.NewPrivilege(clientData, fiberClient)
|
||||||
err = privilegeController.PublishPrivileges()
|
err = privilegeController.PublishPrivileges()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Module("Failed to publish privileges")
|
fmt.Println("Failed to publish privileges", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
@ -37,7 +37,7 @@ func (client *AuthClient) GetUserEmail(userID string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if status != fiber.StatusOK {
|
if status != fiber.StatusOK {
|
||||||
return "", fmt.Errorf("unexpected status code: %d, user: %s, reesponse: %s", status, userID, string(resp))
|
return "", fmt.Errorf("unexpected status code: %d, user: %s, reesponse: %s", status, userID, string(resp))
|
||||||
}
|
}
|
||||||
|
|
||||||
return user.Email, nil
|
return user.Email, nil
|
||||||
|
5
go.mod
5
go.mod
@ -4,18 +4,18 @@ go 1.21.4
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gofiber/fiber/v2 v2.52.0
|
github.com/gofiber/fiber/v2 v2.52.0
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.0
|
|
||||||
github.com/lib/pq v1.10.9
|
github.com/lib/pq v1.10.9
|
||||||
github.com/skeris/appInit v1.0.2
|
github.com/skeris/appInit v1.0.2
|
||||||
github.com/tealeg/xlsx v1.0.5
|
github.com/tealeg/xlsx v1.0.5
|
||||||
github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf
|
github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf
|
||||||
go.uber.org/zap v1.26.0
|
go.uber.org/zap v1.26.0
|
||||||
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d
|
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d
|
||||||
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240219175507-7f8de986a6dc
|
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240313164312-4560248259a0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.0.5 // indirect
|
github.com/andybalholm/brotli v1.0.5 // indirect
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
|
||||||
github.com/golang-migrate/migrate/v4 v4.17.0 // indirect
|
github.com/golang-migrate/migrate/v4 v4.17.0 // indirect
|
||||||
github.com/golang/protobuf v1.5.3 // indirect
|
github.com/golang/protobuf v1.5.3 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
@ -26,6 +26,7 @@ require (
|
|||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||||
github.com/rivo/uniseg v0.2.0 // indirect
|
github.com/rivo/uniseg v0.2.0 // indirect
|
||||||
|
github.com/rs/xid v1.5.0 // indirect
|
||||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
github.com/valyala/fasthttp v1.51.0 // indirect
|
github.com/valyala/fasthttp v1.51.0 // indirect
|
||||||
github.com/valyala/tcplisten v1.0.0 // indirect
|
github.com/valyala/tcplisten v1.0.0 // indirect
|
||||||
|
6
go.sum
6
go.sum
@ -73,6 +73,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
|||||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||||
|
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
|
||||||
|
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||||
github.com/skeris/appInit v1.0.2 h1:Hr4KbXYd6kolTVq4cXGqDpgnpmaauiOiKizA1+Ep4KQ=
|
github.com/skeris/appInit v1.0.2 h1:Hr4KbXYd6kolTVq4cXGqDpgnpmaauiOiKizA1+Ep4KQ=
|
||||||
github.com/skeris/appInit v1.0.2/go.mod h1:4ElEeXWVGzU3dlYq/eMWJ/U5hd+LKisc1z3+ySh1XmY=
|
github.com/skeris/appInit v1.0.2/go.mod h1:4ElEeXWVGzU3dlYq/eMWJ/U5hd+LKisc1z3+ySh1XmY=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
@ -146,5 +148,5 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|||||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||||
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d h1:gbaDt35HMDqOK84WYmDIlXMI7rstUcRqNttaT6Kx1do=
|
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d h1:gbaDt35HMDqOK84WYmDIlXMI7rstUcRqNttaT6Kx1do=
|
||||||
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM=
|
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM=
|
||||||
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240219175507-7f8de986a6dc h1:jIN9XyfL/FJ/eSsYopE1olHboituwmisC1Sf1d4nhWE=
|
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240313164312-4560248259a0 h1:YbffFOWneoINvEqCWClf5Zatu/a0VNo9RM0nYveCzhk=
|
||||||
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240219175507-7f8de986a6dc/go.mod h1:OXYvMlc+3qfcllPTywUB3QDiPK1kwsMNdZMTlPXFIdo=
|
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240313164312-4560248259a0/go.mod h1:JgbH8cdAJBr3jx3BuY5nnuTyMdZ1XkRxo8a3w4Y3C0Y=
|
||||||
|
@ -1,80 +0,0 @@
|
|||||||
package middleware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
AccountId = "id"
|
|
||||||
)
|
|
||||||
|
|
||||||
func JWTAuth() fiber.Handler {
|
|
||||||
return func(c *fiber.Ctx) error {
|
|
||||||
authHeader := c.Get("Authorization")
|
|
||||||
if authHeader == "" {
|
|
||||||
c.Status(fiber.StatusUnauthorized).SendString("no JWT found")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
|
||||||
if tokenString == authHeader {
|
|
||||||
c.Status(fiber.StatusUnauthorized).SendString("invalid JWT Header: missing Bearer")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
publicKey := os.Getenv("PUBLIC_ACCESS_SECRET_KEY")
|
|
||||||
if publicKey == "" {
|
|
||||||
// TODO log
|
|
||||||
c.Status(fiber.StatusInternalServerError).SendString("public key not found")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
||||||
return jwt.ParseRSAPublicKeyFromPEM([]byte(publicKey))
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
c.Status(fiber.StatusUnauthorized).SendString("invalid JWT")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if token.Valid {
|
|
||||||
expirationTime, err := token.Claims.GetExpirationTime()
|
|
||||||
if err != nil {
|
|
||||||
c.Status(fiber.StatusUnauthorized).SendString("no expiration time in JWT")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if time.Now().Unix() >= expirationTime.Unix() {
|
|
||||||
c.Status(fiber.StatusUnauthorized).SendString("expired JWT")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
c.Status(fiber.StatusUnauthorized).SendString("invalid JWT")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
m, ok := token.Claims.(jwt.MapClaims)
|
|
||||||
if !ok {
|
|
||||||
c.Status(fiber.StatusInternalServerError).SendString("broken token claims")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
id, ok := m["id"].(string)
|
|
||||||
if !ok || id == "" {
|
|
||||||
c.Status(fiber.StatusUnauthorized).SendString("missing id claim in JWT")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Context().SetUserValue(AccountId, id)
|
|
||||||
return c.Next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAccountId(c *fiber.Ctx) (string, bool) {
|
|
||||||
id, ok := c.Context().UserValue(AccountId).(string)
|
|
||||||
return id, ok
|
|
||||||
}
|
|
@ -3,8 +3,8 @@ package service
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/middleware"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -2,9 +2,9 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/quiz"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/quiz"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/middleware"
|
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
@ -3,10 +3,10 @@ package service
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/result"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/result"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/middleware"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/tools"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/pkg"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -46,9 +46,10 @@ func (s *Service) GetResultsByQuizID(ctx *fiber.Ctx) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
||||||
}
|
}
|
||||||
|
if _, cnt := account.Privileges["quizCnt"]; !cnt {
|
||||||
if len(account.Privileges) == 0 {
|
if _, unlim := account.Privileges["quizUnlimTime"]; !unlim {
|
||||||
payment = false
|
payment = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
results, totalCount, err := s.dal.ResultRepo.GetQuizResults(ctx.Context(), quizID, result.GetQuizResDeps{
|
results, totalCount, err := s.dal.ResultRepo.GetQuizResults(ctx.Context(), quizID, result.GetQuizResDeps{
|
||||||
@ -152,8 +153,10 @@ func (s *Service) ExportResultsToCSV(ctx *fiber.Ctx) error {
|
|||||||
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(account.Privileges) == 0 {
|
if _, cnt := account.Privileges["quizCnt"]; !cnt {
|
||||||
return ctx.Status(fiber.StatusPaymentRequired).SendString("payment required")
|
if _, unlim := account.Privileges["quizUnlimTime"]; !unlim {
|
||||||
|
return ctx.Status(fiber.StatusPaymentRequired).SendString("payment required")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
questions, err := s.dal.ResultRepo.GetQuestions(ctx.Context(), quizID)
|
questions, err := s.dal.ResultRepo.GetQuestions(ctx.Context(), quizID)
|
||||||
@ -174,7 +177,7 @@ func (s *Service) ExportResultsToCSV(ctx *fiber.Ctx) error {
|
|||||||
|
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
|
|
||||||
if err := pkg.WriteDataToExcel(buffer, questions, answers); err != nil {
|
if err := tools.WriteDataToExcel(buffer, questions, answers); err != nil {
|
||||||
return ctx.Status(fiber.StatusInternalServerError).SendString("failed to write data to Excel")
|
return ctx.Status(fiber.StatusInternalServerError).SendString("failed to write data to Excel")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,8 +203,10 @@ func (s *Service) GetResultAnswers(ctx *fiber.Ctx) error {
|
|||||||
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(account.Privileges) == 0 {
|
if _, cnt := account.Privileges["quizCnt"]; !cnt {
|
||||||
return ctx.Status(fiber.StatusPaymentRequired).SendString("payment required")
|
if _, unlim := account.Privileges["quizUnlimTime"]; !unlim {
|
||||||
|
return ctx.Status(fiber.StatusPaymentRequired).SendString("payment required")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
answers, err := s.dal.ResultRepo.GetResultAnswers(ctx.Context(), resultID)
|
answers, err := s.dal.ResultRepo.GetResultAnswers(ctx.Context(), resultID)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package pkg
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/tealeg/xlsx"
|
"github.com/tealeg/xlsx"
|
Loading…
Reference in New Issue
Block a user