telegram.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package controller
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "io"
  7. "net/http"
  8. "one-api/common"
  9. "one-api/model"
  10. "sort"
  11. "github.com/gin-contrib/sessions"
  12. "github.com/gin-gonic/gin"
  13. )
  14. func TelegramBind(c *gin.Context) {
  15. if !common.TelegramOAuthEnabled {
  16. c.JSON(200, gin.H{
  17. "message": "管理员未开启通过 Telegram 登录以及注册",
  18. "success": false,
  19. })
  20. return
  21. }
  22. params := c.Request.URL.Query()
  23. if !checkTelegramAuthorization(params, common.TelegramBotToken) {
  24. c.JSON(200, gin.H{
  25. "message": "无效的请求",
  26. "success": false,
  27. })
  28. return
  29. }
  30. telegramId := params["id"][0]
  31. if model.IsTelegramIdAlreadyTaken(telegramId) {
  32. c.JSON(200, gin.H{
  33. "message": "该 Telegram 账户已被绑定",
  34. "success": false,
  35. })
  36. return
  37. }
  38. session := sessions.Default(c)
  39. id := session.Get("id")
  40. user := model.User{Id: id.(int)}
  41. if err := user.FillUserById(); err != nil {
  42. c.JSON(200, gin.H{
  43. "message": err.Error(),
  44. "success": false,
  45. })
  46. return
  47. }
  48. if user.Id == 0 {
  49. c.JSON(http.StatusOK, gin.H{
  50. "success": false,
  51. "message": "用户已注销",
  52. })
  53. return
  54. }
  55. user.TelegramId = telegramId
  56. if err := user.Update(false); err != nil {
  57. c.JSON(200, gin.H{
  58. "message": err.Error(),
  59. "success": false,
  60. })
  61. return
  62. }
  63. c.Redirect(302, "/setting")
  64. }
  65. func TelegramLogin(c *gin.Context) {
  66. if !common.TelegramOAuthEnabled {
  67. c.JSON(200, gin.H{
  68. "message": "管理员未开启通过 Telegram 登录以及注册",
  69. "success": false,
  70. })
  71. return
  72. }
  73. params := c.Request.URL.Query()
  74. if !checkTelegramAuthorization(params, common.TelegramBotToken) {
  75. c.JSON(200, gin.H{
  76. "message": "无效的请求",
  77. "success": false,
  78. })
  79. return
  80. }
  81. telegramId := params["id"][0]
  82. user := model.User{TelegramId: telegramId}
  83. if err := user.FillUserByTelegramId(); err != nil {
  84. c.JSON(200, gin.H{
  85. "message": err.Error(),
  86. "success": false,
  87. })
  88. return
  89. }
  90. setupLogin(&user, c)
  91. }
  92. func checkTelegramAuthorization(params map[string][]string, token string) bool {
  93. strs := []string{}
  94. var hash = ""
  95. for k, v := range params {
  96. if k == "hash" {
  97. hash = v[0]
  98. continue
  99. }
  100. strs = append(strs, k+"="+v[0])
  101. }
  102. sort.Strings(strs)
  103. var imploded = ""
  104. for _, s := range strs {
  105. if imploded != "" {
  106. imploded += "\n"
  107. }
  108. imploded += s
  109. }
  110. sha256hash := sha256.New()
  111. io.WriteString(sha256hash, token)
  112. hmachash := hmac.New(sha256.New, sha256hash.Sum(nil))
  113. io.WriteString(hmachash, imploded)
  114. ss := hex.EncodeToString(hmachash.Sum(nil))
  115. return hash == ss
  116. }