user.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  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. // POST /api/users/update/info -> user.UpdateUserInfoHandler
  107. updateUserInfoEndpoint := factory.NewAPIEndpoint(
  108. &types.APIRequestMetadata{
  109. Verb: types.APIVerbUpdate,
  110. Method: types.HTTPVerbPost,
  111. Path: &types.Path{
  112. Parent: basePath,
  113. RelativePath: "/users/update/info",
  114. },
  115. Scopes: []types.PermissionScope{types.UserScope},
  116. },
  117. )
  118. updateUserInfoHandler := user.NewUpdateUserInfoHandler(
  119. config,
  120. factory.GetDecoderValidator(),
  121. factory.GetResultWriter(),
  122. )
  123. routes = append(routes, &router.Route{
  124. Endpoint: updateUserInfoEndpoint,
  125. Handler: updateUserInfoHandler,
  126. Router: r,
  127. })
  128. // POST /api/onboarding_step -> user.UpdateOnboardingStepHandler
  129. updateOnboardingStepEndpoint := factory.NewAPIEndpoint(
  130. &types.APIRequestMetadata{
  131. Verb: types.APIVerbUpdate,
  132. Method: types.HTTPVerbPost,
  133. Path: &types.Path{
  134. Parent: basePath,
  135. RelativePath: "/onboarding_step",
  136. },
  137. Scopes: []types.PermissionScope{types.UserScope},
  138. },
  139. )
  140. updateOnboardingStepHandler := user.NewUpdateOnboardingStepHandler(
  141. config,
  142. factory.GetDecoderValidator(),
  143. factory.GetResultWriter(),
  144. )
  145. routes = append(routes, &router.Route{
  146. Endpoint: updateOnboardingStepEndpoint,
  147. Handler: updateOnboardingStepHandler,
  148. Router: r,
  149. })
  150. // GET /api/users/current -> user.NewUserGetCurrentHandler
  151. authCheckEndpoint := factory.NewAPIEndpoint(
  152. &types.APIRequestMetadata{
  153. Verb: types.APIVerbGet,
  154. Method: types.HTTPVerbGet,
  155. Path: &types.Path{
  156. Parent: basePath,
  157. RelativePath: "/users/current",
  158. },
  159. Scopes: []types.PermissionScope{types.UserScope},
  160. },
  161. )
  162. authCheckHandler := user.NewUserGetCurrentHandler(
  163. config,
  164. factory.GetResultWriter(),
  165. )
  166. routes = append(routes, &router.Route{
  167. Endpoint: authCheckEndpoint,
  168. Handler: authCheckHandler,
  169. Router: r,
  170. })
  171. // DELETE /api/users/current -> user.NewUserDeleteHandler
  172. deleteUserEndpoint := factory.NewAPIEndpoint(
  173. &types.APIRequestMetadata{
  174. Verb: types.APIVerbDelete,
  175. Method: types.HTTPVerbDelete,
  176. Path: &types.Path{
  177. Parent: basePath,
  178. RelativePath: "/users/current",
  179. },
  180. Scopes: []types.PermissionScope{types.UserScope},
  181. },
  182. )
  183. deleteUserHandler := user.NewUserDeleteHandler(
  184. config,
  185. factory.GetResultWriter(),
  186. )
  187. routes = append(routes, &router.Route{
  188. Endpoint: deleteUserEndpoint,
  189. Handler: deleteUserHandler,
  190. Router: r,
  191. })
  192. // POST /api/projects -> project.NewProjectCreateHandler
  193. createEndpoint := factory.NewAPIEndpoint(
  194. &types.APIRequestMetadata{
  195. Verb: types.APIVerbCreate,
  196. Method: types.HTTPVerbPost,
  197. Path: &types.Path{
  198. Parent: basePath,
  199. RelativePath: "/projects",
  200. },
  201. Scopes: []types.PermissionScope{types.UserScope},
  202. },
  203. )
  204. createHandler := project.NewProjectCreateHandler(
  205. config,
  206. factory.GetDecoderValidator(),
  207. factory.GetResultWriter(),
  208. )
  209. routes = append(routes, &router.Route{
  210. Endpoint: createEndpoint,
  211. Handler: createHandler,
  212. Router: r,
  213. })
  214. // GET /api/projects -> project.NewProjectListHandler
  215. listEndpoint := factory.NewAPIEndpoint(
  216. &types.APIRequestMetadata{
  217. Verb: types.APIVerbList,
  218. Method: types.HTTPVerbGet,
  219. Path: &types.Path{
  220. Parent: basePath,
  221. RelativePath: "/projects",
  222. },
  223. Scopes: []types.PermissionScope{types.UserScope},
  224. },
  225. )
  226. listHandler := project.NewProjectListHandler(
  227. config,
  228. factory.GetResultWriter(),
  229. )
  230. routes = append(routes, &router.Route{
  231. Endpoint: listEndpoint,
  232. Handler: listHandler,
  233. Router: r,
  234. })
  235. // POST /email/verify/initiate -> user.VerifyEmailInitiateHandler
  236. emailVerifyInitiateEndpoint := factory.NewAPIEndpoint(
  237. &types.APIRequestMetadata{
  238. Verb: types.APIVerbUpdate,
  239. Method: types.HTTPVerbPost,
  240. Path: &types.Path{
  241. Parent: basePath,
  242. RelativePath: "/email/verify/initiate",
  243. },
  244. Scopes: []types.PermissionScope{types.UserScope},
  245. },
  246. )
  247. emailVerifyInitiateHandler := user.NewVerifyEmailInitiateHandler(config)
  248. routes = append(routes, &router.Route{
  249. Endpoint: emailVerifyInitiateEndpoint,
  250. Handler: emailVerifyInitiateHandler,
  251. Router: r,
  252. })
  253. // GET /email/verify/finalize -> user.VerifyEmailInitiateHandler
  254. emailVerifyFinalizeEndpoint := factory.NewAPIEndpoint(
  255. &types.APIRequestMetadata{
  256. Verb: types.APIVerbGet,
  257. Method: types.HTTPVerbGet,
  258. Path: &types.Path{
  259. Parent: basePath,
  260. RelativePath: "/email/verify/finalize",
  261. },
  262. Scopes: []types.PermissionScope{types.UserScope},
  263. ShouldRedirect: true,
  264. },
  265. )
  266. emailVerifyFinalizeHandler := user.NewVerifyEmailFinalizeHandler(
  267. config,
  268. factory.GetDecoderValidator(),
  269. )
  270. routes = append(routes, &router.Route{
  271. Endpoint: emailVerifyFinalizeEndpoint,
  272. Handler: emailVerifyFinalizeHandler,
  273. Router: r,
  274. })
  275. // GET /api/templates -> template.NewTemplateListHandler
  276. listTemplatesEndpoint := factory.NewAPIEndpoint(
  277. &types.APIRequestMetadata{
  278. Verb: types.APIVerbList,
  279. Method: types.HTTPVerbGet,
  280. Path: &types.Path{
  281. Parent: basePath,
  282. RelativePath: "/templates",
  283. },
  284. Scopes: []types.PermissionScope{types.UserScope},
  285. },
  286. )
  287. listTemplatesRequest := template.NewTemplateListHandler(
  288. config,
  289. factory.GetDecoderValidator(),
  290. factory.GetResultWriter(),
  291. )
  292. routes = append(routes, &router.Route{
  293. Endpoint: listTemplatesEndpoint,
  294. Handler: listTemplatesRequest,
  295. Router: r,
  296. })
  297. // GET /api/templates/{name}/{version} -> template.NewTemplateGetHandler
  298. getTemplateEndpoint := factory.NewAPIEndpoint(
  299. &types.APIRequestMetadata{
  300. Verb: types.APIVerbGet,
  301. Method: types.HTTPVerbGet,
  302. Path: &types.Path{
  303. Parent: basePath,
  304. RelativePath: fmt.Sprintf(
  305. "/templates/{%s}/{%s}",
  306. types.URLParamTemplateName,
  307. types.URLParamTemplateVersion,
  308. ),
  309. },
  310. Scopes: []types.PermissionScope{types.UserScope},
  311. },
  312. )
  313. getTemplateRequest := template.NewTemplateGetHandler(
  314. config,
  315. factory.GetDecoderValidator(),
  316. factory.GetResultWriter(),
  317. )
  318. routes = append(routes, &router.Route{
  319. Endpoint: getTemplateEndpoint,
  320. Handler: getTemplateRequest,
  321. Router: r,
  322. })
  323. // GET /api/templates/{name}/{version}/upgrade_notes -> template.NewTemplateGetUpgradeNotesHandler
  324. getTemplateUpgradeNotesEndpoint := factory.NewAPIEndpoint(
  325. &types.APIRequestMetadata{
  326. Verb: types.APIVerbGet,
  327. Method: types.HTTPVerbGet,
  328. Path: &types.Path{
  329. Parent: basePath,
  330. RelativePath: fmt.Sprintf(
  331. "/templates/{%s}/{%s}/upgrade_notes",
  332. types.URLParamTemplateName,
  333. types.URLParamTemplateVersion,
  334. ),
  335. },
  336. Scopes: []types.PermissionScope{types.UserScope},
  337. },
  338. )
  339. getTemplateUpgradeNotesRequest := template.NewTemplateGetUpgradeNotesHandler(
  340. config,
  341. factory.GetDecoderValidator(),
  342. factory.GetResultWriter(),
  343. )
  344. routes = append(routes, &router.Route{
  345. Endpoint: getTemplateUpgradeNotesEndpoint,
  346. Handler: getTemplateUpgradeNotesRequest,
  347. Router: r,
  348. })
  349. // GET /api/integrations/github-app/oauth -> gitinstallation.NewGithubAppOAuthStartHandler
  350. githubAppOAuthStartEndpoint := 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/oauth",
  357. },
  358. Scopes: []types.PermissionScope{
  359. types.UserScope,
  360. },
  361. },
  362. )
  363. githubAppOAuthStartHandler := gitinstallation.NewGithubAppOAuthStartHandler(
  364. config,
  365. )
  366. routes = append(routes, &router.Route{
  367. Endpoint: githubAppOAuthStartEndpoint,
  368. Handler: githubAppOAuthStartHandler,
  369. Router: r,
  370. })
  371. // GET /api/oauth/github-app/callback -> gitinstallation.GithubAppOAuthCallbackHandler
  372. githubAppOAuthCallbackEndpoint := factory.NewAPIEndpoint(
  373. &types.APIRequestMetadata{
  374. Verb: types.APIVerbGet,
  375. Method: types.HTTPVerbGet,
  376. Path: &types.Path{
  377. Parent: basePath,
  378. RelativePath: "/oauth/github-app/callback",
  379. },
  380. Scopes: []types.PermissionScope{types.UserScope},
  381. },
  382. )
  383. githubAppOAuthCallbackHandler := gitinstallation.NewGithubAppOAuthCallbackHandler(
  384. config,
  385. factory.GetDecoderValidator(),
  386. factory.GetResultWriter(),
  387. )
  388. routes = append(routes, &router.Route{
  389. Endpoint: githubAppOAuthCallbackEndpoint,
  390. Handler: githubAppOAuthCallbackHandler,
  391. Router: r,
  392. })
  393. // GET /api/integrations/github-app/accounts -> gitinstallation.NewGetGithubAppAccountsHandler
  394. githubAppAccountsEndpoint := factory.NewAPIEndpoint(
  395. &types.APIRequestMetadata{
  396. Verb: types.APIVerbGet,
  397. Method: types.HTTPVerbGet,
  398. Path: &types.Path{
  399. Parent: basePath,
  400. RelativePath: "/integrations/github-app/accounts",
  401. },
  402. Scopes: []types.PermissionScope{types.UserScope},
  403. },
  404. )
  405. githubAppAccountsHandler := gitinstallation.NewGetGithubAppAccountsHandler(
  406. config,
  407. factory.GetDecoderValidator(),
  408. factory.GetResultWriter(),
  409. )
  410. routes = append(routes, &router.Route{
  411. Endpoint: githubAppAccountsEndpoint,
  412. Handler: githubAppAccountsHandler,
  413. Router: r,
  414. })
  415. // GET /api/can_create_project -> user.CanCreateProject
  416. canCreateProjectEndpoint := factory.NewAPIEndpoint(
  417. &types.APIRequestMetadata{
  418. Verb: types.APIVerbGet,
  419. Method: types.HTTPVerbGet,
  420. Path: &types.Path{
  421. Parent: basePath,
  422. RelativePath: "/can_create_project",
  423. },
  424. Scopes: []types.PermissionScope{types.UserScope},
  425. },
  426. )
  427. canCreateProjectHandler := user.NewCanCreateProjectHandler(
  428. config,
  429. factory.GetDecoderValidator(),
  430. factory.GetResultWriter(),
  431. )
  432. routes = append(routes, &router.Route{
  433. Endpoint: canCreateProjectEndpoint,
  434. Handler: canCreateProjectHandler,
  435. Router: r,
  436. })
  437. return routes
  438. }