github.com/infraboard/keyauth@v0.8.1/common/header/grpc.go (about) 1 package header 2 3 import ( 4 "context" 5 6 "google.golang.org/grpc/metadata" 7 ) 8 9 const ( 10 ClientHeaderKey = "client-id" 11 ClientSecretKey = "client-secret" 12 ) 13 14 func GetClientCredential(ctx context.Context) (clientId, clientSecret string) { 15 // 重上下文中获取认证信息 16 md, ok := metadata.FromIncomingContext(ctx) 17 if !ok { 18 return 19 } 20 21 cids := md.Get(ClientHeaderKey) 22 sids := md.Get(ClientSecretKey) 23 if len(cids) > 0 { 24 clientId = cids[0] 25 } 26 if len(sids) > 0 { 27 clientSecret = sids[0] 28 } 29 30 return 31 }