api.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. admin2 "gogs.io/gogs/internal/route/api/v1/admin"
  7. misc2 "gogs.io/gogs/internal/route/api/v1/misc"
  8. org2 "gogs.io/gogs/internal/route/api/v1/org"
  9. repo2 "gogs.io/gogs/internal/route/api/v1/repo"
  10. user2 "gogs.io/gogs/internal/route/api/v1/user"
  11. "net/http"
  12. "strings"
  13. "github.com/go-macaron/binding"
  14. "gopkg.in/macaron.v1"
  15. api "github.com/gogs/go-gogs-client"
  16. "gogs.io/gogs/internal/context"
  17. "gogs.io/gogs/internal/db"
  18. "gogs.io/gogs/internal/db/errors"
  19. "gogs.io/gogs/internal/form"
  20. )
  21. // repoAssignment extracts information from URL parameters to retrieve the repository,
  22. // and makes sure the context user has at least the read access to the repository.
  23. func repoAssignment() macaron.Handler {
  24. return func(c *context.APIContext) {
  25. username := c.Params(":username")
  26. reponame := c.Params(":reponame")
  27. var err error
  28. var owner *db.User
  29. // Check if the context user is the repository owner.
  30. if c.IsLogged && c.User.LowerName == strings.ToLower(username) {
  31. owner = c.User
  32. } else {
  33. owner, err = db.GetUserByName(username)
  34. if err != nil {
  35. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  36. return
  37. }
  38. }
  39. c.Repo.Owner = owner
  40. r, err := db.GetRepositoryByName(owner.ID, reponame)
  41. if err != nil {
  42. c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
  43. return
  44. } else if err = r.GetOwner(); err != nil {
  45. c.ServerError("GetOwner", err)
  46. return
  47. }
  48. if c.IsTokenAuth && c.User.IsAdmin {
  49. c.Repo.AccessMode = db.ACCESS_MODE_OWNER
  50. } else {
  51. mode, err := db.UserAccessMode(c.UserID(), r)
  52. if err != nil {
  53. c.ServerError("UserAccessMode", err)
  54. return
  55. }
  56. c.Repo.AccessMode = mode
  57. }
  58. if !c.Repo.HasAccess() {
  59. c.NotFound()
  60. return
  61. }
  62. c.Repo.Repository = r
  63. }
  64. }
  65. // orgAssignment extracts information from URL parameters to retrieve the organization or team.
  66. func orgAssignment(args ...bool) macaron.Handler {
  67. var (
  68. assignOrg bool
  69. assignTeam bool
  70. )
  71. if len(args) > 0 {
  72. assignOrg = args[0]
  73. }
  74. if len(args) > 1 {
  75. assignTeam = args[1]
  76. }
  77. return func(c *context.APIContext) {
  78. c.Org = new(context.APIOrganization)
  79. var err error
  80. if assignOrg {
  81. c.Org.Organization, err = db.GetUserByName(c.Params(":orgname"))
  82. if err != nil {
  83. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  84. return
  85. }
  86. }
  87. if assignTeam {
  88. c.Org.Team, err = db.GetTeamByID(c.ParamsInt64(":teamid"))
  89. if err != nil {
  90. c.NotFoundOrServerError("GetTeamByID", errors.IsTeamNotExist, err)
  91. return
  92. }
  93. }
  94. }
  95. }
  96. // reqToken makes sure the context user is authorized via access token.
  97. func reqToken() macaron.Handler {
  98. return func(c *context.Context) {
  99. if !c.IsTokenAuth {
  100. c.Error(http.StatusUnauthorized)
  101. return
  102. }
  103. }
  104. }
  105. // reqBasicAuth makes sure the context user is authorized via HTTP Basic Auth.
  106. func reqBasicAuth() macaron.Handler {
  107. return func(c *context.Context) {
  108. if !c.IsBasicAuth {
  109. c.Error(http.StatusUnauthorized)
  110. return
  111. }
  112. }
  113. }
  114. // reqAdmin makes sure the context user is a site admin.
  115. func reqAdmin() macaron.Handler {
  116. return func(c *context.Context) {
  117. if !c.IsLogged || !c.User.IsAdmin {
  118. c.Error(http.StatusForbidden)
  119. return
  120. }
  121. }
  122. }
  123. // reqRepoWriter makes sure the context user has at least write access to the repository.
  124. func reqRepoWriter() macaron.Handler {
  125. return func(c *context.Context) {
  126. if !c.Repo.IsWriter() {
  127. c.Error(http.StatusForbidden)
  128. return
  129. }
  130. }
  131. }
  132. // reqRepoWriter makes sure the context user has at least admin access to the repository.
  133. func reqRepoAdmin() macaron.Handler {
  134. return func(c *context.Context) {
  135. if !c.Repo.IsAdmin() {
  136. c.Error(http.StatusForbidden)
  137. return
  138. }
  139. }
  140. }
  141. func mustEnableIssues(c *context.APIContext) {
  142. if !c.Repo.Repository.EnableIssues || c.Repo.Repository.EnableExternalTracker {
  143. c.NotFound()
  144. return
  145. }
  146. }
  147. // RegisterRoutes registers all route in API v1 to the web application.
  148. // FIXME: custom form error response
  149. func RegisterRoutes(m *macaron.Macaron) {
  150. bind := binding.Bind
  151. m.Group("/v1", func() {
  152. // Handle preflight OPTIONS request
  153. m.Options("/*", func() {})
  154. // Miscellaneous
  155. m.Post("/markdown", bind(api.MarkdownOption{}), misc2.Markdown)
  156. m.Post("/markdown/raw", misc2.MarkdownRaw)
  157. // Users
  158. m.Group("/users", func() {
  159. m.Get("/search", user2.Search)
  160. m.Group("/:username", func() {
  161. m.Get("", user2.GetInfo)
  162. m.Group("/tokens", func() {
  163. m.Combo("").
  164. Get(user2.ListAccessTokens).
  165. Post(bind(api.CreateAccessTokenOption{}), user2.CreateAccessToken)
  166. }, reqBasicAuth())
  167. })
  168. })
  169. m.Group("/users", func() {
  170. m.Group("/:username", func() {
  171. m.Get("/keys", user2.ListPublicKeys)
  172. m.Get("/followers", user2.ListFollowers)
  173. m.Group("/following", func() {
  174. m.Get("", user2.ListFollowing)
  175. m.Get("/:target", user2.CheckFollowing)
  176. })
  177. })
  178. }, reqToken())
  179. m.Group("/user", func() {
  180. m.Get("", user2.GetAuthenticatedUser)
  181. m.Combo("/emails").
  182. Get(user2.ListEmails).
  183. Post(bind(api.CreateEmailOption{}), user2.AddEmail).
  184. Delete(bind(api.CreateEmailOption{}), user2.DeleteEmail)
  185. m.Get("/followers", user2.ListMyFollowers)
  186. m.Group("/following", func() {
  187. m.Get("", user2.ListMyFollowing)
  188. m.Combo("/:username").
  189. Get(user2.CheckMyFollowing).
  190. Put(user2.Follow).
  191. Delete(user2.Unfollow)
  192. })
  193. m.Group("/keys", func() {
  194. m.Combo("").
  195. Get(user2.ListMyPublicKeys).
  196. Post(bind(api.CreateKeyOption{}), user2.CreatePublicKey)
  197. m.Combo("/:id").
  198. Get(user2.GetPublicKey).
  199. Delete(user2.DeletePublicKey)
  200. })
  201. m.Get("/issues", repo2.ListUserIssues)
  202. }, reqToken())
  203. // Repositories
  204. m.Get("/users/:username/repos", reqToken(), repo2.ListUserRepositories)
  205. m.Get("/orgs/:org/repos", reqToken(), repo2.ListOrgRepositories)
  206. m.Combo("/user/repos", reqToken()).
  207. Get(repo2.ListMyRepos).
  208. Post(bind(api.CreateRepoOption{}), repo2.Create)
  209. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo2.CreateOrgRepo)
  210. m.Group("/repos", func() {
  211. m.Get("/search", repo2.Search)
  212. m.Get("/:username/:reponame", repoAssignment(), repo2.Get)
  213. })
  214. m.Group("/repos", func() {
  215. m.Post("/migrate", bind(form.MigrateRepo{}), repo2.Migrate)
  216. m.Delete("/:username/:reponame", repoAssignment(), repo2.Delete)
  217. m.Group("/:username/:reponame", func() {
  218. m.Group("/hooks", func() {
  219. m.Combo("").
  220. Get(repo2.ListHooks).
  221. Post(bind(api.CreateHookOption{}), repo2.CreateHook)
  222. m.Combo("/:id").
  223. Patch(bind(api.EditHookOption{}), repo2.EditHook).
  224. Delete(repo2.DeleteHook)
  225. }, reqRepoAdmin())
  226. m.Group("/collaborators", func() {
  227. m.Get("", repo2.ListCollaborators)
  228. m.Combo("/:collaborator").
  229. Get(repo2.IsCollaborator).
  230. Put(bind(api.AddCollaboratorOption{}), repo2.AddCollaborator).
  231. Delete(repo2.DeleteCollaborator)
  232. }, reqRepoAdmin())
  233. m.Get("/raw/*", context.RepoRef(), repo2.GetRawFile)
  234. m.Get("/archive/*", repo2.GetArchive)
  235. m.Get("/forks", repo2.ListForks)
  236. m.Group("/branches", func() {
  237. m.Get("", repo2.ListBranches)
  238. m.Get("/*", repo2.GetBranch)
  239. })
  240. m.Group("/commits", func() {
  241. m.Get("/:sha", repo2.GetSingleCommit)
  242. m.Get("/*", repo2.GetReferenceSHA)
  243. })
  244. m.Group("/keys", func() {
  245. m.Combo("").
  246. Get(repo2.ListDeployKeys).
  247. Post(bind(api.CreateKeyOption{}), repo2.CreateDeployKey)
  248. m.Combo("/:id").
  249. Get(repo2.GetDeployKey).
  250. Delete(repo2.DeleteDeploykey)
  251. }, reqRepoAdmin())
  252. m.Group("/issues", func() {
  253. m.Combo("").
  254. Get(repo2.ListIssues).
  255. Post(bind(api.CreateIssueOption{}), repo2.CreateIssue)
  256. m.Group("/comments", func() {
  257. m.Get("", repo2.ListRepoIssueComments)
  258. m.Patch("/:id", bind(api.EditIssueCommentOption{}), repo2.EditIssueComment)
  259. })
  260. m.Group("/:index", func() {
  261. m.Combo("").
  262. Get(repo2.GetIssue).
  263. Patch(bind(api.EditIssueOption{}), repo2.EditIssue)
  264. m.Group("/comments", func() {
  265. m.Combo("").
  266. Get(repo2.ListIssueComments).
  267. Post(bind(api.CreateIssueCommentOption{}), repo2.CreateIssueComment)
  268. m.Combo("/:id").
  269. Patch(bind(api.EditIssueCommentOption{}), repo2.EditIssueComment).
  270. Delete(repo2.DeleteIssueComment)
  271. })
  272. m.Get("/labels", repo2.ListIssueLabels)
  273. m.Group("/labels", func() {
  274. m.Combo("").
  275. Post(bind(api.IssueLabelsOption{}), repo2.AddIssueLabels).
  276. Put(bind(api.IssueLabelsOption{}), repo2.ReplaceIssueLabels).
  277. Delete(repo2.ClearIssueLabels)
  278. m.Delete("/:id", repo2.DeleteIssueLabel)
  279. }, reqRepoWriter())
  280. })
  281. }, mustEnableIssues)
  282. m.Group("/labels", func() {
  283. m.Get("", repo2.ListLabels)
  284. m.Get("/:id", repo2.GetLabel)
  285. })
  286. m.Group("/labels", func() {
  287. m.Post("", bind(api.CreateLabelOption{}), repo2.CreateLabel)
  288. m.Combo("/:id").
  289. Patch(bind(api.EditLabelOption{}), repo2.EditLabel).
  290. Delete(repo2.DeleteLabel)
  291. }, reqRepoWriter())
  292. m.Group("/milestones", func() {
  293. m.Get("", repo2.ListMilestones)
  294. m.Get("/:id", repo2.GetMilestone)
  295. })
  296. m.Group("/milestones", func() {
  297. m.Post("", bind(api.CreateMilestoneOption{}), repo2.CreateMilestone)
  298. m.Combo("/:id").
  299. Patch(bind(api.EditMilestoneOption{}), repo2.EditMilestone).
  300. Delete(repo2.DeleteMilestone)
  301. }, reqRepoWriter())
  302. m.Patch("/issue-tracker", reqRepoWriter(), bind(api.EditIssueTrackerOption{}), repo2.IssueTracker)
  303. m.Post("/mirror-sync", reqRepoWriter(), repo2.MirrorSync)
  304. m.Get("/editorconfig/:filename", context.RepoRef(), repo2.GetEditorconfig)
  305. }, repoAssignment())
  306. }, reqToken())
  307. m.Get("/issues", reqToken(), repo2.ListUserIssues)
  308. // Organizations
  309. m.Combo("/user/orgs", reqToken()).
  310. Get(org2.ListMyOrgs).
  311. Post(bind(api.CreateOrgOption{}), org2.CreateMyOrg)
  312. m.Get("/users/:username/orgs", org2.ListUserOrgs)
  313. m.Group("/orgs/:orgname", func() {
  314. m.Combo("").
  315. Get(org2.Get).
  316. Patch(bind(api.EditOrgOption{}), org2.Edit)
  317. m.Get("/teams", org2.ListTeams)
  318. }, orgAssignment(true))
  319. m.Group("/admin", func() {
  320. m.Group("/users", func() {
  321. m.Post("", bind(api.CreateUserOption{}), admin2.CreateUser)
  322. m.Group("/:username", func() {
  323. m.Combo("").
  324. Patch(bind(api.EditUserOption{}), admin2.EditUser).
  325. Delete(admin2.DeleteUser)
  326. m.Post("/keys", bind(api.CreateKeyOption{}), admin2.CreatePublicKey)
  327. m.Post("/orgs", bind(api.CreateOrgOption{}), admin2.CreateOrg)
  328. m.Post("/repos", bind(api.CreateRepoOption{}), admin2.CreateRepo)
  329. })
  330. })
  331. m.Group("/orgs/:orgname", func() {
  332. m.Group("/teams", func() {
  333. m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin2.CreateTeam)
  334. })
  335. })
  336. m.Group("/teams", func() {
  337. m.Group("/:teamid", func() {
  338. m.Combo("/members/:username").
  339. Put(admin2.AddTeamMember).
  340. Delete(admin2.RemoveTeamMember)
  341. m.Combo("/repos/:reponame").
  342. Put(admin2.AddTeamRepository).
  343. Delete(admin2.RemoveTeamRepository)
  344. }, orgAssignment(false, true))
  345. })
  346. }, reqAdmin())
  347. m.Any("/*", func(c *context.Context) {
  348. c.NotFound()
  349. })
  350. }, context.APIContexter())
  351. }