From 910fb81a0e08c39fb164c30c8de2175c6d63ab16 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Fri, 31 Jan 2020 12:01:29 +0300 Subject: [PATCH 1/2] update error response --- authenticate/authenticate.go | 58 ++++++++++++---------- service/veritification/cacheable_verify.go | 2 +- service/veritification/error.go | 26 ++++++++++ service/veritification/runtime_verify.go | 49 +++++++++--------- service/veritification/verify.go | 2 +- 5 files changed, 85 insertions(+), 52 deletions(-) create mode 100644 service/veritification/error.go diff --git a/authenticate/authenticate.go b/authenticate/authenticate.go index fc4dfdf..8f146ea 100644 --- a/authenticate/authenticate.go +++ b/authenticate/authenticate.go @@ -61,10 +61,10 @@ func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) { ) if len(appToken) == 0 { - return 0, createError("unauthorized", codes.Unauthenticated) + return 0, createError("unauthorized", codes.Unauthenticated, "application token required") } else if config.GetRemote().(*conf.RemoteConfig).TokensSetting.ApplicationVerify { if appId, err = verifyToken.Application(string(appToken)); err != nil || appId == 0 { - return 0, createError("unauthorized", codes.Unauthenticated, "invalid token") + return 0, createError("unauthorized", codes.Unauthenticated, "received invalid application token") } } @@ -74,7 +74,7 @@ func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) { return 0, createError("internal Server error", codes.Internal) } if len(verifiableKeys) != 4 { - return 0, createError("unauthorized", codes.Unauthenticated, "unknown token") + return 0, createError("unauthorized", codes.Unauthenticated, "received unexpected identities") } applicationId, err := strconv.Atoi(verifiableKeys[utils.ApplicationIdHeader]) @@ -83,34 +83,35 @@ func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) { return 0, createError("internal Server error", codes.Internal) } if appId != -1 && int32(applicationId) != appId { - return 0, createError("unauthorized", codes.Unauthenticated, "unknown application identity") + return 0, createError("unauthorized", codes.Unauthenticated, "received unexpected application identity") } - verifiableKeys[utils.DeviceTokenHeader] = string(getParam(utils.DeviceTokenHeader, &ctx.Request)) - verifiableKeys[utils.UserTokenHeader] = string(getParam(utils.UserTokenHeader, &ctx.Request)) - - if verifiableKeys[utils.UserTokenHeader] != "" { - userId, err := verifyToken.User(verifiableKeys[utils.UserTokenHeader]) + userToken := string(getParam(utils.UserTokenHeader, &ctx.Request)) + if userToken != "" { + userId, err := verifyToken.User(userToken) if err != nil { - return 0, createError("forbidden", codes.PermissionDenied) + return 0, createError("unauthorized", codes.Unauthenticated, "received invalid user token") } verifiableKeys[utils.UserIdHeader] = userId } - permittedToCall := false - validUserId := true - verifiableKeys, permittedToCall, validUserId, err = auth.verify.Identity(verifiableKeys, path) + verifiableKeys[utils.UserTokenHeader] = userToken + verifiableKeys[utils.DeviceTokenHeader] = string(getParam(utils.DeviceTokenHeader, &ctx.Request)) + verifiableKeys, err = auth.verify.Identity(verifiableKeys, path) if err != nil { - log.Error(log_code.ErrorAuthenticate, err) - return 0, createError("internal server error", codes.Internal) - } - if !validUserId { - msg := "doesn't match user id" - log.Error(log_code.ErrorAuthenticate, msg) - return 0, createError(msg, codes.PermissionDenied) - } - if permittedToCall { - return 0, createError("forbidden", codes.PermissionDenied) + switch e := err.(type) { + case veritification.Error: + switch e.Code { + case veritification.ErrorCodeInvalidUserId: + return 0, createError("unauthorized", codes.Unauthenticated, e.Error()) + case veritification.ErrorCodePermittedToCall: + return 0, createError("forbidden", codes.PermissionDenied, e.Error()) + default: + return 0, createError("internal server error", codes.Internal) + } + default: + return 0, createError("internal server error", codes.Internal) + } } for key, value := range verifiableKeys { @@ -120,14 +121,17 @@ func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) { } if _, ok := routing.InnerMethods[path]; ok { - adminToken := getParam(AdminTokenHeader, &ctx.Request) - if verifyToken.Admin(string(adminToken)) != nil { - return 0, createError("forbidden", codes.PermissionDenied) + adminToken := string(getParam(AdminTokenHeader, &ctx.Request)) + if adminToken == "" { + return 0, createError("unauthorized", codes.Unauthenticated, "admin token required") + } + if verifyToken.Admin(adminToken) != nil { + return 0, createError("unauthorized", codes.Unauthenticated, "received invalid admin token") } } if verifiableKeys[utils.UserIdHeader] == "" { if _, ok := routing.AuthUserMethods[path]; ok { - return 0, createError("forbidden", codes.PermissionDenied) + return 0, createError("unauthorized", codes.Unauthenticated, "user token required") } } diff --git a/service/veritification/cacheable_verify.go b/service/veritification/cacheable_verify.go index 9a4e11d..473db43 100644 --- a/service/veritification/cacheable_verify.go +++ b/service/veritification/cacheable_verify.go @@ -47,6 +47,6 @@ func (cv *cacheableVerify) ApplicationToken(token string) (map[string]string, er } } -func (cv *cacheableVerify) Identity(identity map[string]string, uri string) (map[string]string, bool, bool, error) { +func (cv *cacheableVerify) Identity(identity map[string]string, uri string) (map[string]string, error) { return cv.rv.Identity(identity, uri) } diff --git a/service/veritification/error.go b/service/veritification/error.go new file mode 100644 index 0000000..a913c4e --- /dev/null +++ b/service/veritification/error.go @@ -0,0 +1,26 @@ +package veritification + +const ( + ErrorCodeInvalidUserId code = 0 + ErrorCodePermittedToCall code = 1 +) + +type ( + code int + + Error struct { + Code code + message string + } +) + +func (e Error) Error() string { + return e.message +} + +func newError(code code, message string) Error { + return Error{ + Code: code, + message: message, + } +} diff --git a/service/veritification/runtime_verify.go b/service/veritification/runtime_verify.go index c154971..e31fc3b 100644 --- a/service/veritification/runtime_verify.go +++ b/service/veritification/runtime_verify.go @@ -53,13 +53,11 @@ func (v *runtimeVerify) ApplicationToken(token string) (map[string]string, error } } -func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]string, bool, bool, error) { +func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]string, error) { secondDbKey := fmt.Sprintf("%s|%s", t[utils.ApplicationIdHeader], uri) thirdDbKey := fmt.Sprintf("%s|%s", t[utils.UserTokenHeader], t[utils.DomainIdHeader]) fourthDbKey := fmt.Sprintf("%s|%s", t[utils.UserIdHeader], uri) fifthDbKey := fmt.Sprintf("%s|%s", t[utils.DeviceTokenHeader], t[utils.DomainIdHeader]) - permittedToCall := false - validUserId := true if resp, err := rdClient.Client.Get().Pipelined(func(p rd.Pipeliner) error { if _, err := p.Select(int(redis.ApplicationPermissionDb)).Result(); v.notEmptyError(err) { @@ -92,36 +90,41 @@ func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]st return nil }); v.notEmptyError(err) { - return t, false, false, err + return t, err } else { // ===== NOT PERMITTED BY APPLICATION ID ===== - if msg, err := v.findStringCmd(resp, 1); err != nil { - return t, false, false, err - } else if msg == permittedToCallInfo { - permittedToCall = true + msg, err := v.findStringCmd(resp, 1) + if err != nil { + return t, err + } + if msg == permittedToCallInfo { + return t, newError(ErrorCodePermittedToCall, "application has no rights to call this method") } // ===== CHECK USER TOKEN ===== - if msg, err := v.findStringCmd(resp, 3); err != nil { - return t, false, false, err - } else { - if msg != t[utils.UserIdHeader] { - validUserId = false - } + msg, err = v.findStringCmd(resp, 3) + if err != nil { + return t, err + } + userIdentity, found := t[utils.UserIdHeader] + if found && msg != userIdentity { + return t, newError(ErrorCodeInvalidUserId, "received unexpected user identity") } // ===== NOT PERMITTED BY USER ID ===== - if msg, err := v.findStringCmd(resp, 5); err != nil { - return t, false, false, err - } else if msg == permittedToCallInfo { - permittedToCall = true + msg, err = v.findStringCmd(resp, 5) + if err != nil { + return t, err + } + if msg == permittedToCallInfo { + return t, newError(ErrorCodePermittedToCall, "user has no rights to call this method") } // ===== CHECK DEVICE TOKEN ===== - if msg, err := v.findStringCmd(resp, 7); err != nil { - return t, false, false, err - } else { - t[utils.DeviceIdHeader] = msg + msg, err = v.findStringCmd(resp, 7) + if err != nil { + return t, err } + t[utils.DeviceIdHeader] = msg } - return t, permittedToCall, validUserId, nil + return t, nil } func (v *runtimeVerify) notEmptyError(err error) bool { diff --git a/service/veritification/verify.go b/service/veritification/verify.go index 4286929..37e62d7 100644 --- a/service/veritification/verify.go +++ b/service/veritification/verify.go @@ -8,7 +8,7 @@ var Default = NewRuntimeVerify() type Verify interface { ApplicationToken(string) (map[string]string, error) - Identity(map[string]string, string) (map[string]string, bool, bool, error) + Identity(map[string]string, string) (map[string]string, error) } func NewRuntimeVerify() Verify { From e0ab3236d41b1c3334ce272ec671bad8cecaf3a3 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Fri, 31 Jan 2020 15:51:16 +0300 Subject: [PATCH 2/2] ref --- authenticate/authenticate.go | 15 ++++-------- service/veritification/error.go | 29 +++++------------------- service/veritification/runtime_verify.go | 6 ++--- 3 files changed, 14 insertions(+), 36 deletions(-) diff --git a/authenticate/authenticate.go b/authenticate/authenticate.go index 8f146ea..3896176 100644 --- a/authenticate/authenticate.go +++ b/authenticate/authenticate.go @@ -99,16 +99,11 @@ func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) { verifiableKeys[utils.DeviceTokenHeader] = string(getParam(utils.DeviceTokenHeader, &ctx.Request)) verifiableKeys, err = auth.verify.Identity(verifiableKeys, path) if err != nil { - switch e := err.(type) { - case veritification.Error: - switch e.Code { - case veritification.ErrorCodeInvalidUserId: - return 0, createError("unauthorized", codes.Unauthenticated, e.Error()) - case veritification.ErrorCodePermittedToCall: - return 0, createError("forbidden", codes.PermissionDenied, e.Error()) - default: - return 0, createError("internal server error", codes.Internal) - } + switch err { + case veritification.ErrorInvalidUserId: + return 0, createError("unauthorized", codes.Unauthenticated, err.Error()) + case veritification.ErrorPermittedToCallUser, veritification.ErrorPermittedToCallApplication: + return 0, createError("forbidden", codes.PermissionDenied, err.Error()) default: return 0, createError("internal server error", codes.Internal) } diff --git a/service/veritification/error.go b/service/veritification/error.go index a913c4e..b5cc9d4 100644 --- a/service/veritification/error.go +++ b/service/veritification/error.go @@ -1,26 +1,9 @@ package veritification -const ( - ErrorCodeInvalidUserId code = 0 - ErrorCodePermittedToCall code = 1 +import "errors" + +var ( + ErrorInvalidUserId = errors.New("received unexpected user identity") + ErrorPermittedToCallApplication = errors.New("application has no rights to call this method") + ErrorPermittedToCallUser = errors.New("user has no rights to call this method") ) - -type ( - code int - - Error struct { - Code code - message string - } -) - -func (e Error) Error() string { - return e.message -} - -func newError(code code, message string) Error { - return Error{ - Code: code, - message: message, - } -} diff --git a/service/veritification/runtime_verify.go b/service/veritification/runtime_verify.go index e31fc3b..f8847a2 100644 --- a/service/veritification/runtime_verify.go +++ b/service/veritification/runtime_verify.go @@ -98,7 +98,7 @@ func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]st return t, err } if msg == permittedToCallInfo { - return t, newError(ErrorCodePermittedToCall, "application has no rights to call this method") + return t, ErrorPermittedToCallApplication } // ===== CHECK USER TOKEN ===== msg, err = v.findStringCmd(resp, 3) @@ -107,7 +107,7 @@ func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]st } userIdentity, found := t[utils.UserIdHeader] if found && msg != userIdentity { - return t, newError(ErrorCodeInvalidUserId, "received unexpected user identity") + return t, ErrorInvalidUserId } // ===== NOT PERMITTED BY USER ID ===== msg, err = v.findStringCmd(resp, 5) @@ -115,7 +115,7 @@ func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]st return t, err } if msg == permittedToCallInfo { - return t, newError(ErrorCodePermittedToCall, "user has no rights to call this method") + return t, ErrorPermittedToCallUser } // ===== CHECK DEVICE TOKEN ===== msg, err = v.findStringCmd(resp, 7)