user.go 9.7 KB

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