api.go 12 KB

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