api.go 12 KB

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