user.go 11 KB

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