api.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. "github.com/G-Node/gogs/internal/context"
  12. "github.com/G-Node/gogs/internal/db"
  13. "github.com/G-Node/gogs/internal/form"
  14. "github.com/G-Node/gogs/internal/route/api/v1/admin"
  15. "github.com/G-Node/gogs/internal/route/api/v1/misc"
  16. "github.com/G-Node/gogs/internal/route/api/v1/org"
  17. "github.com/G-Node/gogs/internal/route/api/v1/repo"
  18. "github.com/G-Node/gogs/internal/route/api/v1/search"
  19. "github.com/G-Node/gogs/internal/route/api/v1/user"
  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.NotFoundOrError(err, "get user by name")
  36. return
  37. }
  38. }
  39. c.Repo.Owner = owner
  40. r, err := db.GetRepositoryByName(owner.ID, reponame)
  41. if err != nil {
  42. c.NotFoundOrError(err, "get repository by name")
  43. return
  44. } else if err = r.GetOwner(); err != nil {
  45. c.Error(err, "get owner")
  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.Error(err, "get user access mode")
  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.NotFoundOrError(err, "get organization by name")
  84. return
  85. }
  86. }
  87. if assignTeam {
  88. c.Org.Team, err = db.GetTeamByID(c.ParamsInt64(":teamid"))
  89. if err != nil {
  90. c.NotFoundOrError(err, "get team by ID")
  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.Status(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.Status(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.Status(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.Status(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.Status(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{}), misc.Markdown)
  156. m.Post("/markdown/raw", misc.MarkdownRaw)
  157. m.Get("/search/:query", search.Search)
  158. // Users
  159. m.Group("/users", func() {
  160. m.Get("/search", user.Search)
  161. m.Group("/:username", func() {
  162. m.Get("", user.GetInfo)
  163. m.Group("/tokens", func() {
  164. m.Combo("").
  165. Get(user.ListAccessTokens).
  166. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  167. }, reqBasicAuth())
  168. })
  169. })
  170. m.Group("/users", func() {
  171. m.Group("/:username", func() {
  172. m.Get("/keys", user.ListPublicKeys)
  173. m.Get("/followers", user.ListFollowers)
  174. m.Group("/following", func() {
  175. m.Get("", user.ListFollowing)
  176. m.Get("/:target", user.CheckFollowing)
  177. })
  178. })
  179. }, reqToken())
  180. m.Group("/user", func() {
  181. m.Get("", user.GetAuthenticatedUser)
  182. m.Combo("/emails").
  183. Get(user.ListEmails).
  184. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  185. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  186. m.Get("/followers", user.ListMyFollowers)
  187. m.Group("/following", func() {
  188. m.Get("", user.ListMyFollowing)
  189. m.Combo("/:username").
  190. Get(user.CheckMyFollowing).
  191. Put(user.Follow).
  192. Delete(user.Unfollow)
  193. })
  194. m.Group("/keys", func() {
  195. m.Combo("").
  196. Get(user.ListMyPublicKeys).
  197. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  198. m.Combo("/:id").
  199. Get(user.GetPublicKey).
  200. Delete(user.DeletePublicKey)
  201. })
  202. m.Get("/issues", repo.ListUserIssues)
  203. }, reqToken())
  204. // Repositories
  205. m.Get("/users/:username/repos", reqToken(), repo.ListUserRepositories)
  206. m.Get("/orgs/:org/repos", reqToken(), repo.ListOrgRepositories)
  207. m.Combo("/user/repos", reqToken()).
  208. Get(repo.ListMyRepos).
  209. Post(bind(api.CreateRepoOption{}), repo.Create)
  210. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  211. m.Group("/repos", func() {
  212. m.Get("/search", repo.Search)
  213. m.Get("/:username/:reponame", repoAssignment(), repo.Get)
  214. m.Get("/suggest/:query", search.Suggest)
  215. })
  216. m.Group("/repos", func() {
  217. m.Post("/migrate", bind(form.MigrateRepo{}), repo.Migrate)
  218. m.Delete("/:username/:reponame", repoAssignment(), repo.Delete)
  219. m.Group("/:username/:reponame", func() {
  220. m.Group("/hooks", func() {
  221. m.Combo("").
  222. Get(repo.ListHooks).
  223. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  224. m.Combo("/:id").
  225. Patch(bind(api.EditHookOption{}), repo.EditHook).
  226. Delete(repo.DeleteHook)
  227. }, reqRepoAdmin())
  228. m.Group("/collaborators", func() {
  229. m.Get("", repo.ListCollaborators)
  230. m.Combo("/:collaborator").
  231. Get(repo.IsCollaborator).
  232. Put(bind(api.AddCollaboratorOption{}), repo.AddCollaborator).
  233. Delete(repo.DeleteCollaborator)
  234. }, reqRepoAdmin())
  235. m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
  236. m.Group("/contents", func() {
  237. m.Get("", repo.GetContents)
  238. m.Get("/*", repo.GetContents)
  239. })
  240. m.Get("/archive/*", repo.GetArchive)
  241. m.Group("/git/trees", func() {
  242. m.Get("/:sha", repo.GetRepoGitTree)
  243. })
  244. m.Get("/forks", repo.ListForks)
  245. m.Group("/branches", func() {
  246. m.Get("", repo.ListBranches)
  247. m.Get("/*", repo.GetBranch)
  248. })
  249. m.Group("/commits", func() {
  250. m.Get("/:sha", repo.GetSingleCommit)
  251. m.Get("/*", repo.GetReferenceSHA)
  252. })
  253. m.Group("/keys", func() {
  254. m.Combo("").
  255. Get(repo.ListDeployKeys).
  256. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  257. m.Combo("/:id").
  258. Get(repo.GetDeployKey).
  259. Delete(repo.DeleteDeploykey)
  260. }, reqRepoAdmin())
  261. m.Group("/issues", func() {
  262. m.Combo("").
  263. Get(repo.ListIssues).
  264. Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
  265. m.Group("/comments", func() {
  266. m.Get("", repo.ListRepoIssueComments)
  267. m.Patch("/:id", bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
  268. })
  269. m.Group("/:index", func() {
  270. m.Combo("").
  271. Get(repo.GetIssue).
  272. Patch(bind(api.EditIssueOption{}), repo.EditIssue)
  273. m.Group("/comments", func() {
  274. m.Combo("").
  275. Get(repo.ListIssueComments).
  276. Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
  277. m.Combo("/:id").
  278. Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
  279. Delete(repo.DeleteIssueComment)
  280. })
  281. m.Get("/labels", repo.ListIssueLabels)
  282. m.Group("/labels", func() {
  283. m.Combo("").
  284. Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
  285. Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
  286. Delete(repo.ClearIssueLabels)
  287. m.Delete("/:id", repo.DeleteIssueLabel)
  288. }, reqRepoWriter())
  289. })
  290. }, mustEnableIssues)
  291. m.Group("/labels", func() {
  292. m.Get("", repo.ListLabels)
  293. m.Get("/:id", repo.GetLabel)
  294. })
  295. m.Group("/labels", func() {
  296. m.Post("", bind(api.CreateLabelOption{}), repo.CreateLabel)
  297. m.Combo("/:id").
  298. Patch(bind(api.EditLabelOption{}), repo.EditLabel).
  299. Delete(repo.DeleteLabel)
  300. }, reqRepoWriter())
  301. m.Group("/milestones", func() {
  302. m.Get("", repo.ListMilestones)
  303. m.Get("/:id", repo.GetMilestone)
  304. })
  305. m.Group("/milestones", func() {
  306. m.Post("", bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
  307. m.Combo("/:id").
  308. Patch(bind(api.EditMilestoneOption{}), repo.EditMilestone).
  309. Delete(repo.DeleteMilestone)
  310. }, reqRepoWriter())
  311. m.Patch("/issue-tracker", reqRepoWriter(), bind(api.EditIssueTrackerOption{}), repo.IssueTracker)
  312. m.Post("/mirror-sync", reqRepoWriter(), repo.MirrorSync)
  313. m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
  314. }, repoAssignment())
  315. }, reqToken())
  316. m.Get("/issues", reqToken(), repo.ListUserIssues)
  317. // Organizations
  318. m.Combo("/user/orgs", reqToken()).
  319. Get(org.ListMyOrgs).
  320. Post(bind(api.CreateOrgOption{}), org.CreateMyOrg)
  321. m.Get("/users/:username/orgs", org.ListUserOrgs)
  322. m.Group("/orgs/:orgname", func() {
  323. m.Combo("").
  324. Get(org.Get).
  325. Patch(bind(api.EditOrgOption{}), org.Edit)
  326. m.Group("/teams", func() {
  327. m.Combo("").
  328. Get(org.ListTeams).
  329. Post(bind(api.CreateTeamOption{}), org.CreateTeam)
  330. m.Combo("/:team").
  331. Get(org.GetTeam).
  332. Patch(bind(api.CreateTeamOption{}), org.EditTeam).
  333. Delete(org.DeleteTeam)
  334. })
  335. }, orgAssignment(true))
  336. m.Group("/admin", func() {
  337. m.Group("/users", func() {
  338. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  339. m.Group("/:username", func() {
  340. m.Combo("").
  341. Patch(bind(api.EditUserOption{}), admin.EditUser).
  342. Delete(admin.DeleteUser)
  343. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  344. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  345. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  346. })
  347. })
  348. m.Group("/orgs/:orgname", func() {
  349. m.Group("/teams", func() {
  350. m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
  351. })
  352. })
  353. m.Group("/teams", func() {
  354. m.Group("/:teamid", func() {
  355. m.Combo("/members/:username").
  356. Put(admin.AddTeamMember).
  357. Delete(admin.RemoveTeamMember)
  358. m.Combo("/repos/:reponame").
  359. Put(admin.AddTeamRepository).
  360. Delete(admin.RemoveTeamRepository)
  361. }, orgAssignment(false, true))
  362. })
  363. }, reqAdmin())
  364. m.Any("/*", func(c *context.Context) {
  365. c.NotFound()
  366. })
  367. }, context.APIContexter())
  368. }