-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
userinfo.go
216 lines (191 loc) · 6.12 KB
/
userinfo.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package self
import (
"encoding/json"
"fmt"
"time"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/http/httpclient"
"configcenter/src/common/metadata"
"configcenter/src/web_server/application/middleware/user/plugins/manager"
webCommon "configcenter/src/web_server/common"
)
func init() {
plugin := &metadata.LoginPluginInfo{
Name: "blueking login system",
Version: common.BKDefaultLoginUserPluginVersion,
HandleFunc: &user{},
}
manager.RegisterPlugin(plugin) //("blueking login system", "self", "")
}
type loginResultData struct {
UserName string `json:"username"`
ChName string `json:"chname"`
Phone string `json:"Phone"`
Email string `json:"email"`
Role string `json:"role"`
Language string `json:"language"`
OwnerUin string `json:"owner_uin"`
}
type loginResult struct {
Message string
Code string
Result bool
Data *loginResultData
}
type userListResult struct {
Message string `json:"message"`
Data []userInfo `json:"data"`
Code string `json:"code"`
Result bool `json:"result"`
}
type userInfo struct {
UserName string `json:"username"`
QQ string `json:"qq"`
Role string `json:"role"`
Language string `json:"language"`
Phone string `json:"phone"`
WxUserid string `json:"wx_userid"`
Email string `json:"email"`
Chname string `json:"chname"`
TimeZone string `json:"time_zone"`
}
type user struct {
}
// LoginUser user login
func (m *user) LoginUser(c *gin.Context, config map[string]string, isMultiOwner bool) (user *metadata.LoginUserInfo, loginSucc bool) {
bk_token, err := c.Cookie(common.HTTPCookieBKToken)
if nil != err {
return nil, false
}
if nil != err || 0 == len(bk_token) {
return nil, false
}
checkUrl, ok := config["site.check_url"]
if !ok {
blog.Errorf("get login url config item not found")
return nil, false
}
loginURL := checkUrl + bk_token
httpCli := httpclient.NewHttpClient()
httpCli.SetTimeOut(30 * time.Second)
httpCli.SetTlsNoVerity()
loginResultByteArr, err := httpCli.GET(loginURL, nil, nil)
if nil != err {
blog.Errorf("get user info return error: %v", err)
return nil, false
}
blog.V(3).Infof("get user info cond %v, return: %s ", string(loginURL), string(loginResultByteArr))
var resultData loginResult
err = json.Unmarshal(loginResultByteArr, &resultData)
if nil != err {
blog.Errorf("get user info json error: %v, rawData:%s", err, string(loginResultByteArr))
return nil, false
}
if !resultData.Result {
blog.Errorf("get user info return error , error code: %s, error message: %s", resultData.Code, resultData.Message)
return nil, false
}
userDetail := resultData.Data
if "" == userDetail.OwnerUin {
userDetail.OwnerUin = common.BKDefaultOwnerID
}
user = &metadata.LoginUserInfo{
UserName: userDetail.UserName,
ChName: userDetail.ChName,
Phone: userDetail.Phone,
Email: userDetail.Email,
Role: userDetail.Role,
BkToken: bk_token,
OnwerUin: userDetail.OwnerUin,
IsOwner: false,
Language: userDetail.Language,
}
return user, true
}
// GetUserList get user list from paas
func (m *user) GetUserList(c *gin.Context, config map[string]string) ([]*metadata.LoginSystemUserInfo, error) {
accountURL, ok := config["site.bk_account_url"]
if !ok {
return nil, fmt.Errorf("config site.bk_account_url not found")
}
session := sessions.Default(c)
skiplogin := session.Get(webCommon.IsSkipLogin)
skiplogins, ok := skiplogin.(string)
if ok && "1" == skiplogins {
blog.Info("use skip login flag: %v", skiplogin)
adminData := []*metadata.LoginSystemUserInfo{
&metadata.LoginSystemUserInfo{
CnName: "admin",
EnName: "admin",
},
}
return adminData, nil
}
token := session.Get(common.HTTPCookieBKToken)
getURL := fmt.Sprintf(accountURL, token)
httpClient := httpclient.NewHttpClient()
reply, err := httpClient.GET(getURL, nil, nil)
if nil != err {
blog.Errorf("get user list error:%v", err)
return nil, fmt.Errorf("http do error:%s", err.Error())
}
blog.Info("get user list url: %s, return:%s", getURL, reply)
var result userListResult
err = json.Unmarshal([]byte(reply), &result)
if nil != err || false == result.Result {
blog.Errorf("get user list error:%v, error code:%d, error messsage:", err, result.Code, result.Message)
return nil, fmt.Errorf("get user list reply error")
}
userListArr := make([]*metadata.LoginSystemUserInfo, 0)
for _, user := range result.Data {
cellData := make(map[string]interface{})
cellData["chinese_name"] = user.Chname
cellData["english_name"] = user.UserName
userListArr = append(userListArr, &metadata.LoginSystemUserInfo{
CnName: user.Chname,
EnName: user.UserName,
})
}
return userListArr, nil
}
func (m *user) GetLoginUrl(c *gin.Context, config map[string]string, input *metadata.LogoutRequestParams) string {
var ok bool
var loginURL string
var siteURL string
if common.LogoutHTTPSchemeHTTPS == input.HTTPScheme {
loginURL, ok = config["site.bk_https_login_url"]
} else {
loginURL, ok = config["site.bk_login_url"]
}
if !ok {
loginURL = ""
}
if common.LogoutHTTPSchemeHTTPS == input.HTTPScheme {
siteURL, ok = config["site.https_domain_url"]
} else {
siteURL, ok = config["site.domain_url"]
}
if !ok {
siteURL = ""
}
appCode, ok := config["site.app_code"]
if !ok {
appCode = ""
}
loginURL = fmt.Sprintf(loginURL, appCode, fmt.Sprintf("%s%s", siteURL, c.Request.URL.String()))
return loginURL
}