idaas.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package service
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. log "github.com/sirupsen/logrus"
  7. "kpt.xdmy/apiserver/model"
  8. "kpt.xdmy/apiserver/model/http"
  9. "kpt.xdmy/middleware/jwt"
  10. "kpt.xdmy/pkg/logging"
  11. "kpt.xdmy/pkg/setting"
  12. "kpt.xdmy/pkg/util"
  13. "net"
  14. "time"
  15. )
  16. func (s *Service) OAuth2Token(code, host string) (*http.OAuth2TokenResp, error) {
  17. authorization := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(setting.IDaaSSetting.ClientId+":"+setting.IDaaSSetting.ClientSecret)))
  18. resp := new(http.OAuth2TokenResp)
  19. fmt.Println(code, time.Now())
  20. //host = "http://tmrwatch.cn:8082"
  21. if host == "eam" {
  22. host = "https://192-168-61-418000-8jyo36i0p8n4to.ztna-dingtalk.com"
  23. url := fmt.Sprintf(setting.IDaaSSetting.Url+"/api/v1/oauth2/token?&grant_type=%s&code=%s&redirect_uri=%s", "authorization_code",
  24. code, fmt.Sprintf("%s/api/v1/oauth2/token", host))
  25. respByte, err := jwt.PostPush(url, nil, authorization)
  26. if err != nil {
  27. host = "https://eam.modernfarming.cn:8000"
  28. url := fmt.Sprintf(setting.IDaaSSetting.Url+"/api/v1/oauth2/token?&grant_type=%s&code=%s&redirect_uri=%s", "authorization_code",
  29. code, fmt.Sprintf("%s/api/v1/oauth2/token", host))
  30. respByte, err := jwt.PostPush(url, nil, authorization)
  31. if err != nil {
  32. log.Error(err)
  33. return resp, err
  34. }
  35. err = json.Unmarshal(respByte, &resp)
  36. if err != nil {
  37. log.Error(err)
  38. return resp, err
  39. }
  40. return resp, nil
  41. }
  42. err = json.Unmarshal(respByte, &resp)
  43. if err != nil {
  44. log.Error(err)
  45. return resp, err
  46. }
  47. return resp, nil
  48. }
  49. fmt.Println(fmt.Sprintf("%s/api/v1/oauth2/token", host))
  50. url := fmt.Sprintf(setting.IDaaSSetting.Url+"/api/v1/oauth2/token?&grant_type=%s&code=%s&redirect_uri=%s", "authorization_code",
  51. code, fmt.Sprintf("%s/api/v1/oauth2/token", host))
  52. respByte, err := jwt.PostPush(url, nil, authorization)
  53. if err != nil {
  54. log.Error(err)
  55. return resp, err
  56. }
  57. err = json.Unmarshal(respByte, &resp)
  58. if err != nil {
  59. log.Error(err)
  60. return resp, err
  61. }
  62. return resp, nil
  63. }
  64. func (s *Service) OAuth2Logout(token, host string) error {
  65. authorization := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(setting.IDaaSSetting.ClientId+":"+setting.IDaaSSetting.ClientSecret)))
  66. url := fmt.Sprintf(setting.IDaaSSetting.Url+"/api/v1/oauth2/introspect?token=%s&token_type_hint=%s", token, "access_token")
  67. _, err := jwt.PostPush(url, nil, authorization)
  68. if err != nil {
  69. log.Error(err)
  70. return err
  71. }
  72. //host = "http://tmrwatch.cn:8082"
  73. if host == "eam" {
  74. host = "https://192-168-61-418000-8jyo36i0p8n4to.ztna-dingtalk.com"
  75. _, err = jwt.PostPush(fmt.Sprintf("%s/api/v1/logout?redirect_url=%s&client_id=%s", setting.IDaaSSetting.Url, host, setting.IDaaSSetting.ClientId), nil, "")
  76. if err != nil {
  77. host = "https://eam.modernfarming.cn:8000"
  78. _, err = jwt.PostPush(fmt.Sprintf("%s/api/v1/logout?redirect_url=%s&client_id=%s", setting.IDaaSSetting.Url, host, setting.IDaaSSetting.ClientId), nil, "")
  79. if err != nil {
  80. log.Error(err)
  81. return err
  82. }
  83. }
  84. return nil
  85. }
  86. _, err = jwt.PostPush(fmt.Sprintf("%s/api/v1/logout?redirect_url=%s&client_id=%s", setting.IDaaSSetting.Url, host, setting.IDaaSSetting.ClientId), nil, "")
  87. if err != nil {
  88. log.Error(err)
  89. return err
  90. }
  91. return nil
  92. }
  93. func getHostIp() string {
  94. addrList, err := net.InterfaceAddrs()
  95. if err != nil {
  96. fmt.Println("get current host ip err: ", err)
  97. return ""
  98. }
  99. var ip string
  100. for _, address := range addrList {
  101. if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
  102. if ipNet.IP.To4() != nil {
  103. ip = ipNet.IP.String()
  104. break
  105. }
  106. }
  107. }
  108. return ip
  109. }
  110. func (s *Service) OAuth2AddUser(userInfo *http.OAuth2AddUser) (*model.UserInfo, error) {
  111. var userCount, empCount int64
  112. err := s.d.DB.Table("emp").Where("empCode = ? ", userInfo.Username).Count(&empCount).Error
  113. if err != nil {
  114. logging.Error(err)
  115. return nil, err
  116. }
  117. emp1 := new(model.Emp)
  118. s.d.DB.Table("emp").Where("empCode = ? ", userInfo.Username).First(&emp1)
  119. pasture := new(model.Pasture)
  120. if userInfo.Pasture != "" {
  121. err = s.d.DB.Where("name = ? ", userInfo.Pasture).First(&pasture).Error
  122. if err != nil {
  123. logging.Error(err)
  124. return nil, err
  125. }
  126. }
  127. emp := new(model.Emp)
  128. emp.Empname = userInfo.Name
  129. emp.EmpCode = userInfo.Username
  130. emp.Empname = userInfo.Name
  131. if !userInfo.Disabled {
  132. emp.Work = 1
  133. } else {
  134. emp.Work = 0
  135. }
  136. if empCount == 0 {
  137. emp.PastureId = "18"
  138. } else {
  139. emp.PastureId = emp1.PastureId
  140. }
  141. if empCount == 0 {
  142. err = s.d.DB.Create(&emp).Error
  143. if err != nil {
  144. logging.Error(err)
  145. return nil, err
  146. }
  147. } else {
  148. fmt.Println(emp)
  149. err = s.d.DB.Debug().Exec(` update emp set work = ? ,pastureId = ? ,empname = ? where empCode = ? `,
  150. emp.Work, emp.PastureId, emp.Empname, userInfo.Username).Error
  151. if err != nil {
  152. logging.Error(err)
  153. return nil, err
  154. }
  155. err = s.d.DB.Where("empCode = ? ", userInfo.Username).First(&emp).Error
  156. if err != nil {
  157. logging.Error(err)
  158. return nil, err
  159. }
  160. }
  161. err = s.d.DB.Table("user").Where("username = ? ", userInfo.Username).Count(&userCount).Error
  162. if err != nil {
  163. logging.Error(err)
  164. return nil, err
  165. }
  166. user := new(model.UserInfo)
  167. user.Empid = emp.Id
  168. user.Username = userInfo.Username
  169. user.Enable = emp.Work
  170. user.Idaas = userInfo.UserId
  171. if userInfo.PassWord != "" {
  172. user.PassWord = util.EncodeMD5(userInfo.PassWord)
  173. }
  174. if userCount == 0 {
  175. err = s.d.DB.Create(&user).Error
  176. if err != nil {
  177. logging.Error(err)
  178. return user, err
  179. }
  180. } else {
  181. err = s.d.DB.Debug().Exec(`UPDATE user SET empid =?, password=?,enable = ? WHERE username = ? `, user.Empid, user.PassWord, user.Enable, user.Username).Error
  182. if err != nil {
  183. logging.Error(err)
  184. return user, err
  185. }
  186. err = s.d.DB.Where(" username = ? ", user.Username).First(&user).Error
  187. if err != nil {
  188. logging.Error(err)
  189. return user, err
  190. }
  191. }
  192. return user, nil
  193. }