update access checkout by user id
This commit is contained in:
1 parent
ab95347767
commit
78f1fe43b9
4 files changed
+66
-34
No files matched your search
@@ -84,6 +84,15 @@ func Do(ctx *fasthttp.RequestCtx, path string) (int32, error) {
|
||||
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])
|
||||
if err != nil {
|
||||
return 0, createError("forbidden", codes.PermissionDenied)
|
||||
} else {
|
||||
verifiableKeys[utils.UserIdHeader] = userId
|
||||
}
|
||||
}
|
||||
|
||||
permittedToCall := false
|
||||
verifiableKeys, permittedToCall, err = auth.verify.Identity(verifiableKeys, path)
|
||||
if err != nil {
|
||||
|
||||
+37
-12
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cast"
|
||||
"isp-gate-service/conf"
|
||||
)
|
||||
|
||||
@@ -14,6 +15,11 @@ type appClaims struct {
|
||||
AppId int32
|
||||
}
|
||||
|
||||
type userClaims struct {
|
||||
*jwt.StandardClaims
|
||||
UserId int64
|
||||
}
|
||||
|
||||
type token struct{}
|
||||
|
||||
func (t token) Admin(token string) error {
|
||||
@@ -25,26 +31,45 @@ func (t token) Admin(token string) error {
|
||||
func (t token) Application(token string) (int32, error) {
|
||||
secret := config.GetRemote().(*conf.RemoteConfig).TokensSetting.ApplicationSecret
|
||||
|
||||
if parsed, err := t.parse(token, secret, &appClaims{StandardClaims: &jwt.StandardClaims{}}); err != nil {
|
||||
parsed, err := t.parse(token, secret, &appClaims{StandardClaims: &jwt.StandardClaims{}})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
} else if claims, ok := parsed.Claims.(*appClaims); !ok {
|
||||
return 0, errors.New("token is invalid")
|
||||
} else {
|
||||
return claims.AppId, nil
|
||||
}
|
||||
claims, ok := parsed.Claims.(*appClaims)
|
||||
if !ok {
|
||||
return 0, errors.New("token is invalid")
|
||||
}
|
||||
return claims.AppId, nil
|
||||
}
|
||||
|
||||
func (t token) User(token string) (string, error) {
|
||||
secret := config.GetRemote().(*conf.RemoteConfig).TokensSetting.UserSecret
|
||||
|
||||
parsed, err := t.parse(token, secret, &userClaims{StandardClaims: &jwt.StandardClaims{}})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
claims, ok := parsed.Claims.(*userClaims)
|
||||
if !ok {
|
||||
return "", errors.New("token is invalid")
|
||||
}
|
||||
|
||||
return cast.ToString(claims.UserId), nil
|
||||
}
|
||||
|
||||
func (token) parse(token, secret string, claims jwt.Claims) (*jwt.Token, error) {
|
||||
if parsed, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
parsed, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
|
||||
_, ok := token.Method.(*jwt.SigningMethodHMAC)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(secret), nil
|
||||
}); err != nil {
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !parsed.Valid {
|
||||
return nil, errors.New("token is invalid")
|
||||
} else {
|
||||
return parsed, nil
|
||||
}
|
||||
if !parsed.Valid {
|
||||
return nil, errors.New("token is invalid")
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
@@ -45,6 +45,7 @@ type (
|
||||
AdminSecret string `schema:"Секрет для проверки токена администратора"`
|
||||
ApplicationSecret string `schema:"Секрет для проверки токена приложений"`
|
||||
ApplicationVerify bool `schema:"Проверка подписи токена приложений"`
|
||||
UserSecret string `schema:"Секрет для проверки токена пользователя"`
|
||||
}
|
||||
|
||||
Accounting struct {
|
||||
|
||||
@@ -56,6 +56,7 @@ func (v *runtimeVerify) ApplicationToken(token string) (map[string]string, error
|
||||
func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]string, bool, 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
|
||||
|
||||
@@ -74,6 +75,13 @@ func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]st
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := p.Select(int(redis.UserPermissionDb)).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
if _, err := p.Get(fourthDbKey).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := p.Select(int(redis.DeviceTokenDb)).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
@@ -95,34 +103,23 @@ func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]st
|
||||
if msg, err := v.findStringCmd(resp, 3); err != nil {
|
||||
return t, false, err
|
||||
} else {
|
||||
t[utils.UserIdHeader] = msg
|
||||
|
||||
if msg != t[utils.UserIdHeader] {
|
||||
return t, false, errors.Errorf("doesn't match user id")
|
||||
}
|
||||
}
|
||||
// ===== NOT PERMITTED BY USER ID =====
|
||||
if msg, err := v.findStringCmd(resp, 5); err != nil {
|
||||
return t, false, err
|
||||
} else if msg == permittedToCallInfo {
|
||||
permittedToCall = true
|
||||
}
|
||||
// ===== CHECK DEVICE TOKEN =====
|
||||
if msg, err := v.findStringCmd(resp, 5); err != nil {
|
||||
if msg, err := v.findStringCmd(resp, 7); err != nil {
|
||||
return t, false, err
|
||||
} else {
|
||||
t[utils.DeviceIdHeader] = msg
|
||||
}
|
||||
|
||||
// ===== NOT PERMITTED BY USER ID =====
|
||||
fourthDbKey := fmt.Sprintf("%s|%s", t[utils.UserIdHeader], uri)
|
||||
if resp, err := rdClient.Client.Get().Pipelined(func(p rd.Pipeliner) error {
|
||||
if _, err := p.Select(int(redis.UserPermissionDb)).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
if _, err := p.Get(fourthDbKey).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); v.notEmptyError(err) {
|
||||
return t, false, err
|
||||
} else {
|
||||
if msg, err := v.findStringCmd(resp, 1); err != nil {
|
||||
return t, false, err
|
||||
} else if msg == permittedToCallInfo {
|
||||
permittedToCall = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return t, permittedToCall, nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user