verify_login_token_service.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package service
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. "XT_New/models"
  10. "XT_New/utils"
  11. "fmt"
  12. "github.com/astaxie/beego"
  13. )
  14. type AdminUserInfo struct {
  15. AdminUser *models.AdminUser `json:"user"`
  16. CurrentOrgId int64 `json:"current_org_id"`
  17. CurrentAppId int64 `json:"current_app_id"`
  18. OrgIds []int64 `json:"org_ids"`
  19. Orgs map[int64]*models.Org `json:"orgs"`
  20. OrgAppIds map[int64][]int64 `json:"org_app_ids"`
  21. OrgApps map[int64](map[int64]*models.OrgApp) `json:"org_apps"`
  22. App2OrgIds map[int64]int64 `json:"app_to_org_ids"`
  23. AppRoles map[int64]*models.App_Role `json:"app_roles"`
  24. AppPurviews map[int64][]*models.Purview `json:"app_purviews"`
  25. AppUrlfors map[int64][]string `json:"app_urlfors"`
  26. Subscibes map[int64]*models.ServeSubscibe `json:"org_subscibes"`
  27. }
  28. type verifyTokenError struct {
  29. Msg string
  30. }
  31. func (e *verifyTokenError) Error() string {
  32. return e.Msg
  33. }
  34. // 验证 token 成功后返回的管理员用户的所有信息,包括:基本用户信息,所属的所有机构,机构下的所有应用,应用的用户权限
  35. // map 的数据格式为
  36. /*
  37. "admin_user": { AdminUser's json },
  38. current_org_id: 1,
  39. current_app_id: 11,
  40. "org_ids": [1, 2, 3],
  41. "orgs": { (org_id: Org_Obj)
  42. 1: { Org's json },
  43. 2: { Org's json },
  44. },
  45. "org_app_ids": { (org_id: org_app_ids)
  46. 1: [11, 12, 13],
  47. 2: [21, 22, 23],
  48. },
  49. "org_apps": { (org_id: {app_id: OrgApp_Obj})
  50. 1: {
  51. 11: { OrgApp's json },
  52. 12: { OrgApp's json },
  53. },
  54. 2: {
  55. 21: { OrgApp's json },
  56. 22: { OrgApp's json },
  57. },
  58. },
  59. "app_to_org_ids": { (app_id: org_id)
  60. 11: 1,
  61. 12: 1,
  62. 21: 2,
  63. 22: 2,
  64. },
  65. "app_roles": { (app_id: App_Role Obj)
  66. 11: {App_Role's json},
  67. 12: {App_Role's json},
  68. 21: {App_Role's json},
  69. },
  70. "purviews": { (app_id: [processed Purviews' json])
  71. 11: [
  72. {Purview's json .childs[
  73. {Purview's json},
  74. {Purview's json},
  75. ]},
  76. {Purview's json},
  77. ],
  78. 12: [
  79. {Purview's json},
  80. {Purview's json},
  81. ],
  82. },
  83. "purview_urlfors": { (app_id: [url_for])
  84. 11: [
  85. "Controller1.Action1",
  86. "Controller1.Action2",
  87. "Controller2.Action1",
  88. "Controller2.Action2",
  89. ],
  90. }
  91. 应当注意的是,屈服于 Golang 令人恶心的类型机制,这里将所有数值型的 key 或 value 全部转成了 string
  92. */
  93. // 解析用户信息,并返回
  94. func VerifyToken(token string, ip string, sessionID string) (*AdminUserInfo, error, int) {
  95. // if len(sessionID) == 0 {
  96. // return nil, &verifyTokenError{"sessionID 为空"}
  97. // }
  98. ssoDomain := beego.AppConfig.String("sso_domain")
  99. api := ssoDomain + "/verifytoken"
  100. values := make(url.Values)
  101. values.Set("token", token)
  102. values.Set("app_type", "3")
  103. values.Set("ip", ip)
  104. values.Set("session_id", sessionID)
  105. resp, requestErr := http.PostForm(api, values)
  106. fmt.Println("resp", resp)
  107. if requestErr != nil {
  108. utils.ErrorLog("请求验证 sso token 接口失败: %v", requestErr)
  109. return nil, requestErr, 0
  110. }
  111. defer resp.Body.Close()
  112. body, ioErr := ioutil.ReadAll(resp.Body)
  113. if ioErr != nil {
  114. utils.ErrorLog("验证 sso token 接口返回数据读取失败: %v", ioErr)
  115. return nil, ioErr, 0
  116. }
  117. var respJSON map[string]interface{}
  118. utils.InfoLog(string(body))
  119. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  120. utils.ErrorLog("验证 sso token 接口返回数据解析JSON失败: %v", err)
  121. return nil, err, 0
  122. }
  123. fmt.Println(respJSON)
  124. if respJSON["state"].(float64) != 1 {
  125. msg := respJSON["msg"].(string)
  126. utils.ErrorLog("验证 sso token 接口请求失败: %v", msg)
  127. return nil, &verifyTokenError{"验证 sso token 接口请求失败"}, int(respJSON["code"].(float64))
  128. } else {
  129. utils.SuccessLog("验证 sso token 成功")
  130. return processAdminUserInfo(respJSON["data"].(map[string]interface{})), nil, 0
  131. }
  132. }
  133. func processAdminUserInfo(data map[string]interface{}) *AdminUserInfo {
  134. adminUser := processAdminUser(data)
  135. currentOrgId, currentAppId := processCurrentOrgIDAndAppID(data)
  136. orgIds := processOrgIds(data)
  137. orgs := processOrgs(data)
  138. orgAppIds := processOrgAppIds(data)
  139. orgApps := processOrgApps(data)
  140. app2OrgIds := processApp2OrgIds(data)
  141. appRoles := processAppRoles(data)
  142. appPurviews := processPurviews(data)
  143. appUrlfors := processPurviewUrlfors(data)
  144. orgSubscibes := processOrgSubscibes(data)
  145. sessionAdminUserInfo := &AdminUserInfo{
  146. AdminUser: adminUser,
  147. CurrentOrgId: currentOrgId,
  148. CurrentAppId: currentAppId,
  149. OrgIds: orgIds,
  150. Orgs: orgs,
  151. OrgAppIds: orgAppIds,
  152. OrgApps: orgApps,
  153. App2OrgIds: app2OrgIds,
  154. AppRoles: appRoles,
  155. AppPurviews: appPurviews,
  156. AppUrlfors: appUrlfors,
  157. Subscibes: orgSubscibes,
  158. }
  159. return sessionAdminUserInfo
  160. }
  161. // "admin_user": { AdminUser's json },
  162. func processAdminUser(data map[string]interface{}) *models.AdminUser {
  163. userJSONStr := data["admin_user"].(string)
  164. var adminUser models.AdminUser
  165. if err := json.Unmarshal([]byte(userJSONStr), &adminUser); err != nil {
  166. utils.ErrorLog("解析用户信息失败:%v", err)
  167. return nil
  168. } else {
  169. return &adminUser
  170. }
  171. }
  172. // current_org_id: 1,
  173. // current_app_id: 11,
  174. func processCurrentOrgIDAndAppID(data map[string]interface{}) (int64, int64) {
  175. orgIDStr := data["current_org_id"].(string)
  176. appIDStr := data["current_app_id"].(string)
  177. orgID, _ := strconv.Atoi(orgIDStr)
  178. appID, _ := strconv.Atoi(appIDStr)
  179. return int64(orgID), int64(appID)
  180. }
  181. // "org_ids": [1, 2, 3],
  182. func processOrgIds(data map[string]interface{}) []int64 {
  183. orgIdStrs := data["org_ids"].([]interface{})
  184. orgIds := make([]int64, 0, len(orgIdStrs))
  185. for _, idstr := range orgIdStrs {
  186. id, _ := strconv.Atoi(idstr.(string))
  187. orgIds = append(orgIds, int64(id))
  188. }
  189. return orgIds
  190. }
  191. // "orgs": { (org_id: Org_Obj)
  192. // 1: { Org's json },
  193. // 2: { Org's json },
  194. // },
  195. func processOrgs(data map[string]interface{}) map[int64]*models.Org {
  196. orgJSONs := data["orgs"].(map[string]interface{})
  197. orgs := make(map[int64]*models.Org)
  198. for orgIdStr, orgJSON := range orgJSONs {
  199. orgId, _ := strconv.Atoi(orgIdStr)
  200. var org models.Org
  201. json.Unmarshal([]byte(orgJSON.(string)), &org)
  202. orgs[int64(orgId)] = &org
  203. }
  204. return orgs
  205. }
  206. // "org_app_ids": { (org_id: org_app_ids)
  207. // 1: [11, 12, 13],
  208. // 2: [21, 22, 23],
  209. // },
  210. func processOrgAppIds(data map[string]interface{}) map[int64][]int64 {
  211. orgAppIdStrs := data["org_app_ids"].(map[string]interface{})
  212. orgAppIds := make(map[int64][]int64)
  213. for orgIdStr, appIdStrs := range orgAppIdStrs {
  214. orgId, _ := strconv.Atoi(orgIdStr)
  215. appIds := make([]int64, 0, len(appIdStrs.([]interface{})))
  216. for _, appIdStr := range appIdStrs.([]interface{}) {
  217. appId, _ := strconv.Atoi(appIdStr.(string))
  218. appIds = append(appIds, int64(appId))
  219. }
  220. orgAppIds[int64(orgId)] = appIds
  221. }
  222. return orgAppIds
  223. }
  224. // "org_apps": { (org_id: {app_id: OrgApp_Obj})
  225. // 1: {
  226. // 11: { OrgApp's json },
  227. // 12: { OrgApp's json },
  228. // },
  229. // 2: {
  230. // 21: { OrgApp's json },
  231. // 22: { OrgApp's json },
  232. // },
  233. // },
  234. func processOrgApps(data map[string]interface{}) map[int64]map[int64]*models.OrgApp {
  235. orgAppJSONs := data["org_apps"].(map[string]interface{})
  236. orgApps := make(map[int64]map[int64]*models.OrgApp)
  237. for orgIdStr, appJSONStrMap := range orgAppJSONs {
  238. orgId, _ := strconv.Atoi(orgIdStr)
  239. apps := make(map[int64]*models.OrgApp)
  240. for appIdStr, appJSONStr := range appJSONStrMap.(map[string]interface{}) {
  241. appId, _ := strconv.Atoi(appIdStr)
  242. var app models.OrgApp
  243. json.Unmarshal([]byte(appJSONStr.(string)), &app)
  244. apps[int64(appId)] = &app
  245. }
  246. orgApps[int64(orgId)] = apps
  247. }
  248. return orgApps
  249. }
  250. // "app_to_org_ids": { (app_id: org_id)
  251. // 11: 1,
  252. // 12: 1,
  253. // 21: 2,
  254. // 22: 2,
  255. // },
  256. func processApp2OrgIds(data map[string]interface{}) map[int64]int64 {
  257. app2OrgIdStrs := data["app_to_org_ids"].(map[string]interface{})
  258. app2OrgIds := make(map[int64]int64)
  259. for appIdStr, orgIdStr := range app2OrgIdStrs {
  260. orgId, _ := strconv.Atoi(orgIdStr.(string))
  261. appId, _ := strconv.Atoi(appIdStr)
  262. app2OrgIds[int64(appId)] = int64(orgId)
  263. }
  264. return app2OrgIds
  265. }
  266. // "app_roles": { (app_id: App_Role Obj)
  267. // 11: {App_Role's json},
  268. // 12: {App_Role's json},
  269. // 21: {App_Role's json},
  270. // },
  271. func processAppRoles(data map[string]interface{}) map[int64]*models.App_Role {
  272. appRoleJSONs := data["app_roles"].(map[string]interface{})
  273. appRoles := make(map[int64]*models.App_Role)
  274. for appIDStr, appRoleJSON := range appRoleJSONs {
  275. appID, _ := strconv.Atoi(appIDStr)
  276. var appRole models.App_Role
  277. json.Unmarshal([]byte(appRoleJSON.(string)), &appRole)
  278. appRoles[int64(appID)] = &appRole
  279. }
  280. return appRoles
  281. }
  282. // "purviews": { (app_id: [processed Purviews' json])
  283. // 11: [
  284. // {Purview's json .childs[
  285. // {Purview's json},
  286. // {Purview's json},
  287. // ]},
  288. // {Purview's json},
  289. // ],
  290. // 12: [
  291. // {Purview's json},
  292. // {Purview's json},
  293. // ],
  294. // },
  295. func processPurviews(data map[string]interface{}) map[int64][]*models.Purview {
  296. appPurviewJSONsStrs := data["purviews"].(map[string]interface{})
  297. appPurviews := make(map[int64][]*models.Purview)
  298. for appIdStr, purviewJSONsStr := range appPurviewJSONsStrs {
  299. appId, _ := strconv.Atoi(appIdStr)
  300. var purviews []*models.Purview
  301. json.Unmarshal([]byte(purviewJSONsStr.(string)), &purviews)
  302. // setLinkForPurviews(purviews)
  303. appPurviews[int64(appId)] = purviews
  304. }
  305. return appPurviews
  306. }
  307. // func setLinkForPurviews(purviews []*models.Purview) {
  308. // for _, purview := range purviews {
  309. // if len(purview.Urlfor) == 0 {
  310. // purview.Link = ""
  311. // } else {
  312. // purview.Link = beego.URLFor(purview.Urlfor)
  313. // }
  314. // if purview.Childs == nil {
  315. // purview.Childs = make([]*models.Purview, 0)
  316. // } else {
  317. // setLinkForPurviews(purview.Childs)
  318. // }
  319. // // utils.TraceLog("%+v", purview)
  320. // }
  321. // }
  322. // "purview_urlfors": { (app_id: [url_for])
  323. // 11: [
  324. // "Controller1.Action1",
  325. // "Controller1.Action2",
  326. // "Controller2.Action1",
  327. // "Controller2.Action2",
  328. // ],
  329. // }
  330. func processPurviewUrlfors(data map[string]interface{}) map[int64][]string {
  331. appUrlforsStrs := data["purview_urlfors"].(map[string]interface{})
  332. appUrlfors := make(map[int64][]string)
  333. for appIdStr, urlforsStr := range appUrlforsStrs {
  334. appId, _ := strconv.Atoi(appIdStr)
  335. var urlfors []string
  336. json.Unmarshal([]byte(urlforsStr.(string)), &urlfors)
  337. appUrlfors[int64(appId)] = urlfors
  338. }
  339. return appUrlfors
  340. }
  341. // "org_subscibes": { (org_id: ServeSubscibe)
  342. // 11: {ServeSubscibe's json}
  343. // },
  344. func processOrgSubscibes(data map[string]interface{}) map[int64]*models.ServeSubscibe {
  345. subscibeJSONs := data["org_subscibes"].(map[string]interface{})
  346. subscibes := make(map[int64]*models.ServeSubscibe)
  347. for orgIDStr, subscibeJSON := range subscibeJSONs {
  348. orgID, _ := strconv.Atoi(orgIDStr)
  349. var subscibe models.ServeSubscibe
  350. json.Unmarshal([]byte(subscibeJSON.(string)), &subscibe)
  351. subscibes[int64(orgID)] = &subscibe
  352. }
  353. return subscibes
  354. }
  355. func ModifyPassword(adminID int64, password string) error {
  356. err := writeUserDb.Model(&models.AdminUser{}).Where("id = ? AND status = 1", adminID).Updates(map[string]interface{}{"password": password, "mtime": time.Now().Unix()}).Error
  357. return err
  358. }