api.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package v1
  5. import (
  6. "strings"
  7. "github.com/go-macaron/binding"
  8. "gopkg.in/macaron.v1"
  9. api "github.com/gogits/go-gogs-client"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/auth"
  12. "github.com/gogits/gogs/modules/middleware"
  13. "github.com/gogits/gogs/routers/api/v1/admin"
  14. "github.com/gogits/gogs/routers/api/v1/misc"
  15. "github.com/gogits/gogs/routers/api/v1/repo"
  16. "github.com/gogits/gogs/routers/api/v1/user"
  17. )
  18. func RepoAssignment() macaron.Handler {
  19. return func(ctx *middleware.Context) {
  20. userName := ctx.Params(":username")
  21. repoName := ctx.Params(":reponame")
  22. var (
  23. owner *models.User
  24. err error
  25. )
  26. // Check if the user is the same as the repository owner.
  27. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
  28. owner = ctx.User
  29. } else {
  30. owner, err = models.GetUserByName(userName)
  31. if err != nil {
  32. if models.IsErrUserNotExist(err) {
  33. ctx.Error(404)
  34. } else {
  35. ctx.APIError(500, "GetUserByName", err)
  36. }
  37. return
  38. }
  39. }
  40. ctx.Repo.Owner = owner
  41. // Get repository.
  42. repo, err := models.GetRepositoryByName(owner.Id, repoName)
  43. if err != nil {
  44. if models.IsErrRepoNotExist(err) {
  45. ctx.Error(404)
  46. } else {
  47. ctx.APIError(500, "GetRepositoryByName", err)
  48. }
  49. return
  50. } else if err = repo.GetOwner(); err != nil {
  51. ctx.APIError(500, "GetOwner", err)
  52. return
  53. }
  54. mode, err := models.AccessLevel(ctx.User, repo)
  55. if err != nil {
  56. ctx.APIError(500, "AccessLevel", err)
  57. return
  58. }
  59. ctx.Repo.AccessMode = mode
  60. // Check access.
  61. if ctx.Repo.AccessMode == models.ACCESS_MODE_NONE {
  62. ctx.Error(404)
  63. return
  64. }
  65. ctx.Repo.Repository = repo
  66. }
  67. }
  68. // Contexter middleware already checks token for user sign in process.
  69. func ReqToken() macaron.Handler {
  70. return func(ctx *middleware.Context) {
  71. if !ctx.IsSigned {
  72. ctx.Error(401)
  73. return
  74. }
  75. }
  76. }
  77. func ReqBasicAuth() macaron.Handler {
  78. return func(ctx *middleware.Context) {
  79. if !ctx.IsBasicAuth {
  80. ctx.Error(401)
  81. return
  82. }
  83. }
  84. }
  85. func ReqAdmin() macaron.Handler {
  86. return func(ctx *middleware.Context) {
  87. if !ctx.User.IsAdmin {
  88. ctx.Error(403)
  89. return
  90. }
  91. }
  92. }
  93. // RegisterRoutes registers all v1 APIs routes to web application.
  94. // FIXME: custom form error response
  95. func RegisterRoutes(m *macaron.Macaron) {
  96. bind := binding.Bind
  97. m.Group("/v1", func() {
  98. // Miscellaneous
  99. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  100. m.Post("/markdown/raw", misc.MarkdownRaw)
  101. // Users
  102. m.Group("/users", func() {
  103. m.Get("/search", user.Search)
  104. m.Group("/:username", func() {
  105. m.Get("", user.GetInfo)
  106. m.Group("/tokens", func() {
  107. m.Combo("").Get(user.ListAccessTokens).
  108. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  109. }, ReqBasicAuth())
  110. })
  111. })
  112. m.Group("/users", func() {
  113. m.Group("/:username", func() {
  114. m.Get("/keys", user.ListPublicKeys)
  115. })
  116. }, ReqToken())
  117. m.Group("/user", func() {
  118. m.Group("/keys", func() {
  119. m.Combo("").Get(user.ListMyPublicKeys).
  120. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  121. m.Combo("/:id").Get(user.GetPublicKey).
  122. Delete(user.DeletePublicKey)
  123. })
  124. }, ReqToken())
  125. // Repositories
  126. m.Combo("/user/repos", ReqToken()).Get(repo.ListMyRepos).
  127. Post(bind(api.CreateRepoOption{}), repo.Create)
  128. m.Post("/org/:org/repos", ReqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  129. m.Group("/repos", func() {
  130. m.Get("/search", repo.Search)
  131. })
  132. m.Group("/repos", func() {
  133. m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
  134. m.Combo("/:username/:reponame").Get(repo.Get).
  135. Delete(repo.Delete)
  136. m.Group("/:username/:reponame", func() {
  137. m.Combo("/hooks").Get(repo.ListHooks).
  138. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  139. m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), repo.EditHook)
  140. m.Get("/raw/*", middleware.RepoRef(), repo.GetRawFile)
  141. m.Get("/archive/*", repo.GetArchive)
  142. m.Group("/keys", func() {
  143. m.Combo("").Get(repo.ListDeployKeys).
  144. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  145. m.Combo("/:id").Get(repo.GetDeployKey).
  146. Delete(repo.DeleteDeploykey)
  147. })
  148. }, RepoAssignment())
  149. }, ReqToken())
  150. m.Any("/*", func(ctx *middleware.Context) {
  151. ctx.Error(404)
  152. })
  153. m.Group("/admin", func() {
  154. m.Group("/users", func() {
  155. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  156. m.Group("/:username", func() {
  157. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  158. Delete(admin.DeleteUser)
  159. m.Post("/keys", admin.CreatePublicKey)
  160. })
  161. })
  162. }, ReqAdmin())
  163. })
  164. }