github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/utils/cputil/header.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cputil 16 17 import ( 18 "context" 19 20 "google.golang.org/grpc/metadata" 21 22 "github.com/erda-project/erda-infra/pkg/transport" 23 "github.com/erda-project/erda-infra/providers/component-protocol/protobuf/proto-go/cp/pb" 24 ) 25 26 // GetOrgID . 27 func GetOrgID(ctx context.Context) string { 28 return GetHeader(ctx, "org-id") 29 } 30 31 // GetUserID . 32 func GetUserID(ctx context.Context) string { 33 return GetHeader(ctx, "user-id") 34 } 35 36 // GetHeader . 37 func GetHeader(ctx context.Context, key string) string { 38 header := transport.ContextHeader(ctx) 39 if header != nil { 40 for _, v := range header.Get(key) { 41 if len(v) > 0 { 42 return v 43 } 44 } 45 } 46 return "" 47 } 48 49 // GetAllHeaders get all headers from ctx. 50 func GetAllHeaders(ctx context.Context) metadata.MD { 51 return transport.ContextHeader(ctx) 52 } 53 54 // GetIdentity get identity from ctx. 55 func GetIdentity(ctx context.Context) *pb.IdentityInfo { 56 return &pb.IdentityInfo{ 57 UserID: GetUserID(ctx), 58 OrgID: GetOrgID(ctx), 59 } 60 }