ref
This commit is contained in:
1 parent
4fdf7c0ca2
commit
5b115440f1
12 files changed
+115
-89
No files matched your search
@@ -2,56 +2,64 @@ package authenticate
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/valyala/fasthttp"
|
||||
"google.golang.org/grpc/codes"
|
||||
"isp-gate-service/log_code"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
verifiableKeyHeaderKeyMap = map[string]string{
|
||||
SystemIdentity: utils.SystemIdHeader,
|
||||
DomainIdentity: utils.DomainIdHeader,
|
||||
ServiceIdentity: utils.ServiceIdHeader,
|
||||
ApplicationIdentity: utils.ApplicationIdHeader,
|
||||
DeviceIdentity: utils.DeviceIdHeader,
|
||||
UserIdentity: utils.UserIdHeader,
|
||||
DeviceToken: utils.DeviceTokenHeader,
|
||||
UserToken: utils.UserTokenHeader,
|
||||
}
|
||||
notExpectedHeaders = []string{
|
||||
utils.SystemIdHeader, utils.DomainIdHeader, utils.ServiceIdHeader, utils.ApplicationIdHeader,
|
||||
systemIdentity: utils.SystemIdHeader,
|
||||
domainIdentity: utils.DomainIdHeader,
|
||||
serviceIdentity: utils.ServiceIdHeader,
|
||||
applicationIdentity: utils.ApplicationIdHeader,
|
||||
}
|
||||
)
|
||||
|
||||
func Do(ctx *fasthttp.RequestCtx) error {
|
||||
for _, notExpectedHeader := range notExpectedHeaders {
|
||||
for _, notExpectedHeader := range verifiableKeyHeaderKeyMap {
|
||||
ctx.Request.Header.Del(notExpectedHeader)
|
||||
}
|
||||
|
||||
appToken := ctx.Request.Header.Peek(utils.ApplicationTokenHeader)
|
||||
if len(appToken) == 0 {
|
||||
return Error.Create(codes.Unauthenticated)
|
||||
return Error.create(codes.Unauthenticated)
|
||||
}
|
||||
verifiableKeys, err := verification.appToken(string(appToken))
|
||||
|
||||
keys, err := verification.appToken(string(appToken))
|
||||
if err != nil {
|
||||
return err
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return Error.create(codes.Internal)
|
||||
}
|
||||
if len(verifiableKeys) != 4 {
|
||||
return Error.Create(codes.Unauthenticated)
|
||||
if len(keys) != 4 {
|
||||
return Error.create(codes.Unauthenticated)
|
||||
}
|
||||
|
||||
verifiableKeys := make(map[string]string)
|
||||
for i, value := range keys {
|
||||
verifiableKeys[verifiableKeyHeaderKeyMap[i]] = value
|
||||
}
|
||||
|
||||
uri := strings.Replace(strings.ToLower(string(ctx.Path())), "/", "", -1)
|
||||
verifiableKeys[DeviceToken] = string(ctx.Request.Header.Peek(utils.DeviceTokenHeader))
|
||||
verifiableKeys[UserToken] = string(ctx.Request.Header.Peek(utils.UserTokenHeader))
|
||||
verifiableKeys[utils.DeviceTokenHeader] = string(ctx.Request.Header.Peek(utils.DeviceTokenHeader))
|
||||
verifiableKeys[utils.UserTokenHeader] = string(ctx.Request.Header.Peek(utils.UserTokenHeader))
|
||||
|
||||
verifiableKeys, err = verification.keys(verifiableKeys, uri)
|
||||
if err != nil {
|
||||
return err
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return Error.create(codes.Internal)
|
||||
}
|
||||
if verifiableKeys[permittedToCallInfo] == "0" {
|
||||
return Error.create(codes.PermissionDenied)
|
||||
} else {
|
||||
delete(verifiableKeys, permittedToCallInfo)
|
||||
}
|
||||
|
||||
for key, value := range verifiableKeys {
|
||||
if value != "" {
|
||||
ctx.Request.Header.Set(verifiableKeyHeaderKeyMap[key], value)
|
||||
ctx.Request.Header.Set(key, value)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -7,16 +7,15 @@ import (
|
||||
|
||||
var Error errorHelper
|
||||
|
||||
func (errorHelper) Create(status codes.Code) ErrorDescription {
|
||||
type errorHelper struct{}
|
||||
|
||||
func (errorHelper) create(status codes.Code) ErrorDescription {
|
||||
return ErrorDescription{grpcStatus: status}
|
||||
}
|
||||
|
||||
type (
|
||||
errorHelper struct{}
|
||||
ErrorDescription struct {
|
||||
grpcStatus codes.Code
|
||||
}
|
||||
)
|
||||
type ErrorDescription struct {
|
||||
grpcStatus codes.Code
|
||||
}
|
||||
|
||||
func (e ErrorDescription) Error() string {
|
||||
return fmt.Sprintf("%v", e)
|
||||
|
||||
@@ -5,22 +5,19 @@ import (
|
||||
rd "github.com/go-redis/redis"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/integration-system/isp-lib/redis"
|
||||
"google.golang.org/grpc/codes"
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/conf"
|
||||
rdClient "isp-gate-service/redis"
|
||||
)
|
||||
|
||||
const (
|
||||
SystemIdentity = "1"
|
||||
DomainIdentity = "2"
|
||||
ServiceIdentity = "3"
|
||||
ApplicationIdentity = "4"
|
||||
systemIdentity = "1"
|
||||
domainIdentity = "2"
|
||||
serviceIdentity = "3"
|
||||
applicationIdentity = "4"
|
||||
|
||||
DeviceIdentity = "5"
|
||||
UserIdentity = "6"
|
||||
|
||||
DeviceToken = "7"
|
||||
UserToken = "8"
|
||||
permittedToCallInfo = "permittedToCallInfo"
|
||||
)
|
||||
|
||||
var verification verificationHelper
|
||||
@@ -40,12 +37,12 @@ func (v verificationHelper) appToken(token string) (map[string]string, error) {
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, Error.Create(codes.Internal)
|
||||
return nil, err
|
||||
} else {
|
||||
if len(resp) < 2 || resp[1].Err() == rd.Nil {
|
||||
return nil, nil
|
||||
} else if resp[1].Err() != nil {
|
||||
return nil, Error.Create(codes.Unauthenticated)
|
||||
return nil, err
|
||||
} else if stringStringMapCmd, ok := resp[1].(*rd.StringStringMapCmd); !ok {
|
||||
return nil, nil
|
||||
} else {
|
||||
@@ -55,9 +52,9 @@ func (v verificationHelper) appToken(token string) (map[string]string, error) {
|
||||
}
|
||||
|
||||
func (v *verificationHelper) keys(t map[string]string, uri string) (map[string]string, error) {
|
||||
secondDbKey := fmt.Sprintf("%s|%s", t[ApplicationIdentity], uri)
|
||||
thirdDbKey := fmt.Sprintf("%s|%s", t[UserToken], t[DomainIdentity])
|
||||
fifthDbKey := fmt.Sprintf("%s|%s", t[DeviceToken], t[DomainIdentity])
|
||||
secondDbKey := fmt.Sprintf("%s|%s", t[utils.ApplicationIdHeader], uri)
|
||||
thirdDbKey := fmt.Sprintf("%s|%s", t[utils.UserTokenHeader], t[utils.DomainIdHeader])
|
||||
fifthDbKey := fmt.Sprintf("%s|%s", t[utils.DeviceTokenHeader], t[utils.DomainIdHeader])
|
||||
|
||||
if resp, err := rdClient.Client.Get().Pipelined(func(p rd.Pipeliner) error {
|
||||
if cmd := p.Select(int(redis.ApplicationPermissionDb)); v.notEmptyError(cmd.Err()) {
|
||||
@@ -81,26 +78,26 @@ func (v *verificationHelper) keys(t map[string]string, uri string) (map[string]s
|
||||
return cmd.Err()
|
||||
}
|
||||
return nil
|
||||
}); err != nil && err != rd.Nil {
|
||||
return t, Error.Create(codes.Internal)
|
||||
}); v.notEmptyError(err) {
|
||||
return t, err
|
||||
} else {
|
||||
// It is not permitted to call this methodParts
|
||||
if v.fineCmd(resp, 1) {
|
||||
if intCmd, ok := resp[1].(*rd.IntCmd); ok && intCmd.Val() == 0 {
|
||||
return t, Error.Create(codes.PermissionDenied)
|
||||
}
|
||||
if msg, err := v.findStringCmd(resp, 1); err != nil {
|
||||
return t, err
|
||||
} else {
|
||||
t[permittedToCallInfo] = msg
|
||||
}
|
||||
// ===== CHECK USER TOKEN =====
|
||||
if v.fineCmd(resp, 3) {
|
||||
if stringCmd, ok := resp[3].(*rd.StringCmd); ok {
|
||||
t[UserIdentity] = stringCmd.Val()
|
||||
}
|
||||
if msg, err := v.findStringCmd(resp, 3); err != nil {
|
||||
return t, err
|
||||
} else {
|
||||
t[utils.UserIdHeader] = msg
|
||||
}
|
||||
// ===== CHECK DEVICE TOKEN =====
|
||||
if v.fineCmd(resp, 5) {
|
||||
if stringCmd, ok := resp[5].(*rd.StringCmd); ok {
|
||||
t[DeviceIdentity] = stringCmd.Val()
|
||||
}
|
||||
if msg, err := v.findStringCmd(resp, 5); err != nil {
|
||||
return t, err
|
||||
} else {
|
||||
t[utils.DeviceIdHeader] = msg
|
||||
}
|
||||
}
|
||||
return t, nil
|
||||
@@ -110,11 +107,25 @@ func (v *verificationHelper) notEmptyError(err error) bool {
|
||||
return err != nil && err != rd.Nil
|
||||
}
|
||||
|
||||
func (v *verificationHelper) fineCmd(cmders []rd.Cmder, arrayKey int) bool {
|
||||
if len(cmders) > arrayKey+1 {
|
||||
cmder := cmders[arrayKey]
|
||||
return cmder != nil && (cmder.Err() == nil || cmder.Err() == rd.Nil)
|
||||
func (v *verificationHelper) findStringCmd(cmders []rd.Cmder, arrayKey int) (string, error) {
|
||||
if len(cmders) > arrayKey {
|
||||
cmd := cmders[arrayKey]
|
||||
if cmd != nil {
|
||||
if cmd.Err() != nil {
|
||||
if cmd.Err() == rd.Nil {
|
||||
return "", nil
|
||||
}
|
||||
return "", cmd.Err()
|
||||
}
|
||||
if stringCmd, ok := cmd.(*rd.StringCmd); !ok {
|
||||
return "", errors.New("unexpected type")
|
||||
} else {
|
||||
return stringCmd.Val(), nil
|
||||
}
|
||||
} else {
|
||||
return "", errors.New("empty cmd")
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
return "", errors.New("")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user