diff --git a/.version b/.version index 04edabd..c7ba1e8 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -5.4.1 \ No newline at end of file +5.5.0 \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index f23ccc4..85279d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +### v5.5.0 +* Добавлена поддержка basic-auth, где `username` - название приложения, `password` - токен приложения ### v5.4.1 * Оптимизация очистки тела запроса от экранирования unicode символов для корректного логирования ### v5.4.0 diff --git a/domain/auth.go b/domain/auth.go index 1813906..59766ec 100644 --- a/domain/auth.go +++ b/domain/auth.go @@ -1,6 +1,7 @@ package domain type AuthData struct { + AppName string SystemId int DomainId int ServiceId int diff --git a/go.mod b/go.mod index c374923..ea72fa5 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/redis/go-redis/v9 v9.11.0 github.com/stretchr/testify v1.11.1 github.com/tomakado/websocketproxy v0.1.0 - github.com/txix-open/isp-kit v1.57.1 + github.com/txix-open/isp-kit v1.62.0 github.com/txix-open/jsonschema v1.3.0 golang.org/x/net v0.44.0 google.golang.org/genproto/googleapis/rpc v0.0.0-20250908214217-97024824d090 diff --git a/go.sum b/go.sum index 9f94477..87bae38 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/getsentry/sentry-go v0.35.2 h1:jKuujpRwa8FFRYMIwwZpu83Xh0voll9bmvyc63 github.com/getsentry/sentry-go v0.35.2/go.mod h1:mdL49ixwT2yi57k5eh7mpnDyPybixPzlzEJFu0Z76QA= github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= +github.com/go-faker/faker/v4 v4.6.1 h1:xUyVpAjEtB04l6XFY0V/29oR332rOSPWV4lU8RwDt4k= +github.com/go-faker/faker/v4 v4.6.1/go.mod h1:arSdxNCSt7mOhdk8tEolvHeIJ7eX4OX80wXjKKvkKBY= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -123,8 +125,8 @@ github.com/txix-open/etp/v4 v4.0.1 h1:5VSYgGsvnMz0tvUHsjVWFETI0qWBTCF1O3ARCHaCIg github.com/txix-open/etp/v4 v4.0.1/go.mod h1:FIu7TUDdwcgmtmF6tFTV1bGA2fRY/iVaPQdsGzvqYc8= github.com/txix-open/grmq v1.9.0 h1:ArDbJJcFX9zXKEcPf49rp9nYiRyGMJxGKJAxdbSsfaM= github.com/txix-open/grmq v1.9.0/go.mod h1:EyFOP17KrwEFOUjR+PH8iH3LCtiN9TSQ8t0ysyk62po= -github.com/txix-open/isp-kit v1.57.1 h1:EltTEs9VZT8eqUrrRt5EqWAPdXFvw1XL2/EDN8xfdUU= -github.com/txix-open/isp-kit v1.57.1/go.mod h1:Mbprn7ExedjRg4yUPa9xShYx1oMJIU+a19x6ksVubaw= +github.com/txix-open/isp-kit v1.62.0 h1:SfY8x5/3yYEicr5phGTJBaUwFvgktSIsD/NubrKYbg4= +github.com/txix-open/isp-kit v1.62.0/go.mod h1:hyCFmWF0qoVY2loTLyHJx8QPZJuULntG0JfthqFCdsA= github.com/txix-open/jsonschema v1.3.0 h1:XGuOTg22G7PdqfKypjajM20/i5FyGfFYsJKmheebKi4= github.com/txix-open/jsonschema v1.3.0/go.mod h1:l8YDZ1nvJrw6uxWowSVOxCV/ebiMJyapffW87ZEqH00= github.com/txix-open/validator/v10 v10.0.0-20250506161033-f8ce404fffdb h1:UJgT4u/QMv5QHKOQeJ7igShHa36c2/vIRqJiRLdDlf0= diff --git a/middleware/authenticate.go b/middleware/authenticate.go index ced3821..a5c5890 100644 --- a/middleware/authenticate.go +++ b/middleware/authenticate.go @@ -21,7 +21,10 @@ type Authenticator interface { func Authenticate(authenticator Authenticator) Middleware { return func(next Handler) Handler { return HandlerFunc(func(ctx *request.Context) error { - token := ctx.Param(applicationTokenHeader) + token, appName, err := extractToken(ctx) + if err != nil { + return err + } if token == "" { return httperrors.New( http.StatusUnauthorized, @@ -32,18 +35,43 @@ func Authenticate(authenticator Authenticator) Middleware { resp, err := authenticator.Authenticate(ctx.Context(), token) if err != nil { - return errors.WithMessagef(err, "authenticate: authenticate") + return errors.WithMessage(err, "authenticate: authenticator error") } + if !resp.Authenticated { return httperrors.New( http.StatusUnauthorized, "invalid application token", - errors.WithMessage(errors.New(resp.ErrorReason), "authenticate: authenticate"), + errors.Errorf("authenticate: %s", resp.ErrorReason), ) } - ctx.Authenticate(request.AuthData(*resp.AuthData)) + if appName != "" && resp.AuthData.AppName != appName { + return httperrors.New( + http.StatusUnauthorized, + "invalid application token", + errors.New("authenticate: application name mismatch"), + ) + } + + ctx.Authenticate(request.AuthData(*resp.AuthData)) return next.Handle(ctx) }) } } + +func extractToken(ctx *request.Context) (string, string, error) { + appName, token, ok := ctx.Request().BasicAuth() + if !ok { + return ctx.Param(applicationTokenHeader), "", nil + } + + if appName == "" { + return "", "", httperrors.New( + http.StatusUnauthorized, + "application name required", + errors.New("authenticate: application name required on basic auth"), + ) + } + return token, appName, nil +} diff --git a/request/context.go b/request/context.go index ede23ce..04323a8 100644 --- a/request/context.go +++ b/request/context.go @@ -13,6 +13,7 @@ var ( ) type AuthData struct { + AppName string SystemId int DomainId int ServiceId int