SsoController.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package partial
  2. import (
  3. "fmt"
  4. "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
  5. "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
  6. "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
  7. sysmodel "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
  8. sysutils "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
  9. "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils/auth"
  10. "regexp"
  11. "strings"
  12. //"git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
  13. )
  14. // _Login
  15. // @Title _Login
  16. // @Description 用户登录
  17. // @Param logininfo false "登录信息"
  18. // @Success 200 {object} Account
  19. // @Failure 403 :id is empty
  20. func Sso_Login(c *entitys.CtrlContext) {
  21. iauth := getAuth(c)
  22. if iauth == nil{
  23. hostname := sysutils.GetHostname(c.Ctx)
  24. c.Ctx.JSON(500, sysmodel.SysReturn{500, "iauth of " + hostname + " is nil", nil})
  25. return
  26. }
  27. iauth.Login(c)
  28. }
  29. // _Logout
  30. // @Title _Logout
  31. // @Description 用户退出
  32. // @Success 200 {object} Account
  33. // @Failure 403 :id is empty
  34. func Sso_Logout(c *entitys.CtrlContext) {
  35. iauth := getAuth(c)
  36. if iauth == nil{
  37. hostname := sysutils.GetHostname(c.Ctx)
  38. c.Ctx.JSON(500, sysmodel.SysReturn{500, "iauth of " + hostname + " is nil", nil})
  39. return
  40. }
  41. iauth.Logout(c)
  42. }
  43. func Sso_Validate(c *entitys.CtrlContext) {
  44. accessToken := c.Ctx.Query("access_token")
  45. loginId := c.Ctx.Query("username")
  46. requesterType := c.Ctx.Query("type")
  47. if requesterType == "app" {
  48. //timestamp := c.Ctx.Query("timestamp")
  49. //signature := c.Ctx.Query("signature")
  50. //token, err := models.ValidateApp(loginId, accessToken, timestamp, signature)
  51. //if err != nil {
  52. // this.Ctx.WriteString(err.Error())
  53. // this.Ctx.Output.SetStatus(401)
  54. // return
  55. //}
  56. //this.Ctx.Output.JSON(token, false, false)
  57. } else {
  58. domain := strings.TrimSpace(strings.ToLower(c.Ctx.Query("domain")))
  59. token, err := sysutils.Validate(accessToken, loginId, domain)
  60. if err != nil {
  61. logs.Error(accessToken, loginId, "校验AccessToken失败:", err)
  62. c.Ctx.Data(401, "text", []byte(err.Error()))
  63. return
  64. }
  65. c.Ctx.JSON(200, token)
  66. }
  67. }
  68. // _TokenValidate
  69. // @Title _TokenValidate
  70. // @Description token验证
  71. // @Param token string false "token"
  72. // @Success 200 {object} Account
  73. // @Failure 403 :id is empty
  74. func Sso_TokenValidate(c *entitys.CtrlContext) {
  75. token := c.Ctx.Query("token")
  76. user, err := sysutils.TokenValidate(token)
  77. if err != nil {
  78. logs.Error(token, "校验AccessToken失败:", err)
  79. c.Ctx.Data(401, "text", []byte(err.Error()))
  80. return
  81. }
  82. c.Ctx.JSON(200, user)
  83. }
  84. func getAuth(c *entitys.CtrlContext)auth.IAuth{
  85. var iauth auth.IAuth = nil
  86. authMode := config.AppConfig.GetKey("auth_mode")
  87. if authMode == "local"{
  88. iauth = auth.GetAuth("qianqiusoft.com")
  89. }else{
  90. hostname := sysutils.GetHostname(c.Ctx)
  91. tld := getTLD(hostname)
  92. fmt.Println("------>hostname", hostname, "tld", tld)
  93. iauth = auth.GetAuth(hostname)
  94. if iauth == nil{
  95. iauth = auth.GetAuth(tld)
  96. }
  97. }
  98. return iauth
  99. }
  100. /**
  101. * @brief: 获取一级域名
  102. × @param1 hostname: 请求名称
  103. */
  104. func getTLD(hostname string)string{
  105. patternstr := `(2(5[0-5]{1}|[0-4]\d{1})|[0-1]?\d{1,2})(\.(2(5[0-5]{1}|[0-4]\d{1})|[0-1]?\d{1,2})){3}`
  106. reg := regexp.MustCompile(patternstr)
  107. if res := reg.FindAllString(hostname, -1); res == nil {
  108. size := 0
  109. if strings.HasSuffix(hostname, "gov.cn") || strings.HasSuffix(hostname, "edu.cn"){
  110. size = 3
  111. }else{
  112. size = 2
  113. }
  114. hnarr := strings.Split(hostname, ".")
  115. if len(hnarr) >= size{
  116. tld := hnarr[len(hnarr) - size]
  117. for i := size - 1; i >= 1; i--{
  118. tld += "." + hnarr[len(hnarr) - i]
  119. }
  120. return tld
  121. }else{
  122. // 少于两个的直接返回
  123. return hostname
  124. }
  125. } else {
  126. // 直接返回ip
  127. return hostname
  128. }
  129. }
  130. func __none_func_sso__(params ...interface{}) bool {
  131. return true
  132. }