api.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/org"
  16. "github.com/gogits/gogs/routers/api/v1/repo"
  17. "github.com/gogits/gogs/routers/api/v1/user"
  18. )
  19. func RepoAssignment() macaron.Handler {
  20. return func(ctx *middleware.Context) {
  21. userName := ctx.Params(":username")
  22. repoName := ctx.Params(":reponame")
  23. var (
  24. owner *models.User
  25. err error
  26. )
  27. // Check if the user is the same as the repository owner.
  28. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
  29. owner = ctx.User
  30. } else {
  31. owner, err = models.GetUserByName(userName)
  32. if err != nil {
  33. if models.IsErrUserNotExist(err) {
  34. ctx.Error(404)
  35. } else {
  36. ctx.APIError(500, "GetUserByName", err)
  37. }
  38. return
  39. }
  40. }
  41. ctx.Repo.Owner = owner
  42. // Get repository.
  43. repo, err := models.GetRepositoryByName(owner.Id, repoName)
  44. if err != nil {
  45. if models.IsErrRepoNotExist(err) {
  46. ctx.Error(404)
  47. } else {
  48. ctx.APIError(500, "GetRepositoryByName", err)
  49. }
  50. return
  51. } else if err = repo.GetOwner(); err != nil {
  52. ctx.APIError(500, "GetOwner", err)
  53. return
  54. }
  55. mode, err := models.AccessLevel(ctx.User, repo)
  56. if err != nil {
  57. ctx.APIError(500, "AccessLevel", err)
  58. return
  59. }
  60. ctx.Repo.AccessMode = mode
  61. // Check access.
  62. if ctx.Repo.AccessMode == models.ACCESS_MODE_NONE {
  63. ctx.Error(404)
  64. return
  65. }
  66. ctx.Repo.Repository = repo
  67. }
  68. }
  69. // Contexter middleware already checks token for user sign in process.
  70. func ReqToken() macaron.Handler {
  71. return func(ctx *middleware.Context) {
  72. if !ctx.IsSigned {
  73. ctx.Error(401)
  74. return
  75. }
  76. }
  77. }
  78. func ReqBasicAuth() macaron.Handler {
  79. return func(ctx *middleware.Context) {
  80. if !ctx.IsBasicAuth {
  81. ctx.Error(401)
  82. return
  83. }
  84. }
  85. }
  86. func ReqAdmin() macaron.Handler {
  87. return func(ctx *middleware.Context) {
  88. if !ctx.User.IsAdmin {
  89. ctx.Error(403)
  90. return
  91. }
  92. }
  93. }
  94. // RegisterRoutes registers all v1 APIs routes to web application.
  95. // FIXME: custom form error response
  96. func RegisterRoutes(m *macaron.Macaron) {
  97. bind := binding.Bind
  98. m.Group("/v1", func() {
  99. // Miscellaneous
  100. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  101. m.Post("/markdown/raw", misc.MarkdownRaw)
  102. // Users
  103. m.Group("/users", func() {
  104. m.Get("/search", user.Search)
  105. m.Group("/:username", func() {
  106. m.Get("", user.GetInfo)
  107. m.Group("/tokens", func() {
  108. m.Combo("").Get(user.ListAccessTokens).
  109. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  110. }, ReqBasicAuth())
  111. })
  112. })
  113. m.Group("/users", func() {
  114. m.Group("/:username", func() {
  115. m.Get("/keys", user.ListPublicKeys)
  116. m.Get("/followers", user.ListFollowers)
  117. m.Group("/following", func() {
  118. m.Get("", user.ListFollowing)
  119. m.Get("/:target", user.CheckFollowing)
  120. })
  121. })
  122. }, ReqToken())
  123. m.Group("/user", func() {
  124. m.Combo("/emails").Get(user.ListEmails).
  125. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  126. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  127. m.Get("/followers", user.ListMyFollowers)
  128. m.Group("/following", func() {
  129. m.Get("", user.ListMyFollowing)
  130. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  131. })
  132. m.Group("/keys", func() {
  133. m.Combo("").Get(user.ListMyPublicKeys).
  134. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  135. m.Combo("/:id").Get(user.GetPublicKey).
  136. Delete(user.DeletePublicKey)
  137. })
  138. }, ReqToken())
  139. // Repositories
  140. m.Combo("/user/repos", ReqToken()).Get(repo.ListMyRepos).
  141. Post(bind(api.CreateRepoOption{}), repo.Create)
  142. m.Post("/org/:org/repos", ReqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  143. m.Group("/repos", func() {
  144. m.Get("/search", repo.Search)
  145. })
  146. m.Group("/repos", func() {
  147. m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
  148. m.Combo("/:username/:reponame").Get(repo.Get).
  149. Delete(repo.Delete)
  150. m.Group("/:username/:reponame", func() {
  151. m.Combo("/hooks").Get(repo.ListHooks).
  152. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  153. m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), repo.EditHook)
  154. m.Get("/raw/*", middleware.RepoRef(), repo.GetRawFile)
  155. m.Get("/archive/*", repo.GetArchive)
  156. m.Group("/keys", func() {
  157. m.Combo("").Get(repo.ListDeployKeys).
  158. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  159. m.Combo("/:id").Get(repo.GetDeployKey).
  160. Delete(repo.DeleteDeploykey)
  161. })
  162. }, RepoAssignment())
  163. }, ReqToken())
  164. // Organizations
  165. m.Get("/user/orgs", ReqToken(), org.ListMyOrgs)
  166. m.Get("/users/:username/orgs", org.ListUserOrgs)
  167. m.Combo("/orgs/:orgname").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
  168. m.Any("/*", func(ctx *middleware.Context) {
  169. ctx.Error(404)
  170. })
  171. m.Group("/admin", func() {
  172. m.Group("/users", func() {
  173. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  174. m.Group("/:username", func() {
  175. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  176. Delete(admin.DeleteUser)
  177. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  178. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  179. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  180. })
  181. })
  182. }, ReqAdmin())
  183. })
  184. }