fix parse meta ctx
This commit is contained in:
		
							parent
							
								
									6ec06c2813
								
							
						
					
					
						commit
						fc42bc92ff
					
				|  | @ -2,9 +2,8 @@ package service | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"encoding/json" |  | ||||||
| 	"strconv" |  | ||||||
| 
 | 
 | ||||||
|  | 	"git.apinb.com/bsm-sdk/core/crypto/encipher" | ||||||
| 	"git.apinb.com/bsm-sdk/core/errcode" | 	"git.apinb.com/bsm-sdk/core/errcode" | ||||||
| 	"git.apinb.com/bsm-sdk/core/utils" | 	"git.apinb.com/bsm-sdk/core/utils" | ||||||
| 	"google.golang.org/grpc/metadata" | 	"google.golang.org/grpc/metadata" | ||||||
|  | @ -15,6 +14,7 @@ type Meta struct { | ||||||
| 	IDENTITY string            `json:"identity"` | 	IDENTITY string            `json:"identity"` | ||||||
| 	EXTEND   map[string]string `json:"extend"` | 	EXTEND   map[string]string `json:"extend"` | ||||||
| 	CLIENT   string            `json:"client"` | 	CLIENT   string            `json:"client"` | ||||||
|  | 	ROLE     string            `json:"role"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // 解析Context中MetaData的数据
 | // 解析Context中MetaData的数据
 | ||||||
|  | @ -30,31 +30,28 @@ func ParseMetaCtx(ctx context.Context, opts *ParseOptions) (*Meta, error) { | ||||||
| 		return nil, errcode.ErrJWTAuthNotFound | 		return nil, errcode.ErrJWTAuthNotFound | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// 安全获取 metadata 中的值
 | 	var Authorizations []string = md.Get("authorization") | ||||||
| 	identityValues := md.Get("authorization_identity") | 	if len(Authorizations) == 0 || Authorizations[0] == "" { | ||||||
| 	clientValues := md.Get("client") |  | ||||||
| 
 |  | ||||||
| 	if len(identityValues) == 0 { |  | ||||||
| 		return nil, errcode.ErrJWTAuthNotFound | 		return nil, errcode.ErrJWTAuthNotFound | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	claims, err := encipher.ParseTokenAes(Authorizations[0]) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	clientValues := md.Get("client") | ||||||
|  | 
 | ||||||
| 	if len(clientValues) == 0 { | 	if len(clientValues) == 0 { | ||||||
| 		return nil, errcode.ErrJWTAuthNotFound | 		return nil, errcode.ErrJWTAuthNotFound | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	meta := &Meta{ | 	meta := &Meta{ | ||||||
| 		IDENTITY: md["authorization_identity"][0], | 		ID:       claims.ID, | ||||||
|  | 		IDENTITY: claims.Identity, | ||||||
| 		CLIENT:   md["client"][0], | 		CLIENT:   md["client"][0], | ||||||
| 	} | 		EXTEND:   claims.Extend, | ||||||
| 
 | 		ROLE:     claims.Role, | ||||||
| 	if id, err := strconv.Atoi(md["authorization_id"][0]); err != nil { |  | ||||||
| 		return nil, errcode.ErrJWTAuthKeyId |  | ||||||
| 	} else { |  | ||||||
| 		meta.ID = uint(id) |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	data := make(map[string]string) |  | ||||||
| 	if err := json.Unmarshal([]byte(md["authorization_extend"][0]), &data); err == nil { |  | ||||||
| 		meta.EXTEND = data |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if opts != nil { | 	if opts != nil { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue