mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2026-11-16 08:08:04 +00:00
Backported fixes from another similar projecT
This commit is contained in:
1 parent
72e4a78fbf
commit
2b3bef186c
21 files changed
+225
-35
No files matched your search
Vendored
+1
@@ -75,6 +75,7 @@ pipeline {
|
||||
sh '''docker build --pull --no-cache \\
|
||||
-t "${IMAGE}:${BRANCH_LOWER}-ci-${BUILD_NUMBER}" \\
|
||||
-f docker/Dockerfile \\
|
||||
--progress=plain \\
|
||||
--build-arg BUILD_COMMIT="${BUILD_COMMIT}" \\
|
||||
--build-arg BUILD_DATE="$(date '+%Y-%m-%d %T %Z')" \\
|
||||
--build-arg BUILD_VERSION="${BUILD_VERSION}" \\
|
||||
|
||||
+1
-1
@@ -9,7 +9,6 @@ require (
|
||||
github.com/alexflint/go-arg v1.5.1
|
||||
github.com/amacneil/dbmate/v2 v2.20.0
|
||||
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/drexedam/gravatar v0.0.0-20210327211422-e94eea8c338e
|
||||
github.com/fatih/color v1.18.0
|
||||
github.com/glebarez/sqlite v1.11.0
|
||||
@@ -17,6 +16,7 @@ require (
|
||||
github.com/go-chi/cors v1.2.2
|
||||
github.com/go-chi/jwtauth/v5 v5.3.3
|
||||
github.com/go-ldap/ldap/v3 v3.4.11
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1
|
||||
github.com/jc21/go-sse v1.7.0
|
||||
github.com/jc21/jsref v0.0.0-20250501111625-0ce4620b7d96
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
|
||||
+2
-2
@@ -18,8 +18,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/drexedam/gravatar v0.0.0-20210327211422-e94eea8c338e h1:2R8DvYLNr5DL25eWwpOdPno1eIbTNjJC0d7v8ti5cus=
|
||||
github.com/drexedam/gravatar v0.0.0-20210327211422-e94eea8c338e/go.mod h1:YjikoytuRI4q+GRd3xrOrKJN+Ayi2dwRomHLDDeMHfs=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
@@ -44,6 +42,8 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
||||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
|
||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
|
||||
|
||||
@@ -201,9 +201,23 @@ func newTokenLDAP(w http.ResponseWriter, r *http.Request, payload tokenPayload)
|
||||
// Route: POST /auth/refresh
|
||||
func RefreshToken() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: Use your own methods to verify an existing user is
|
||||
// able to refresh their token and then give them a new one
|
||||
userObj, _ := user.GetByEmail("jc@jc21.com")
|
||||
userID, ok := r.Context().Value(c.UserIDCtxKey).(uint)
|
||||
if !ok {
|
||||
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
|
||||
return
|
||||
}
|
||||
|
||||
userObj, userErr := user.GetByID(userID)
|
||||
if userErr != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if userObj.IsDisabled {
|
||||
h.ResultErrorJSON(w, r, http.StatusUnauthorized, errors.ErrUserDisabled.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
if response, err := njwt.Generate(&userObj, false); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusInternalServerError, err.Error(), nil)
|
||||
} else {
|
||||
|
||||
@@ -12,6 +12,11 @@ import (
|
||||
|
||||
const defaultLimit = 10
|
||||
|
||||
// maxLimit caps ?limit= on every paginated list endpoint - without a cap, a
|
||||
// client could ask for an arbitrarily large page and force the server to
|
||||
// pull an unbounded result set in one query.
|
||||
const maxLimit = 500
|
||||
|
||||
func getPageInfoFromRequest(r *http.Request) (model.PageInfo, error) {
|
||||
pageInfo := model.PageInfo{}
|
||||
var err error
|
||||
@@ -99,6 +104,9 @@ func getPagination(r *http.Request) (int, int, error) {
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
if limit > maxLimit {
|
||||
limit = maxLimit
|
||||
}
|
||||
|
||||
return offset, limit, nil
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const something = "something"
|
||||
|
||||
func TestGetPageInfoFromRequest(t *testing.T) {
|
||||
t.Run("basic test", func(t *testing.T) {
|
||||
r := httptest.NewRequest(http.MethodGet, "/hosts", nil)
|
||||
@@ -28,6 +30,25 @@ func TestGetPageInfoFromRequest(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetPageInfoFromRequestClampsLimit(t *testing.T) {
|
||||
// Without this clamp, ?limit= passed straight through to the DB query
|
||||
// unbounded would let a client force an arbitrarily large result set -
|
||||
// see maxLimit's own doc comment.
|
||||
r := httptest.NewRequest(http.MethodGet, "/hosts?limit=999999999", nil)
|
||||
p, err := getPageInfoFromRequest(r)
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, maxLimit, p.Limit)
|
||||
}
|
||||
|
||||
func TestGetPageInfoFromRequestAllowsLimitUnderTheCap(t *testing.T) {
|
||||
r := httptest.NewRequest(http.MethodGet, "/hosts?limit=200", nil)
|
||||
p, err := getPageInfoFromRequest(r)
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, 200, p.Limit)
|
||||
}
|
||||
|
||||
func TestGetQueryVarInt(t *testing.T) {
|
||||
type want struct {
|
||||
val int
|
||||
@@ -45,7 +66,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "simple default",
|
||||
url: "/hosts",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: false,
|
||||
defaultValue: 100,
|
||||
want: want{
|
||||
@@ -56,7 +77,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "required flag",
|
||||
url: "/hosts",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 0,
|
||||
@@ -66,7 +87,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "simple get",
|
||||
url: "/hosts?something=50",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 50,
|
||||
@@ -76,7 +97,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "invalid number",
|
||||
url: "/hosts?something=aaa",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 0,
|
||||
@@ -86,7 +107,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "preceding zeros",
|
||||
url: "/hosts?something=0000050",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 50,
|
||||
@@ -96,7 +117,7 @@ func TestGetQueryVarInt(t *testing.T) {
|
||||
{
|
||||
name: "decimals",
|
||||
url: "/hosts?something=50.50",
|
||||
queryVar: "something",
|
||||
queryVar: something,
|
||||
required: true,
|
||||
want: want{
|
||||
val: 0,
|
||||
|
||||
@@ -9,6 +9,6 @@ import (
|
||||
// NotAllowed is a json error handler for when method is not allowed
|
||||
func NotAllowed() func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
h.ResultErrorJSON(w, r, http.StatusNotFound, "Not allowed", nil)
|
||||
h.ResultErrorJSON(w, r, http.StatusMethodNotAllowed, "Not allowed", nil)
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,7 @@ func NotFound() func(http.ResponseWriter, *http.Request) {
|
||||
defaultFile := "index.html"
|
||||
path := strings.TrimLeft(r.URL.Path, "/")
|
||||
|
||||
isAPI := false
|
||||
if len(path) >= 3 && path[0:3] == "api" {
|
||||
isAPI = true
|
||||
}
|
||||
isAPI := len(path) >= 4 && path[0:4] == "api/"
|
||||
|
||||
if path == "" {
|
||||
path = defaultFile
|
||||
|
||||
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
@@ -85,6 +86,8 @@ func UpdateUser() func(http.ResponseWriter, *http.Request) {
|
||||
case nil:
|
||||
// nolint: errcheck,gosec
|
||||
userObject.Expand([]string{"capabilities"})
|
||||
hadFullAdmin := slices.Contains(userObject.Capabilities, user.CapabilityFullAdmin)
|
||||
|
||||
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
|
||||
err := json.Unmarshal(bodyBytes, &userObject)
|
||||
if err != nil {
|
||||
@@ -97,6 +100,16 @@ func UpdateUser() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Only reachable via the admin (UpdateUserAdmin-schema'd) route,
|
||||
// since the self-service schema never allows "capabilities" in
|
||||
// the payload at all - but an admin can still target their own
|
||||
// numeric id, so guard it explicitly rather than relying on
|
||||
// that alone.
|
||||
if self && hadFullAdmin && !slices.Contains(userObject.Capabilities, user.CapabilityFullAdmin) {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, "You cannot remove your own full-admin capability!", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = userObject.Save(); err != nil {
|
||||
if err == errors.ErrDuplicateEmailUser || err == errors.ErrSystemUserReadonly {
|
||||
h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil)
|
||||
|
||||
@@ -48,9 +48,15 @@ func Enforce(permissions ...string) func(http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
userID := uint(claims["uid"].(float64))
|
||||
uidClaim, ok := claims["uid"].(float64)
|
||||
if token == nil || !ok {
|
||||
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
|
||||
return
|
||||
}
|
||||
|
||||
userID := uint(uidClaim)
|
||||
_, enabled, _ := user.IsEnabled(userID)
|
||||
if token == nil || !enabled {
|
||||
if !enabled {
|
||||
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,16 +6,30 @@ import (
|
||||
"net/http"
|
||||
|
||||
c "npm/internal/api/context"
|
||||
h "npm/internal/api/http"
|
||||
)
|
||||
|
||||
// BodyContext simply adds the body data to a context item
|
||||
// maxRequestBodyBytes bounds how much of a request body this middleware
|
||||
// will ever buffer into memory. Without a cap here, a client could send an
|
||||
// arbitrarily large body and have it read fully into memory before schema
|
||||
// validation (or even auth) ever runs, since this middleware sits ahead of
|
||||
// both in the chain - a trivial, pre-auth memory-exhaustion vector.
|
||||
const maxRequestBodyBytes = 1 << 20 // 1 MiB
|
||||
|
||||
// BodyContext reads the request body once (bounded by maxRequestBodyBytes)
|
||||
// and stashes it on the context. A body over the limit is rejected with
|
||||
// 413, not silently truncated.
|
||||
func BodyContext() func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Grab the Body Data
|
||||
var body []byte
|
||||
if r.Body != nil {
|
||||
body, _ = io.ReadAll(r.Body)
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodyBytes)
|
||||
var err error
|
||||
if body, err = io.ReadAll(r.Body); err != nil {
|
||||
h.ResultErrorJSON(w, r, http.StatusRequestEntityTooLarge, "Request body too large", nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
// Add it to the context
|
||||
ctx := r.Context()
|
||||
|
||||
@@ -41,3 +41,27 @@ func TestBodyContext(t *testing.T) {
|
||||
status := rr.Code
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
}
|
||||
|
||||
func TestBodyContextRejectsOversizedBody(t *testing.T) {
|
||||
// goleak is used to detect goroutine leaks
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
|
||||
|
||||
// One byte over BodyContext's own maxRequestBodyBytes (1 MiB) cap
|
||||
oversized := (1 << 20) + 1
|
||||
body := bytes.Repeat([]byte("a"), oversized)
|
||||
req, err := http.NewRequest("POST", "/test", bytes.NewBuffer(body))
|
||||
assert.Nil(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
called := false
|
||||
handler := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
|
||||
called = true
|
||||
})
|
||||
|
||||
mw := middleware.BodyContext()(handler)
|
||||
mw.ServeHTTP(rr, req)
|
||||
|
||||
assert.False(t, called, "the wrapped handler must not run once the body is rejected as too large")
|
||||
assert.Equal(t, http.StatusRequestEntityTooLarge, rr.Code)
|
||||
}
|
||||
@@ -71,7 +71,6 @@ func Options(routes chi.Router) func(http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodOptions {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, "{}")
|
||||
return
|
||||
|
||||
@@ -58,7 +58,10 @@ func TestOptions(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
assert.Equal(t, "*", rr.Header().Get("Access-Control-Allow-Origin"))
|
||||
// Access-Control-Allow-Origin is deliberately not set here any more -
|
||||
// that's corss.Handler's job alone now (see router.go/cors.go's own
|
||||
// doc comments) - Options() only answers the request body/content-type.
|
||||
assert.Equal(t, "", rr.Header().Get("Access-Control-Allow-Origin"))
|
||||
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"))
|
||||
assert.Equal(t, "{}", rr.Body.String())
|
||||
}
|
||||
|
||||
@@ -39,10 +39,20 @@ func NewRouter() http.Handler {
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Use(
|
||||
// corss.Handler is the one and only place that decides
|
||||
// Access-Control-Allow-Origin - it must run before
|
||||
// middleware.Cors/Options so it can fully answer (and stop the
|
||||
// chain for) a real CORS preflight using the configured allowlist;
|
||||
// those two previously ran first and Options independently
|
||||
// hardcoded a wildcard, which shadowed this properly-configured
|
||||
// library entirely for every preflight request. middleware.Cors/
|
||||
// Options still run afterward for their own, non-CORS purpose
|
||||
// (reporting a matched route's allowed methods, and answering a
|
||||
// bare OPTIONS probe that isn't a browser CORS preflight at all).
|
||||
middleware.AccessControl,
|
||||
corss.Handler,
|
||||
middleware.Cors(r),
|
||||
middleware.Options(r),
|
||||
corss.Handler,
|
||||
chiMiddleware.RealIP,
|
||||
chiMiddleware.Recoverer,
|
||||
chiMiddleware.Throttle(5),
|
||||
|
||||
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
golog "log"
|
||||
"strings"
|
||||
|
||||
"npm/internal/logger"
|
||||
|
||||
@@ -38,7 +39,7 @@ func initLogger() error {
|
||||
// this removes timestamp prefixes from logs
|
||||
golog.SetFlags(0)
|
||||
|
||||
switch Configuration.Log.Level {
|
||||
switch strings.ToLower(Configuration.Log.Level) {
|
||||
case "debug":
|
||||
logLevel = logger.DebugLevel
|
||||
case "warn":
|
||||
|
||||
@@ -3,6 +3,10 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"npm/internal/errors"
|
||||
|
||||
"github.com/rotisserie/eris"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -26,6 +30,34 @@ func (d *db) GetDriver() string {
|
||||
return strings.ToLower(d.Driver)
|
||||
}
|
||||
|
||||
// IsValid is a basic check for config. Sqlite has no host/port/credentials
|
||||
// to validate; postgres and mysql require them to form a usable DSN.
|
||||
func (d *db) IsValid() (bool, error) {
|
||||
var errs []error
|
||||
|
||||
switch d.GetDriver() {
|
||||
case DatabaseSqlite:
|
||||
// no connection fields required
|
||||
case DatabasePostgres, DatabaseMysql:
|
||||
if d.Name == "" {
|
||||
errs = append(errs, eris.New("database name is empty"))
|
||||
}
|
||||
if d.Host == "" {
|
||||
errs = append(errs, eris.New("database host is empty"))
|
||||
}
|
||||
if d.Port <= 0 {
|
||||
errs = append(errs, eris.New("database port is invalid"))
|
||||
}
|
||||
if d.Username == "" {
|
||||
errs = append(errs, eris.New("database username is empty"))
|
||||
}
|
||||
default:
|
||||
errs = append(errs, eris.Errorf("database driver %s is not supported. Valid options are: %s, %s or %s", d.Driver, DatabaseSqlite, DatabasePostgres, DatabaseMysql))
|
||||
}
|
||||
|
||||
return len(errs) == 0, errors.Join(errs...)
|
||||
}
|
||||
|
||||
// GetGormConnectURL is used by Gorm
|
||||
func (d *db) GetGormConnectURL() string {
|
||||
switch d.GetDriver() {
|
||||
|
||||
@@ -20,6 +20,11 @@ var dbInstance *gorm.DB
|
||||
|
||||
// NewDB creates a new connection
|
||||
func NewDB() {
|
||||
if ok, err := config.Configuration.DB.IsValid(); !ok {
|
||||
logger.Error("DatabaseError", eris.Wrap(err, "database configuration is invalid, check for missing environment variables"))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Info("Creating new DB instance using %s", strings.ToLower(config.Configuration.DB.Driver))
|
||||
db, err := connect()
|
||||
if err != nil {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/rotisserie/eris"
|
||||
)
|
||||
|
||||
@@ -18,3 +20,39 @@ var (
|
||||
ErrCABundleDoesNotExist = eris.New("ca-bundle-does-not-exist")
|
||||
ErrProviderNotFound = eris.New("provider_not_found")
|
||||
)
|
||||
|
||||
// Join works the same as golang builtin errors.Join except that it
|
||||
// won't use new lines as separators, it uses commas
|
||||
func Join(errs ...error) error {
|
||||
nonNil := make([]error, 0, len(errs))
|
||||
for _, err := range errs {
|
||||
if err != nil {
|
||||
nonNil = append(nonNil, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(nonNil) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return joinedError{errs: nonNil}
|
||||
}
|
||||
|
||||
type joinedError struct {
|
||||
errs []error
|
||||
}
|
||||
|
||||
func (e joinedError) Error() string {
|
||||
messages := make([]string, len(e.errs))
|
||||
|
||||
for i, err := range e.errs {
|
||||
messages[i] = err.Error()
|
||||
}
|
||||
|
||||
return strings.Join(messages, ", ")
|
||||
}
|
||||
|
||||
// Unwrap lets errors.Is and errors.As inspect every joined error.
|
||||
func (e joinedError) Unwrap() []error {
|
||||
return e.errs
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"npm/internal/entity/user"
|
||||
"npm/internal/logger"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/rotisserie/eris"
|
||||
)
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
type UserJWTClaims struct {
|
||||
UserID uint `json:"uid"`
|
||||
Roles []string `json:"roles"`
|
||||
jwt.StandardClaims
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
// GeneratedResponse is the response of a generated token, usually used in http response
|
||||
@@ -39,9 +39,9 @@ func Generate(userObj *user.Model, forSSE bool) (GeneratedResponse, error) {
|
||||
claims := UserJWTClaims{
|
||||
userObj.ID,
|
||||
[]string{"user"},
|
||||
jwt.StandardClaims{
|
||||
IssuedAt: time.Now().Unix(),
|
||||
ExpiresAt: expires.Unix(),
|
||||
jwt.RegisteredClaims{
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
ExpiresAt: jwt.NewNumericDate(expires),
|
||||
Issuer: issuer,
|
||||
},
|
||||
}
|
||||
@@ -49,8 +49,7 @@ func Generate(userObj *user.Model, forSSE bool) (GeneratedResponse, error) {
|
||||
// Create a new token object, specifying signing method and the claims
|
||||
// you would like it to contain.
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
|
||||
var err error
|
||||
token.Signature, err = token.SignedString(key)
|
||||
signed, err := token.SignedString(key)
|
||||
if err != nil {
|
||||
logger.Error("JWTError", eris.Wrapf(err, "Error signing token: %v", err))
|
||||
return response, err
|
||||
@@ -58,7 +57,7 @@ func Generate(userObj *user.Model, forSSE bool) (GeneratedResponse, error) {
|
||||
|
||||
response = GeneratedResponse{
|
||||
Expires: expires.Unix(),
|
||||
Token: token.Signature,
|
||||
Token: signed,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
|
||||
@@ -142,7 +142,12 @@ func (l *Logger) logLevel(logLevel Level, format string, args ...any) {
|
||||
errorClass := ""
|
||||
if logLevel == ErrorLevel {
|
||||
// First arg is the errorClass
|
||||
errorClass = args[0].(string)
|
||||
if value, ok := args[0].(string); ok {
|
||||
errorClass = value
|
||||
} else {
|
||||
errorClass = ""
|
||||
}
|
||||
|
||||
if len(args) > 1 {
|
||||
args = args[1:]
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user