add admin authenticate
This commit is contained in:
1 parent
5b115440f1
commit
0edf80ffed
10 files changed
+128
-38
No files matched your search
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/valyala/fasthttp"
|
||||
"google.golang.org/grpc/codes"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/routing"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -25,16 +26,16 @@ func Do(ctx *fasthttp.RequestCtx) error {
|
||||
|
||||
appToken := ctx.Request.Header.Peek(utils.ApplicationTokenHeader)
|
||||
if len(appToken) == 0 {
|
||||
return Error.create(codes.Unauthenticated)
|
||||
return createError(codes.Unauthenticated)
|
||||
}
|
||||
|
||||
keys, err := verification.appToken(string(appToken))
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return Error.create(codes.Internal)
|
||||
return createError(codes.Internal)
|
||||
}
|
||||
if len(keys) != 4 {
|
||||
return Error.create(codes.Unauthenticated)
|
||||
return createError(codes.Unauthenticated)
|
||||
}
|
||||
|
||||
verifiableKeys := make(map[string]string)
|
||||
@@ -42,17 +43,18 @@ func Do(ctx *fasthttp.RequestCtx) error {
|
||||
verifiableKeys[verifiableKeyHeaderKeyMap[i]] = value
|
||||
}
|
||||
|
||||
uri := strings.Replace(strings.ToLower(string(ctx.Path())), "/", "", -1)
|
||||
path := ctx.Path()
|
||||
uri := strings.Replace(strings.ToLower(string(path)), "/", "", -1)
|
||||
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 {
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return Error.create(codes.Internal)
|
||||
return createError(codes.Internal)
|
||||
}
|
||||
if verifiableKeys[permittedToCallInfo] == "0" {
|
||||
return Error.create(codes.PermissionDenied)
|
||||
return createError(codes.PermissionDenied)
|
||||
} else {
|
||||
delete(verifiableKeys, permittedToCallInfo)
|
||||
}
|
||||
@@ -62,5 +64,27 @@ func Do(ctx *fasthttp.RequestCtx) error {
|
||||
ctx.Request.Header.Set(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
path = getPathWithoutPrefix(path)
|
||||
if _, ok := routing.InnerAddressMap[string(path)]; ok {
|
||||
adminToken := ctx.Request.Header.Peek("x-auth-admin") //todo const key
|
||||
if token.Check(string(adminToken)) != nil {
|
||||
return createError(codes.PermissionDenied)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getPathWithoutPrefix(path []byte) []byte {
|
||||
firstFound := false
|
||||
for key, value := range path {
|
||||
if value == '/' {
|
||||
if firstFound {
|
||||
return path[key+1:]
|
||||
} else {
|
||||
firstFound = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return []byte{}
|
||||
}
|
||||
@@ -5,11 +5,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
var Error errorHelper
|
||||
|
||||
type errorHelper struct{}
|
||||
|
||||
func (errorHelper) create(status codes.Code) ErrorDescription {
|
||||
func createError(status codes.Code) ErrorDescription {
|
||||
return ErrorDescription{grpcStatus: status}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package authenticate
|
||||
|
||||
import (
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/conf"
|
||||
)
|
||||
|
||||
var token tokenHelper
|
||||
|
||||
type tokenHelper struct{}
|
||||
|
||||
func (tokenHelper) Check(token string) error {
|
||||
secret := config.GetRemote().(*conf.RemoteConfig).SecretKey
|
||||
|
||||
if parsed, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, errors.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(secret), nil
|
||||
}); err != nil {
|
||||
return err
|
||||
} else if !parsed.Valid {
|
||||
return errors.New("token is invalid")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -126,6 +126,6 @@ func (v *verificationHelper) findStringCmd(cmders []rd.Cmder, arrayKey int) (str
|
||||
return "", errors.New("empty cmd")
|
||||
}
|
||||
} else {
|
||||
return "", errors.New("")
|
||||
return "", errors.New("not found cmd")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user