project_oauth.go 3.3 KB

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