From ed7a49ef04382b8edf730d6a4f655fe035b6efbc Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Thu, 21 Oct 2021 17:02:56 +0300 Subject: [PATCH] MSP-325 add ids return from authentication --- authenticate/authenticate.go | 18 +++++++++++------- authenticate/token.go | 18 +++++++++++++++--- handler/handler.go | 25 +++++++++++++++---------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/authenticate/authenticate.go b/authenticate/authenticate.go index 3e14b1b..fcca417 100644 --- a/authenticate/authenticate.go +++ b/authenticate/authenticate.go @@ -42,6 +42,7 @@ type ( verifiable struct { appId int32 + adminId int64 ctx *fasthttp.RequestCtx path string headers map[string]string @@ -60,7 +61,7 @@ func ReceiveConfiguration(conf conf.Cache) { } } -func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) { +func Do(ctx *fasthttp.RequestCtx, path string) (int32, int64, error) { for _, notExpectedHeader := range notExpectedHeaders { ctx.Request.Header.Del(notExpectedHeader) } @@ -68,27 +69,27 @@ func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) { verifiable := newVerifiable(ctx, path) err := verifiable.verifyAppToken() if err != nil { - return 0, err + return 0, 0, err } err = verifiable.verifyUserToken() if err != nil { - return 0, err + return 0, 0, err } verifiable.setHeaders() err = verifiable.checkInnerMethods() if err != nil { - return 0, err + return 0, 0, err } err = verifiable.checkUserMethods() if err != nil { - return 0, err + return 0, 0, err } - return verifiable.appId, nil + return verifiable.appId, verifiable.adminId, nil } func newVerifiable(ctx *fasthttp.RequestCtx, path string) *verifiable { @@ -96,6 +97,7 @@ func newVerifiable(ctx *fasthttp.RequestCtx, path string) *verifiable { ctx: ctx, path: path, appId: -1, + adminId: -1, headers: make(map[string]string), } } @@ -179,9 +181,11 @@ func (v *verifiable) checkInnerMethods() error { if adminToken == "" { return createError("unauthorized", codes.Unauthenticated, "admin token required") } - if verifyToken.Admin(adminToken) != nil { + adminId, err := verifyToken.Admin(adminToken) + if err != nil { return createError("unauthorized", codes.Unauthenticated, "received invalid admin token") } + v.adminId = adminId } return nil } diff --git a/authenticate/token.go b/authenticate/token.go index 881a736..4e7828f 100644 --- a/authenticate/token.go +++ b/authenticate/token.go @@ -23,12 +23,24 @@ type userClaims struct { UserId int64 } +type adminClaims struct { + *jwt.StandardClaims + Id int64 +} + type token struct{} -func (t token) Admin(token string) error { +func (t token) Admin(token string) (int64, error) { secret := config.GetRemote().(*conf.RemoteConfig).TokensSetting.AdminSecret - _, err := t.parse(token, secret, jwt.MapClaims{}) - return err + parsed, err := t.parse(token, secret, &adminClaims{StandardClaims: &jwt.StandardClaims{}}) + if err != nil { + return 0, err + } + claims, ok := parsed.Claims.(*adminClaims) + if !ok { + return 0, errInvalidToken + } + return claims.Id, nil } func (t token) Application(token string) (int32, error) { diff --git a/handler/handler.go b/handler/handler.go index 60e4fd6..6914dd2 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -41,7 +41,7 @@ func New(logger log.Logger) *Handler { func (h Handler) CompleteRequest(ctx *fasthttp.RequestCtx) { currentTime := time.Now() - method, resp := h.authenticateAccountingProxy(ctx) + _, _, method, resp := h.authenticateAccountingProxy(ctx) executionTime := time.Since(currentTime) / execution @@ -69,26 +69,31 @@ func (h Handler) CompleteRequest(ctx *fasthttp.RequestCtx) { } } -func (h Handler) authenticateAccountingProxy(ctx *fasthttp.RequestCtx) (string, domain.ProxyResponse) { - initialPath := string(ctx.Path()) +func (h Handler) authenticateAccountingProxy(ctx *fasthttp.RequestCtx) (int32, int64, string, domain.ProxyResponse) { + var ( + appId int32 = -1 + adminId int64 = -1 + initialPath = string(ctx.Path()) + ) p, path := proxy.Find(initialPath) if p == nil { msg := fmt.Sprintf("unknown proxy for '%s'", initialPath) utils.WriteError(ctx, msg, codes.NotFound, nil) - return initialPath, domain.Create().SetError(errors.New(msg)) + return appId, adminId, initialPath, domain.Create().SetError(errors.New(msg)) } if !p.SkipExistCheck() { if _, ok := routing.AllMethods[path]; !ok { msg := "not implemented" utils.WriteError(ctx, msg, codes.Unimplemented, nil) - return path, domain.Create().SetError(errors.New(msg)) + return appId, adminId, path, domain.Create().SetError(errors.New(msg)) } } if !p.SkipAuth() { - applicationId, err := authenticate.Do(ctx, path) + var err error + appId, adminId, err = authenticate.Do(ctx, path) if err != nil { message := "unknown error" status := codes.Unknown @@ -99,17 +104,17 @@ func (h Handler) authenticateAccountingProxy(ctx *fasthttp.RequestCtx) (string, details = e.Details() } utils.WriteError(ctx, message, status, details) - return path, domain.Create().SetError(err) + return appId, adminId, path, domain.Create().SetError(err) } - if !accounting.AcceptRequest(applicationId, path) { + if !accounting.AcceptRequest(appId, path) { err := errAccounting utils.WriteError(ctx, "too many requests", codes.ResourceExhausted, nil) - return path, domain.Create().SetError(err) + return appId, adminId, path, domain.Create().SetError(err) } } - return path, p.ProxyRequest(ctx, path) + return appId, adminId, path, p.ProxyRequest(ctx, path) } func (Handler) setMetricStatus(statusCode int) string {