user.go 13 KB

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