/
header.go
110 lines (95 loc) · 2.67 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package apis
import (
"context"
"net/http"
"strconv"
"github.com/erda-project/erda-infra/pkg/transport"
"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda-proto-go/common/pb"
)
const (
headerInternalClient = "internal-client"
)
var langKeys = []string{"lang", "accept-language"}
// Language .
func Language(ctx context.Context) i18n.LanguageCodes {
header := transport.ContextHeader(ctx)
if header != nil {
for _, key := range langKeys {
vals := header.Get(key)
for _, v := range vals {
if len(v) > 0 {
langs, _ := i18n.ParseLanguageCode(v)
return langs
}
}
}
}
return nil
}
// HTTPLanguage .
func HTTPLanguage(r *http.Request) i18n.LanguageCodes {
lang := r.Header.Get("Lang")
if len(lang) <= 0 {
lang = r.Header.Get("Accept-Language")
}
langs, _ := i18n.ParseLanguageCode(lang)
return langs
}
// GetOrgID .
func GetOrgID(ctx context.Context) string {
return GetHeader(ctx, "org-id")
}
// GetIntOrgID .
func GetIntOrgID(ctx context.Context) (int64, error) {
return strconv.ParseInt(GetOrgID(ctx), 10, 64)
}
// GetUserID .
func GetUserID(ctx context.Context) string {
return GetHeader(ctx, "user-id")
}
// GetIntUserID .
func GetIntUserID(ctx context.Context) (int64, error) {
return strconv.ParseInt(GetUserID(ctx), 10, 64)
}
// GetHeader
func GetHeader(ctx context.Context, key string) string {
header := transport.ContextHeader(ctx)
if header != nil {
for _, v := range header.Get(key) {
if len(v) > 0 {
return v
}
}
}
return ""
}
func GetInternalClient(ctx context.Context) string {
return GetHeader(ctx, headerInternalClient)
}
func IsInternalClient(ctx context.Context) bool {
return GetInternalClient(ctx) != ""
}
// GetIdentityInfo get User-ID and Internal-Client from header.
// return nil if no identity info found.
func GetIdentityInfo(ctx context.Context) *pb.IdentityInfo {
// try to get User-ID
userID := GetUserID(ctx)
internalClient := GetInternalClient(ctx)
if userID == "" && internalClient == "" {
return nil
}
return &pb.IdentityInfo{UserID: userID, InternalClient: internalClient}
}