Merge branch 'access_checkout_by_user_id' into 'master'
add access checkout by user id See merge request integration-system/isp-gate-service!1
This commit is contained in:
commit
08c4fb9bee
8 files changed
+88
-32
No files matched your search
@@ -1,3 +1,5 @@
|
||||
## 2.4.0
|
||||
* add access checkout by user id
|
||||
## 2.3.0
|
||||
* add support for tokens in get params
|
||||
## 2.2.0
|
||||
|
||||
Generated
+2
-2
@@ -43,11 +43,11 @@
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:35e6ba92e1d4e89f4dc51b9868a5d35c3a03d9d2ffc956380502835cf0812a73"
|
||||
digest = "1:dfc03bd895603c3186f77cd6069f3953a1f14713a9c9901b52d837afd19205c0"
|
||||
name = "github.com/fasthttp/websocket"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "1e7306f3b587255aa9ddb1d5dd122750e00623cf"
|
||||
revision = "41f3051261751fe62d58a04b14bd522a96f0f7e9"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:abeb38ade3f32a92943e5be54f55ed6d6e3b6602761d74b4aab4c9dd45c18abd"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -65,7 +65,8 @@
|
||||
"tokensSetting": {
|
||||
"adminSecret": "secret",
|
||||
"applicationSecret": "secret",
|
||||
"applicationVerify": false
|
||||
"applicationVerify": false,
|
||||
"userSecret": "secret"
|
||||
},
|
||||
"authCacheSetting": {
|
||||
"enableCash": false,
|
||||
|
||||
@@ -45,6 +45,7 @@ type (
|
||||
AdminSecret string `schema:"Секрет для проверки токена администратора"`
|
||||
ApplicationSecret string `schema:"Секрет для проверки токена приложений"`
|
||||
ApplicationVerify bool `schema:"Проверка подписи токена приложений"`
|
||||
UserSecret string `schema:"Секрет для проверки токена пользователя"`
|
||||
}
|
||||
|
||||
Accounting struct {
|
||||
|
||||
@@ -56,48 +56,66 @@ 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
|
||||
|
||||
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 _, err := p.Select(int(redis.ApplicationPermissionDb)).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
if cmd := p.Get(secondDbKey); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
if _, err := p.Get(secondDbKey).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if cmd := p.Select(int(redis.UserTokenDb)); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
if _, err := p.Select(int(redis.UserTokenDb)).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
if cmd := p.Get(thirdDbKey); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
if _, err := p.Get(thirdDbKey).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if cmd := p.Select(int(redis.DeviceTokenDb)); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.Err()
|
||||
if _, err := p.Select(int(redis.UserPermissionDb)).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
if cmd := p.Get(fifthDbKey); v.notEmptyError(cmd.Err()) {
|
||||
return cmd.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
|
||||
}
|
||||
if _, err := p.Get(fifthDbKey).Result(); v.notEmptyError(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}); v.notEmptyError(err) {
|
||||
return t, false, err
|
||||
} else {
|
||||
// It is not permitted to call this methodParts
|
||||
// ===== NOT PERMITTED BY APPLICATION ID =====
|
||||
if msg, err := v.findStringCmd(resp, 1); err != nil {
|
||||
return t, false, err
|
||||
} else if msg == permittedToCallInfo {
|
||||
permittedToCall = true
|
||||
}
|
||||
// ===== CHECK USER TOKEN =====
|
||||
// ===== CHECK USER TOKEN =====
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user