project_oauth.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/project_oauth"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. func NewProjectOAuthScopedRegisterer(children ...*Registerer) *Registerer {
  10. return &Registerer{
  11. GetRoutes: GetProjectOAuthScopedRoutes,
  12. Children: children,
  13. }
  14. }
  15. func GetProjectOAuthScopedRoutes(
  16. r chi.Router,
  17. config *config.Config,
  18. basePath *types.Path,
  19. factory shared.APIEndpointFactory,
  20. children ...*Registerer,
  21. ) []*Route {
  22. routes, projPath := getProjectOAuthRoutes(r, config, basePath, factory)
  23. if len(children) > 0 {
  24. r.Route(projPath.RelativePath, func(r chi.Router) {
  25. for _, child := range children {
  26. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  27. routes = append(routes, childRoutes...)
  28. }
  29. })
  30. }
  31. return routes
  32. }
  33. func getProjectOAuthRoutes(
  34. r chi.Router,
  35. config *config.Config,
  36. basePath *types.Path,
  37. factory shared.APIEndpointFactory,
  38. ) ([]*Route, *types.Path) {
  39. relPath := "/oauth"
  40. newPath := &types.Path{
  41. Parent: basePath,
  42. RelativePath: relPath,
  43. }
  44. routes := make([]*Route, 0)
  45. // GET /api/projects/{project_id}/oauth/slack -> project_integration.NewProjectOAuthSlackHandler
  46. slackEndpoint := factory.NewAPIEndpoint(
  47. &types.APIRequestMetadata{
  48. Verb: types.APIVerbGet,
  49. Method: types.HTTPVerbGet,
  50. Path: &types.Path{
  51. Parent: basePath,
  52. RelativePath: relPath + "/slack",
  53. },
  54. Scopes: []types.PermissionScope{
  55. types.UserScope,
  56. types.ProjectScope,
  57. },
  58. },
  59. )
  60. slackHandler := project_oauth.NewProjectOAuthSlackHandler(
  61. config,
  62. factory.GetDecoderValidator(),
  63. factory.GetResultWriter(),
  64. )
  65. routes = append(routes, &Route{
  66. Endpoint: slackEndpoint,
  67. Handler: slackHandler,
  68. Router: r,
  69. })
  70. // GET /api/projects/{project_id}/oauth/digitalocean -> project_integration.NewProjectOAuthDOHandler
  71. doEndpoint := factory.NewAPIEndpoint(
  72. &types.APIRequestMetadata{
  73. Verb: types.APIVerbGet,
  74. Method: types.HTTPVerbGet,
  75. Path: &types.Path{
  76. Parent: basePath,
  77. RelativePath: relPath + "/digitalocean",
  78. },
  79. Scopes: []types.PermissionScope{
  80. types.UserScope,
  81. types.ProjectScope,
  82. },
  83. },
  84. )
  85. doHandler := project_oauth.NewProjectOAuthDOHandler(
  86. config,
  87. factory.GetDecoderValidator(),
  88. factory.GetResultWriter(),
  89. )
  90. routes = append(routes, &Route{
  91. Endpoint: doEndpoint,
  92. Handler: doHandler,
  93. Router: r,
  94. })
  95. return routes, newPath
  96. }