2
0

user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/gitinstallation"
  6. "github.com/porter-dev/porter/api/server/handlers/project"
  7. "github.com/porter-dev/porter/api/server/handlers/template"
  8. "github.com/porter-dev/porter/api/server/handlers/user"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/server/shared/router"
  12. "github.com/porter-dev/porter/api/types"
  13. )
  14. func NewUserScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  15. return &router.Registerer{
  16. GetRoutes: GetUserScopedRoutes,
  17. Children: children,
  18. }
  19. }
  20. func GetUserScopedRoutes(
  21. r chi.Router,
  22. config *config.Config,
  23. basePath *types.Path,
  24. factory shared.APIEndpointFactory,
  25. children ...*router.Registerer,
  26. ) []*router.Route {
  27. routes := getUserRoutes(r, config, basePath, factory)
  28. for _, child := range children {
  29. r.Group(func(r chi.Router) {
  30. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  31. routes = append(routes, childRoutes...)
  32. })
  33. }
  34. return routes
  35. }
  36. func getUserRoutes(
  37. r chi.Router,
  38. config *config.Config,
  39. basePath *types.Path,
  40. factory shared.APIEndpointFactory,
  41. ) []*router.Route {
  42. routes := make([]*router.Route, 0)
  43. // POST /api/welcome -> user.NewUserWelcomeHandler
  44. welcomeEndpoint := factory.NewAPIEndpoint(
  45. &types.APIRequestMetadata{
  46. Verb: types.APIVerbCreate,
  47. Method: types.HTTPVerbPost,
  48. Path: &types.Path{
  49. Parent: basePath,
  50. RelativePath: "/welcome",
  51. },
  52. Scopes: []types.PermissionScope{types.UserScope},
  53. },
  54. )
  55. welcomeHandler := user.NewUserWelcomeHandler(
  56. config,
  57. factory.GetDecoderValidator(),
  58. factory.GetResultWriter(),
  59. )
  60. routes = append(routes, &router.Route{
  61. Endpoint: welcomeEndpoint,
  62. Handler: welcomeHandler,
  63. Router: r,
  64. })
  65. // GET /api/cli/login -> user.user.NewCLILoginHandler
  66. cliLoginUserEndpoint := factory.NewAPIEndpoint(
  67. &types.APIRequestMetadata{
  68. Verb: types.APIVerbGet,
  69. Method: types.HTTPVerbGet,
  70. Path: &types.Path{
  71. Parent: basePath,
  72. RelativePath: "/cli/login",
  73. },
  74. Scopes: []types.PermissionScope{types.UserScope},
  75. ShouldRedirect: true,
  76. },
  77. )
  78. cliLoginUserHandler := user.NewCLILoginHandler(
  79. config,
  80. factory.GetDecoderValidator(),
  81. factory.GetResultWriter(),
  82. )
  83. routes = append(routes, &router.Route{
  84. Endpoint: cliLoginUserEndpoint,
  85. Handler: cliLoginUserHandler,
  86. Router: r,
  87. })
  88. // POST /api/logout -> user.NewUserLogoutHandler
  89. logoutUserEndpoint := factory.NewAPIEndpoint(
  90. &types.APIRequestMetadata{
  91. Verb: types.APIVerbUpdate,
  92. Method: types.HTTPVerbPost,
  93. Path: &types.Path{
  94. Parent: basePath,
  95. RelativePath: "/logout",
  96. },
  97. Scopes: []types.PermissionScope{types.UserScope},
  98. },
  99. )
  100. logoutUserHandler := user.NewUserLogoutHandler(config)
  101. routes = append(routes, &router.Route{
  102. Endpoint: logoutUserEndpoint,
  103. Handler: logoutUserHandler,
  104. Router: r,
  105. })
  106. // GET /api/users/current -> user.NewUserGetCurrentHandler
  107. authCheckEndpoint := factory.NewAPIEndpoint(
  108. &types.APIRequestMetadata{
  109. Verb: types.APIVerbGet,
  110. Method: types.HTTPVerbGet,
  111. Path: &types.Path{
  112. Parent: basePath,
  113. RelativePath: "/users/current",
  114. },
  115. Scopes: []types.PermissionScope{types.UserScope},
  116. },
  117. )
  118. authCheckHandler := user.NewUserGetCurrentHandler(
  119. config,
  120. factory.GetResultWriter(),
  121. )
  122. routes = append(routes, &router.Route{
  123. Endpoint: authCheckEndpoint,
  124. Handler: authCheckHandler,
  125. Router: r,
  126. })
  127. // DELETE /api/users/current -> user.NewUserDeleteHandler
  128. deleteUserEndpoint := factory.NewAPIEndpoint(
  129. &types.APIRequestMetadata{
  130. Verb: types.APIVerbDelete,
  131. Method: types.HTTPVerbDelete,
  132. Path: &types.Path{
  133. Parent: basePath,
  134. RelativePath: "/users/current",
  135. },
  136. Scopes: []types.PermissionScope{types.UserScope},
  137. },
  138. )
  139. deleteUserHandler := user.NewUserDeleteHandler(
  140. config,
  141. factory.GetResultWriter(),
  142. )
  143. routes = append(routes, &router.Route{
  144. Endpoint: deleteUserEndpoint,
  145. Handler: deleteUserHandler,
  146. Router: r,
  147. })
  148. // POST /api/projects -> project.NewProjectCreateHandler
  149. createEndpoint := factory.NewAPIEndpoint(
  150. &types.APIRequestMetadata{
  151. Verb: types.APIVerbCreate,
  152. Method: types.HTTPVerbPost,
  153. Path: &types.Path{
  154. Parent: basePath,
  155. RelativePath: "/projects",
  156. },
  157. Scopes: []types.PermissionScope{types.UserScope},
  158. },
  159. )
  160. createHandler := project.NewProjectCreateHandler(
  161. config,
  162. factory.GetDecoderValidator(),
  163. factory.GetResultWriter(),
  164. )
  165. routes = append(routes, &router.Route{
  166. Endpoint: createEndpoint,
  167. Handler: createHandler,
  168. Router: r,
  169. })
  170. // GET /api/projects -> project.NewProjectListHandler
  171. listEndpoint := factory.NewAPIEndpoint(
  172. &types.APIRequestMetadata{
  173. Verb: types.APIVerbList,
  174. Method: types.HTTPVerbGet,
  175. Path: &types.Path{
  176. Parent: basePath,
  177. RelativePath: "/projects",
  178. },
  179. Scopes: []types.PermissionScope{types.UserScope},
  180. },
  181. )
  182. listHandler := project.NewProjectListHandler(
  183. config,
  184. factory.GetResultWriter(),
  185. )
  186. routes = append(routes, &router.Route{
  187. Endpoint: listEndpoint,
  188. Handler: listHandler,
  189. Router: r,
  190. })
  191. // POST /email/verify/initiate -> user.VerifyEmailInitiateHandler
  192. emailVerifyInitiateEndpoint := factory.NewAPIEndpoint(
  193. &types.APIRequestMetadata{
  194. Verb: types.APIVerbUpdate,
  195. Method: types.HTTPVerbPost,
  196. Path: &types.Path{
  197. Parent: basePath,
  198. RelativePath: "/email/verify/initiate",
  199. },
  200. Scopes: []types.PermissionScope{types.UserScope},
  201. },
  202. )
  203. emailVerifyInitiateHandler := user.NewVerifyEmailInitiateHandler(config)
  204. routes = append(routes, &router.Route{
  205. Endpoint: emailVerifyInitiateEndpoint,
  206. Handler: emailVerifyInitiateHandler,
  207. Router: r,
  208. })
  209. // GET /email/verify/finalize -> user.VerifyEmailInitiateHandler
  210. emailVerifyFinalizeEndpoint := factory.NewAPIEndpoint(
  211. &types.APIRequestMetadata{
  212. Verb: types.APIVerbGet,
  213. Method: types.HTTPVerbGet,
  214. Path: &types.Path{
  215. Parent: basePath,
  216. RelativePath: "/email/verify/finalize",
  217. },
  218. Scopes: []types.PermissionScope{types.UserScope},
  219. ShouldRedirect: true,
  220. },
  221. )
  222. emailVerifyFinalizeHandler := user.NewVerifyEmailFinalizeHandler(
  223. config,
  224. factory.GetDecoderValidator(),
  225. )
  226. routes = append(routes, &router.Route{
  227. Endpoint: emailVerifyFinalizeEndpoint,
  228. Handler: emailVerifyFinalizeHandler,
  229. Router: r,
  230. })
  231. // GET /api/templates -> template.NewTemplateListHandler
  232. listTemplatesEndpoint := factory.NewAPIEndpoint(
  233. &types.APIRequestMetadata{
  234. Verb: types.APIVerbList,
  235. Method: types.HTTPVerbGet,
  236. Path: &types.Path{
  237. Parent: basePath,
  238. RelativePath: "/templates",
  239. },
  240. Scopes: []types.PermissionScope{types.UserScope},
  241. },
  242. )
  243. listTemplatesRequest := template.NewTemplateListHandler(
  244. config,
  245. factory.GetDecoderValidator(),
  246. factory.GetResultWriter(),
  247. )
  248. routes = append(routes, &router.Route{
  249. Endpoint: listTemplatesEndpoint,
  250. Handler: listTemplatesRequest,
  251. Router: r,
  252. })
  253. // GET /api/templates/{name}/{version} -> template.NewTemplateGetHandler
  254. getTemplateEndpoint := factory.NewAPIEndpoint(
  255. &types.APIRequestMetadata{
  256. Verb: types.APIVerbGet,
  257. Method: types.HTTPVerbGet,
  258. Path: &types.Path{
  259. Parent: basePath,
  260. RelativePath: fmt.Sprintf(
  261. "/templates/{%s}/{%s}",
  262. types.URLParamTemplateName,
  263. types.URLParamTemplateVersion,
  264. ),
  265. },
  266. Scopes: []types.PermissionScope{types.UserScope},
  267. },
  268. )
  269. getTemplateRequest := template.NewTemplateGetHandler(
  270. config,
  271. factory.GetDecoderValidator(),
  272. factory.GetResultWriter(),
  273. )
  274. routes = append(routes, &router.Route{
  275. Endpoint: getTemplateEndpoint,
  276. Handler: getTemplateRequest,
  277. Router: r,
  278. })
  279. // GET /api/templates/{name}/{version}/upgrade_notes -> template.NewTemplateGetUpgradeNotesHandler
  280. getTemplateUpgradeNotesEndpoint := factory.NewAPIEndpoint(
  281. &types.APIRequestMetadata{
  282. Verb: types.APIVerbGet,
  283. Method: types.HTTPVerbGet,
  284. Path: &types.Path{
  285. Parent: basePath,
  286. RelativePath: fmt.Sprintf(
  287. "/templates/{%s}/{%s}/upgrade_notes",
  288. types.URLParamTemplateName,
  289. types.URLParamTemplateVersion,
  290. ),
  291. },
  292. Scopes: []types.PermissionScope{types.UserScope},
  293. },
  294. )
  295. getTemplateUpgradeNotesRequest := template.NewTemplateGetUpgradeNotesHandler(
  296. config,
  297. factory.GetDecoderValidator(),
  298. factory.GetResultWriter(),
  299. )
  300. routes = append(routes, &router.Route{
  301. Endpoint: getTemplateUpgradeNotesEndpoint,
  302. Handler: getTemplateUpgradeNotesRequest,
  303. Router: r,
  304. })
  305. // GET /api/integrations/github-app/oauth -> gitinstallation.NewGithubAppOAuthStartHandler
  306. githubAppOAuthStartEndpoint := factory.NewAPIEndpoint(
  307. &types.APIRequestMetadata{
  308. Verb: types.APIVerbGet,
  309. Method: types.HTTPVerbGet,
  310. Path: &types.Path{
  311. Parent: basePath,
  312. RelativePath: "/integrations/github-app/oauth",
  313. },
  314. Scopes: []types.PermissionScope{
  315. types.UserScope,
  316. },
  317. },
  318. )
  319. githubAppOAuthStartHandler := gitinstallation.NewGithubAppOAuthStartHandler(
  320. config,
  321. )
  322. routes = append(routes, &router.Route{
  323. Endpoint: githubAppOAuthStartEndpoint,
  324. Handler: githubAppOAuthStartHandler,
  325. Router: r,
  326. })
  327. // GET /api/oauth/github-app/callback -> gitinstallation.GithubAppOAuthCallbackHandler
  328. githubAppOAuthCallbackEndpoint := factory.NewAPIEndpoint(
  329. &types.APIRequestMetadata{
  330. Verb: types.APIVerbGet,
  331. Method: types.HTTPVerbGet,
  332. Path: &types.Path{
  333. Parent: basePath,
  334. RelativePath: "/oauth/github-app/callback",
  335. },
  336. Scopes: []types.PermissionScope{types.UserScope},
  337. },
  338. )
  339. githubAppOAuthCallbackHandler := gitinstallation.NewGithubAppOAuthCallbackHandler(
  340. config,
  341. factory.GetDecoderValidator(),
  342. factory.GetResultWriter(),
  343. )
  344. routes = append(routes, &router.Route{
  345. Endpoint: githubAppOAuthCallbackEndpoint,
  346. Handler: githubAppOAuthCallbackHandler,
  347. Router: r,
  348. })
  349. // GET /api/integrations/github-app/accounts -> gitinstallation.NewGetGithubAppAccountsHandler
  350. githubAppAccountsEndpoint := factory.NewAPIEndpoint(
  351. &types.APIRequestMetadata{
  352. Verb: types.APIVerbGet,
  353. Method: types.HTTPVerbGet,
  354. Path: &types.Path{
  355. Parent: basePath,
  356. RelativePath: "/integrations/github-app/accounts",
  357. },
  358. Scopes: []types.PermissionScope{types.UserScope},
  359. },
  360. )
  361. githubAppAccountsHandler := gitinstallation.NewGetGithubAppAccountsHandler(
  362. config,
  363. factory.GetDecoderValidator(),
  364. factory.GetResultWriter(),
  365. )
  366. routes = append(routes, &router.Route{
  367. Endpoint: githubAppAccountsEndpoint,
  368. Handler: githubAppAccountsHandler,
  369. Router: r,
  370. })
  371. // GET /api/can_create_project -> user.CanCreateProject
  372. canCreateProjectEndpoint := factory.NewAPIEndpoint(
  373. &types.APIRequestMetadata{
  374. Verb: types.APIVerbGet,
  375. Method: types.HTTPVerbGet,
  376. Path: &types.Path{
  377. Parent: basePath,
  378. RelativePath: "/can_create_project",
  379. },
  380. Scopes: []types.PermissionScope{types.UserScope},
  381. },
  382. )
  383. canCreateProjectHandler := user.NewCanCreateProjectHandler(
  384. config,
  385. factory.GetDecoderValidator(),
  386. factory.GetResultWriter(),
  387. )
  388. routes = append(routes, &router.Route{
  389. Endpoint: canCreateProjectEndpoint,
  390. Handler: canCreateProjectHandler,
  391. Router: r,
  392. })
  393. return routes
  394. }