ref
This commit is contained in:
1 parent
f3934e038e
commit
4fdf7c0ca2
19 files changed
+457
-379
No files matched your search
@@ -2,51 +2,56 @@ package authenticate
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/valyala/fasthttp"
|
||||
"isp-gate-service/redis"
|
||||
"google.golang.org/grpc/codes"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var redisKeyHeaderKeyMap = map[string]string{
|
||||
redis.SystemIdentity: utils.SystemIdHeader,
|
||||
redis.DomainIdentity: utils.DomainIdHeader,
|
||||
redis.ServiceIdentity: utils.ServiceIdHeader,
|
||||
redis.ApplicationIdentity: utils.ApplicationIdHeader,
|
||||
redis.DeviceIdentity: utils.DeviceIdHeader,
|
||||
redis.UserIdentity: utils.UserIdHeader,
|
||||
redis.DeviceToken: utils.DeviceTokenHeader,
|
||||
redis.UserToken: utils.UserTokenHeader,
|
||||
}
|
||||
|
||||
func Compete(ctx *fasthttp.RequestCtx) error {
|
||||
for _, notExpectedHeader := range []string{
|
||||
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,
|
||||
} {
|
||||
}
|
||||
)
|
||||
|
||||
func Do(ctx *fasthttp.RequestCtx) error {
|
||||
for _, notExpectedHeader := range notExpectedHeaders {
|
||||
ctx.Request.Header.Del(notExpectedHeader)
|
||||
}
|
||||
|
||||
appToken := ctx.Request.Header.Peek(utils.ApplicationTokenHeader)
|
||||
if len(appToken) == 0 {
|
||||
return errors.Errorf("%s not found", utils.ApplicationTokenHeader)
|
||||
return Error.Create(codes.Unauthenticated)
|
||||
}
|
||||
tokens, err := redis.Client.GetTokens(string(appToken))
|
||||
if err != nil || len(tokens) != 4 {
|
||||
return errors.Errorf("redis error %v", err)
|
||||
}
|
||||
|
||||
tokens[redis.Uri] = strings.Replace(strings.ToLower(string(ctx.Path())), "/", "", -1)
|
||||
tokens[redis.DeviceToken] = string(ctx.Request.Header.Peek(utils.DeviceTokenHeader))
|
||||
tokens[redis.UserToken] = string(ctx.Request.Header.Peek(utils.UserTokenHeader))
|
||||
|
||||
tokens, err = redis.Client.CheckTokens(tokens)
|
||||
verifiableKeys, err := verification.appToken(string(appToken))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
delete(tokens, redis.Uri)
|
||||
for key, value := range tokens {
|
||||
if len(verifiableKeys) != 4 {
|
||||
return Error.Create(codes.Unauthenticated)
|
||||
}
|
||||
|
||||
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, err = verification.keys(verifiableKeys, uri)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for key, value := range verifiableKeys {
|
||||
if value != "" {
|
||||
ctx.Request.Header.Set(redisKeyHeaderKeyMap[key], value)
|
||||
ctx.Request.Header.Set(verifiableKeyHeaderKeyMap[key], value)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package authenticate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
var Error errorHelper
|
||||
|
||||
func (errorHelper) Create(status codes.Code) ErrorDescription {
|
||||
return ErrorDescription{grpcStatus: status}
|
||||
}
|
||||
|
||||
type (
|
||||
errorHelper struct{}
|
||||
ErrorDescription struct {
|
||||
grpcStatus codes.Code
|
||||
}
|
||||
)
|
||||
|
||||
func (e ErrorDescription) Error() string {
|
||||
return fmt.Sprintf("%v", e)
|
||||
}
|
||||
|
||||
func (e ErrorDescription) ConvertToGrpcStatus() codes.Code {
|
||||
return e.grpcStatus
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package authenticate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
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"
|
||||
"isp-gate-service/conf"
|
||||
rdClient "isp-gate-service/redis"
|
||||
)
|
||||
|
||||
const (
|
||||
SystemIdentity = "1"
|
||||
DomainIdentity = "2"
|
||||
ServiceIdentity = "3"
|
||||
ApplicationIdentity = "4"
|
||||
|
||||
DeviceIdentity = "5"
|
||||
UserIdentity = "6"
|
||||
|
||||
DeviceToken = "7"
|
||||
UserToken = "8"
|
||||
)
|
||||
|
||||
var verification verificationHelper
|
||||
|
||||
type verificationHelper struct{}
|
||||
|
||||
func (v verificationHelper) appToken(token string) (map[string]string, error) {
|
||||
instanceUuid := config.Get().(*conf.Configuration).InstanceUuid
|
||||
key := fmt.Sprintf("%s|%s", token, instanceUuid)
|
||||
|
||||
if resp, err := rdClient.Client.Get().Pipelined(func(p rd.Pipeliner) error {
|
||||
if cmd := p.Select(int(redis.ApplicationTokenDb)); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
}
|
||||
if cmd := p.HGetAll(key); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, Error.Create(codes.Internal)
|
||||
} 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)
|
||||
} else if stringStringMapCmd, ok := resp[1].(*rd.StringStringMapCmd); !ok {
|
||||
return nil, nil
|
||||
} else {
|
||||
return stringStringMapCmd.Val(), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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])
|
||||
|
||||
if resp, err := rdClient.Client.Get().Pipelined(func(p rd.Pipeliner) error {
|
||||
if cmd := p.Select(int(redis.ApplicationPermissionDb)); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
}
|
||||
if cmd := p.Get(secondDbKey); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
}
|
||||
|
||||
if cmd := p.Select(int(redis.UserTokenDb)); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
}
|
||||
if cmd := p.Get(thirdDbKey); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
}
|
||||
|
||||
if cmd := p.Select(int(redis.DeviceTokenDb)); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
}
|
||||
if cmd := p.Get(fifthDbKey); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
}
|
||||
return nil
|
||||
}); err != nil && err != rd.Nil {
|
||||
return t, Error.Create(codes.Internal)
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
// ===== CHECK USER TOKEN =====
|
||||
if v.fineCmd(resp, 3) {
|
||||
if stringCmd, ok := resp[3].(*rd.StringCmd); ok {
|
||||
t[UserIdentity] = stringCmd.Val()
|
||||
}
|
||||
}
|
||||
// ===== CHECK DEVICE TOKEN =====
|
||||
if v.fineCmd(resp, 5) {
|
||||
if stringCmd, ok := resp[5].(*rd.StringCmd); ok {
|
||||
t[DeviceIdentity] = stringCmd.Val()
|
||||
}
|
||||
}
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
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)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user