misc.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package controller
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "message-pusher/common"
  9. "message-pusher/model"
  10. "net/http"
  11. "sort"
  12. "strings"
  13. )
  14. func GetStatus(c *gin.Context) {
  15. c.JSON(http.StatusOK, gin.H{
  16. "success": true,
  17. "message": "",
  18. "data": gin.H{
  19. "version": common.Version,
  20. "start_time": common.StartTime,
  21. "email_verification": common.EmailVerificationEnabled,
  22. "github_oauth": common.GitHubOAuthEnabled,
  23. "github_client_id": common.GitHubClientId,
  24. "system_name": common.SystemName,
  25. "footer_html": common.Footer,
  26. "wechat_qrcode": common.WeChatAccountQRCodeImageURL,
  27. "wechat_login": common.WeChatAuthEnabled,
  28. "server_address": common.ServerAddress,
  29. "turnstile_check": common.TurnstileCheckEnabled,
  30. "turnstile_site_key": common.TurnstileSiteKey,
  31. },
  32. })
  33. return
  34. }
  35. func GetNotice(c *gin.Context) {
  36. common.OptionMapRWMutex.RLock()
  37. defer common.OptionMapRWMutex.RUnlock()
  38. c.JSON(http.StatusOK, gin.H{
  39. "success": true,
  40. "message": "",
  41. "data": common.OptionMap["Notice"],
  42. })
  43. return
  44. }
  45. func GetAbout(c *gin.Context) {
  46. common.OptionMapRWMutex.RLock()
  47. defer common.OptionMapRWMutex.RUnlock()
  48. c.JSON(http.StatusOK, gin.H{
  49. "success": true,
  50. "message": "",
  51. "data": common.OptionMap["About"],
  52. })
  53. return
  54. }
  55. func SendEmailVerification(c *gin.Context) {
  56. email := c.Query("email")
  57. if err := common.Validate.Var(email, "required,email"); err != nil {
  58. c.JSON(http.StatusOK, gin.H{
  59. "success": false,
  60. "message": "无效的参数",
  61. })
  62. return
  63. }
  64. if model.IsEmailAlreadyTaken(email) {
  65. c.JSON(http.StatusOK, gin.H{
  66. "success": false,
  67. "message": "邮箱地址已被占用",
  68. })
  69. return
  70. }
  71. code := common.GenerateVerificationCode(6)
  72. common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
  73. subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
  74. content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
  75. "<p>您的验证码为: <strong>%s</strong></p>"+
  76. "<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, common.VerificationValidMinutes)
  77. err := common.SendEmail(subject, email, content)
  78. if err != nil {
  79. c.JSON(http.StatusOK, gin.H{
  80. "success": false,
  81. "message": err.Error(),
  82. })
  83. return
  84. }
  85. c.JSON(http.StatusOK, gin.H{
  86. "success": true,
  87. "message": "",
  88. })
  89. return
  90. }
  91. func SendPasswordResetEmail(c *gin.Context) {
  92. email := c.Query("email")
  93. if err := common.Validate.Var(email, "required,email"); err != nil {
  94. c.JSON(http.StatusOK, gin.H{
  95. "success": false,
  96. "message": "无效的参数",
  97. })
  98. return
  99. }
  100. if !model.IsEmailAlreadyTaken(email) {
  101. c.JSON(http.StatusOK, gin.H{
  102. "success": false,
  103. "message": "该邮箱地址未注册",
  104. })
  105. return
  106. }
  107. code := common.GenerateVerificationCode(0)
  108. common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
  109. link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", common.ServerAddress, email, code)
  110. subject := fmt.Sprintf("%s密码重置", common.SystemName)
  111. content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
  112. "<p>点击<a href='%s'>此处</a>进行密码重置。</p>"+
  113. "<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, common.VerificationValidMinutes)
  114. err := common.SendEmail(subject, email, content)
  115. if err != nil {
  116. c.JSON(http.StatusOK, gin.H{
  117. "success": false,
  118. "message": err.Error(),
  119. })
  120. return
  121. }
  122. c.JSON(http.StatusOK, gin.H{
  123. "success": true,
  124. "message": "",
  125. })
  126. return
  127. }
  128. type PasswordResetRequest struct {
  129. Email string `json:"email"`
  130. Token string `json:"token"`
  131. }
  132. func ResetPassword(c *gin.Context) {
  133. var req PasswordResetRequest
  134. err := json.NewDecoder(c.Request.Body).Decode(&req)
  135. if req.Email == "" || req.Token == "" {
  136. c.JSON(http.StatusOK, gin.H{
  137. "success": false,
  138. "message": "无效的参数",
  139. })
  140. return
  141. }
  142. if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
  143. c.JSON(http.StatusOK, gin.H{
  144. "success": false,
  145. "message": "重置链接非法或已过期",
  146. })
  147. return
  148. }
  149. password := common.GenerateVerificationCode(12)
  150. err = model.ResetUserPasswordByEmail(req.Email, password)
  151. if err != nil {
  152. c.JSON(http.StatusOK, gin.H{
  153. "success": false,
  154. "message": err.Error(),
  155. })
  156. return
  157. }
  158. common.DeleteKey(req.Email, common.PasswordResetPurpose)
  159. c.JSON(http.StatusOK, gin.H{
  160. "success": true,
  161. "message": "",
  162. "data": password,
  163. })
  164. return
  165. }
  166. func WeChatTestAccountVerification(c *gin.Context) {
  167. user := model.User{Username: c.Param("username")}
  168. user.FillUserByUsername()
  169. // https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
  170. signature := c.Query("signature")
  171. timestamp := c.Query("timestamp")
  172. nonce := c.Query("nonce")
  173. echoStr := c.Query("echostr")
  174. arr := []string{user.WeChatTestAccountVerificationToken, timestamp, nonce}
  175. sort.Strings(arr)
  176. str := strings.Join(arr, "")
  177. hash := sha1.Sum([]byte(str))
  178. hexStr := hex.EncodeToString(hash[:])
  179. if signature == hexStr {
  180. c.String(http.StatusOK, echoStr)
  181. } else {
  182. c.Status(http.StatusForbidden)
  183. }
  184. }