update error response
This commit is contained in:
1 parent
f1c597bde9
commit
910fb81a0e
5 files changed
+85
-52
No files matched your search
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user