auth.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package middleware
  2. import (
  3. "github.com/gin-contrib/sessions"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "strings"
  9. )
  10. func authHelper(c *gin.Context, minRole int) {
  11. session := sessions.Default(c)
  12. username := session.Get("username")
  13. role := session.Get("role")
  14. id := session.Get("id")
  15. status := session.Get("status")
  16. if username == nil {
  17. // Check access token
  18. accessToken := c.Request.Header.Get("Authorization")
  19. if accessToken == "" {
  20. c.JSON(http.StatusUnauthorized, gin.H{
  21. "success": false,
  22. "message": "无权进行此操作,未登录且未提供 access token",
  23. })
  24. c.Abort()
  25. return
  26. }
  27. user := model.ValidateAccessToken(accessToken)
  28. if user != nil && user.Username != "" {
  29. // Token is valid
  30. username = user.Username
  31. role = user.Role
  32. id = user.Id
  33. status = user.Status
  34. } else {
  35. c.JSON(http.StatusOK, gin.H{
  36. "success": false,
  37. "message": "无权进行此操作,access token 无效",
  38. })
  39. c.Abort()
  40. return
  41. }
  42. }
  43. if status.(int) == common.UserStatusDisabled {
  44. c.JSON(http.StatusOK, gin.H{
  45. "success": false,
  46. "message": "用户已被封禁",
  47. })
  48. c.Abort()
  49. return
  50. }
  51. if role.(int) < minRole {
  52. c.JSON(http.StatusOK, gin.H{
  53. "success": false,
  54. "message": "无权进行此操作,权限不足",
  55. })
  56. c.Abort()
  57. return
  58. }
  59. c.Set("username", username)
  60. c.Set("role", role)
  61. c.Set("id", id)
  62. c.Next()
  63. }
  64. func UserAuth() func(c *gin.Context) {
  65. return func(c *gin.Context) {
  66. authHelper(c, common.RoleCommonUser)
  67. }
  68. }
  69. func AdminAuth() func(c *gin.Context) {
  70. return func(c *gin.Context) {
  71. authHelper(c, common.RoleAdminUser)
  72. }
  73. }
  74. func RootAuth() func(c *gin.Context) {
  75. return func(c *gin.Context) {
  76. authHelper(c, common.RoleRootUser)
  77. }
  78. }
  79. func TokenAuth() func(c *gin.Context) {
  80. return func(c *gin.Context) {
  81. key := c.Request.Header.Get("Authorization")
  82. parts := strings.Split(key, "-")
  83. key = parts[0]
  84. token, err := model.ValidateUserToken(key)
  85. if err != nil {
  86. c.JSON(http.StatusOK, gin.H{
  87. "error": gin.H{
  88. "message": err.Error(),
  89. "type": "one_api_error",
  90. },
  91. })
  92. c.Abort()
  93. return
  94. }
  95. if !model.IsUserEnabled(token.UserId) {
  96. c.JSON(http.StatusOK, gin.H{
  97. "error": gin.H{
  98. "message": "用户已被封禁",
  99. "type": "one_api_error",
  100. },
  101. })
  102. c.Abort()
  103. return
  104. }
  105. c.Set("id", token.UserId)
  106. c.Set("token_id", token.Id)
  107. requestURL := c.Request.URL.String()
  108. consumeQuota := false
  109. switch requestURL {
  110. case "/v1/chat/completions":
  111. consumeQuota = !token.UnlimitedQuota
  112. case "/v1/completions":
  113. consumeQuota = !token.UnlimitedQuota
  114. case "/v1/edits":
  115. consumeQuota = !token.UnlimitedQuota
  116. }
  117. c.Set("consume_quota", consumeQuota)
  118. if len(parts) > 1 {
  119. if model.IsAdmin(token.UserId) {
  120. c.Set("channelId", parts[1])
  121. } else {
  122. c.JSON(http.StatusOK, gin.H{
  123. "error": gin.H{
  124. "message": "普通用户不支持指定渠道",
  125. "type": "one_api_error",
  126. },
  127. })
  128. c.Abort()
  129. return
  130. }
  131. }
  132. c.Next()
  133. }
  134. }