auth.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package handler
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/common"
  5. "cold-delivery/common/global"
  6. "errors"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "github.com/go-redis/redis/v7"
  10. "github.com/go-sql-driver/mysql"
  11. "github.com/mssola/user_agent"
  12. "gogs.baozhida.cn/zoie/OAuth-core/api"
  13. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  14. jwt "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth"
  15. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  16. "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  17. "gogs.baozhida.cn/zoie/OAuth-core/sdk"
  18. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  19. "gorm.io/gorm"
  20. "net/http"
  21. "strings"
  22. )
  23. func PayloadFunc(data interface{}) jwt.MapClaims {
  24. if v, ok := data.(map[string]interface{}); ok {
  25. u, _ := v["user"].(SysUser)
  26. r, _ := v["role"].(SysRole)
  27. d, _ := v["dept"].(SysDept)
  28. single, _ := v["single"].(bool)
  29. return jwt.MapClaims{
  30. jwt.UUIDKey: u.Uuid,
  31. jwt.IdentityKey: u.Id,
  32. jwt.RoleIdKey: r.Id,
  33. jwt.RoleKey: r.RoleKey,
  34. jwt.UserNameKey: u.Username,
  35. jwt.DataScopeKey: r.DataScope,
  36. jwt.RoleNameKey: r.Name,
  37. jwt.SingleKey: single,
  38. jwt.DeptIdKey: u.DeptId,
  39. jwt.DeptNameKey: d.DeptName,
  40. }
  41. }
  42. return jwt.MapClaims{}
  43. }
  44. func IdentityHandler(c *gin.Context) interface{} {
  45. claims := jwt.ExtractClaims(c)
  46. return map[string]interface{}{
  47. "UUIDKey": claims["uuid"],
  48. "IdentityKey": claims["identity"],
  49. "UserName": claims["username"],
  50. "RoleName": claims["roleName"],
  51. "RoleKey": claims["roleKey"],
  52. "Id": claims["identity"],
  53. "RoleId": claims["roleId"],
  54. "DataScope": claims["dataScope"],
  55. "single": claims["single"],
  56. "DeptId": claims["deptId"],
  57. "DeptName": claims["deptName"],
  58. }
  59. }
  60. // Authenticator 登录认证
  61. // Update 登录认证
  62. // @Summary 登录认证
  63. // @Description 登录认证
  64. // @Tags 登录
  65. // @Accept application/json
  66. // @Product application/json
  67. // @Param data body Login true "body"
  68. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  69. // @Router /api/login [post]
  70. func Authenticator(c *gin.Context) (interface{}, error) {
  71. log := api.GetRequestLogger(c)
  72. ormDB, err := pkg.GetOrm(c)
  73. if err != nil {
  74. log.Errorf("get db error, %s", err.Error())
  75. response.Error(c, 500, err, "数据库连接获取失败")
  76. return nil, jwt.ErrFailedAuthentication
  77. }
  78. var loginVals Login
  79. var status = "2"
  80. var msg = "登录成功"
  81. var username = ""
  82. defer func() {
  83. LoginLogToDB(c, status, msg, username)
  84. }()
  85. if err = c.ShouldBind(&loginVals); err != nil {
  86. username = loginVals.Username
  87. msg = "数据解析失败"
  88. status = "1"
  89. return nil, jwt.ErrFailedAuthentication
  90. }
  91. //if config.ApplicationConfig.Mode != "dev" {
  92. // if !captcha.Verify(loginVals.UUID, loginVals.Code, true) {
  93. // username = loginVals.Username
  94. // msg = "验证码错误"
  95. // status = "1"
  96. //
  97. // return nil, jwt.ErrInvalidVerificationCode
  98. // }
  99. //}
  100. var u SysUser
  101. var role SysRole
  102. var dept SysDept
  103. var e error
  104. if loginVals.Type == 1 {
  105. u, role, dept, e = loginVals.GetUser(ormDB)
  106. if e != nil {
  107. msg = e.Error()
  108. status = "1"
  109. log.Warnf("%s login failed!", username)
  110. return nil, jwt.ErrFailedAuthentication
  111. }
  112. }
  113. if loginVals.Type == 2 {
  114. u, role, dept, e = loginVals.GetUserByCode(ormDB)
  115. if e != nil {
  116. msg = e.Error()
  117. status = "1"
  118. log.Warnf("%s login failed!", username)
  119. return nil, jwt.ErrFailedSmsVerifyCode
  120. }
  121. }
  122. if loginVals.Type == 3 {
  123. u, role, dept, e = loginVals.GetUserByWX(ormDB)
  124. if e != nil {
  125. msg = e.Error()
  126. status = "1"
  127. log.Warnf("%s login failed!", username)
  128. return nil, jwt.ErrFailedSmsVerifyCode
  129. }
  130. }
  131. if loginVals.Type == 0 {
  132. return nil, jwt.ErrFailedAuthentication
  133. }
  134. username = loginVals.Username
  135. var single bool
  136. //single, err = GetSingleLogin(c)
  137. //if err != nil {
  138. // return nil, err
  139. //}
  140. return map[string]interface{}{"user": u, "role": role, "dept": dept, "single": single, "mobile": loginVals.Mobile}, nil
  141. }
  142. // LoginLogToDB Write log to database
  143. func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
  144. if !config.LoggerConfig.EnabledDB {
  145. return
  146. }
  147. log := api.GetRequestLogger(c)
  148. l := make(map[string]interface{})
  149. ua := user_agent.New(c.Request.UserAgent())
  150. l["ipaddr"] = common.GetClientIP(c)
  151. l["loginTime"] = pkg.GetCurrentTime()
  152. l["status"] = status
  153. l["remark"] = c.Request.UserAgent()
  154. browserName, browserVersion := ua.Browser()
  155. l["browser"] = browserName + " " + browserVersion
  156. l["os"] = ua.OS()
  157. l["platform"] = ua.Platform()
  158. l["username"] = username
  159. l["msg"] = msg
  160. q := sdk.Runtime.GetMemoryQueue(c.Request.Host)
  161. message, err := sdk.Runtime.GetStreamMessage("", global.LoginLog, l)
  162. if err != nil {
  163. log.Errorf("GetStreamMessage error, %s", err.Error())
  164. //日志报错错误,不中断请求
  165. } else {
  166. err = q.Append(message)
  167. if err != nil {
  168. log.Errorf("Append message error, %s", err.Error())
  169. }
  170. }
  171. }
  172. // LogOut 退出登录
  173. // @Summary 退出登录
  174. // @Description 退出登录
  175. // @Description LoginHandler can be used by clients to get a jwt token.
  176. // @Description Reply will be of the form {"token": "TOKEN"}.
  177. // @Tags 登录
  178. // @Accept application/json
  179. // @Product application/json
  180. // @Success 200 {string} string "{"code": 200, "msg": "成功退出系统"}"
  181. // @Router /logout [post]
  182. // @Security Bearer
  183. func LogOut(c *gin.Context) {
  184. LoginLogToDB(c, "2", "退出成功", user.GetUserName(c))
  185. c.JSON(http.StatusOK, gin.H{
  186. "code": 200,
  187. "msg": "退出成功",
  188. })
  189. }
  190. func Authorizator(data interface{}, c *gin.Context) bool {
  191. if v, ok := data.(map[string]interface{}); ok {
  192. u, _ := v["user"].(model.SysUser)
  193. r, _ := v["role"].(model.SysRole)
  194. d, _ := v["dept"].(model.SysDept)
  195. single, _ := v["single"].(bool)
  196. c.Set("uuid", u.Uuid)
  197. c.Set("identity", u.Id)
  198. c.Set("userName", u.Username)
  199. c.Set("roleName", r.Name)
  200. c.Set("roleKey", r.RoleKey)
  201. c.Set("userId", u.Id)
  202. c.Set("roleId", r.Id)
  203. c.Set("single", single)
  204. c.Set("dataScope", r.DataScope)
  205. c.Set("deptId", u.DeptId)
  206. c.Set("deptName", d.Name)
  207. return true
  208. }
  209. return false
  210. }
  211. func Unauthorized(c *gin.Context, code int, message string) {
  212. c.JSON(http.StatusOK, gin.H{
  213. "code": code,
  214. "msg": message,
  215. })
  216. }
  217. // 保存token到redis
  218. func SaveNewestToken(c *gin.Context, userId int64, token string, expire int64) error {
  219. key := fmt.Sprintf("%s:%d", "bzd.oauth.token", userId)
  220. return sdk.Runtime.GetCacheAdapter().Set(key, token, int(expire))
  221. }
  222. // redis从redis获取token
  223. func GetNewestToken(c *gin.Context, userId int64) (string, error) {
  224. key := fmt.Sprintf("%s:%d", "bzd.oauth.token", userId)
  225. return sdk.Runtime.GetCacheAdapter().Get(key)
  226. }
  227. func GetSingleLogin(c *gin.Context) (bool, error) {
  228. log := api.GetRequestLogger(c)
  229. ormDB, err := pkg.GetOrm(c)
  230. if err != nil {
  231. log.Errorf("get db error, %s", err.Error())
  232. response.Error(c, 500, err, "数据库连接获取失败")
  233. return false, err
  234. }
  235. //result := map[string]interface{}{}
  236. var result string
  237. err = ormDB.Table("sys_config").Select("config_value").Where("config_key = ? ", "sys_single_login").Scan(&result).Error
  238. if err != nil {
  239. if errors.Is(err, gorm.ErrRecordNotFound) || err.(*mysql.MySQLError).Number == 1146 {
  240. // 默认为非单一登录
  241. return false, nil
  242. }
  243. log.Errorf("get sys_config error, %s", err.Error())
  244. return false, err
  245. }
  246. if result == "是" {
  247. return true, nil
  248. }
  249. return false, nil
  250. }
  251. func SetEnterDeptId(c *gin.Context, newToken string, userId int64) error {
  252. oldToken := ""
  253. list := strings.Split(c.Request.Header.Get("Authorization"), ".")
  254. if len(list) > 0 {
  255. oldToken = list[len(list)-1]
  256. } else {
  257. return errors.New("token is null")
  258. }
  259. list2 := strings.Split(newToken, ".")
  260. newToken2 := list2[len(list2)-1]
  261. deptIdStr, err := sdk.Runtime.GetCacheAdapter().Get(fmt.Sprintf("enter-dept-%s-%d", oldToken, userId))
  262. if err == nil {
  263. sdk.Runtime.GetCacheAdapter().Set(fmt.Sprintf("enter-dept-%s-%d", newToken2, userId), deptIdStr, int(config.JwtConfig.Timeout)+7200)
  264. sdk.Runtime.GetCacheAdapter().Del(fmt.Sprintf("enter-dept-%s-%d", oldToken, userId))
  265. }
  266. deptName, err := sdk.Runtime.GetCacheAdapter().Get(fmt.Sprintf("enter-dept-name-%s-%d", oldToken, userId))
  267. if err == nil {
  268. sdk.Runtime.GetCacheAdapter().Set(fmt.Sprintf("enter-dept-name-%s-%d", newToken2, userId), deptName, int(config.JwtConfig.Timeout)+7200)
  269. sdk.Runtime.GetCacheAdapter().Del(fmt.Sprintf("enter-dept-name-%s-%d", oldToken, userId))
  270. }
  271. if err == redis.Nil {
  272. return nil
  273. }
  274. return err
  275. }