Skip to content

Commit

Permalink
Move Register/Login/Logout (harness#2172)
Browse files Browse the repository at this point in the history
* move register/login/logout, git resources, and system check APIs outside of auth
  • Loading branch information
johannesHarness authored and Harness committed Jul 8, 2024
1 parent c6f6d83 commit 28b55f0
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions app/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,22 @@ func NewAPIHandler(
// configure cors middleware
r.Use(corsHandler(config))

// for now always attempt auth - enforced per operation.
r.Use(middlewareauthn.Attempt(authenticator))

r.Use(audit.Middleware())

r.Route("/v1", func(r chi.Router) {
setupRoutesV1(r, appCtx, config, repoCtrl, repoSettingsCtrl, executionCtrl, triggerCtrl, logCtrl, pipelineCtrl,
connectorCtrl, templateCtrl, pluginCtrl, secretCtrl, spaceCtrl, pullreqCtrl,
webhookCtrl, githookCtrl, git, saCtrl, userCtrl, principalCtrl, checkCtrl, sysCtrl, uploadCtrl,
searchCtrl, gitspaceCtrl, migrateCtrl)
// special methods that don't require authentication
setupAccountWithoutAuth(r, userCtrl, sysCtrl, config)
setupSystem(r, config, sysCtrl)
setupResources(r)

r.Group(func(r chi.Router) {
r.Use(middlewareauthn.Attempt(authenticator))

setupRoutesV1WithAuth(r, appCtx, config, repoCtrl, repoSettingsCtrl, executionCtrl, triggerCtrl, logCtrl,
pipelineCtrl, connectorCtrl, templateCtrl, pluginCtrl, secretCtrl, spaceCtrl, pullreqCtrl,
webhookCtrl, githookCtrl, git, saCtrl, userCtrl, principalCtrl, checkCtrl, uploadCtrl,
searchCtrl, gitspaceCtrl, migrateCtrl)
})
})

// wrap router in terminatedPath encoder.
Expand All @@ -175,7 +181,7 @@ func corsHandler(config *types.Config) func(http.Handler) http.Handler {
}

// nolint: revive // it's the app context, it shouldn't be the first argument
func setupRoutesV1(r chi.Router,
func setupRoutesV1WithAuth(r chi.Router,
appCtx context.Context,
config *types.Config,
repoCtrl *repo.Controller,
Expand All @@ -197,12 +203,12 @@ func setupRoutesV1(r chi.Router,
userCtrl *user.Controller,
principalCtrl principal.Controller,
checkCtrl *check.Controller,
sysCtrl *system.Controller,
uploadCtrl *upload.Controller,
searchCtrl *keywordsearch.Controller,
gitspaceCtrl *gitspace.Controller,
migrateCtrl *migrate.Controller,
) {
setupAccountWithAuth(r, userCtrl, config)
setupSpaces(r, appCtx, spaceCtrl)
setupRepos(r, repoCtrl, repoSettingsCtrl, pipelineCtrl, executionCtrl, triggerCtrl,
logCtrl, pullreqCtrl, webhookCtrl, checkCtrl, uploadCtrl)
Expand All @@ -214,9 +220,6 @@ func setupRoutesV1(r chi.Router,
setupPrincipals(r, principalCtrl)
setupInternal(r, githookCtrl, git)
setupAdmin(r, userCtrl)
setupAccount(r, userCtrl, sysCtrl, config)
setupSystem(r, config, sysCtrl)
setupResources(r)
setupPlugins(r, pluginCtrl)
setupKeywordSearch(r, searchCtrl)
setupGitspaces(r, gitspaceCtrl)
Expand Down Expand Up @@ -729,10 +732,19 @@ func setupAdmin(r chi.Router, userCtrl *user.Controller) {
})
}

func setupAccount(r chi.Router, userCtrl *user.Controller, sysCtrl *system.Controller, config *types.Config) {
func setupAccountWithoutAuth(
r chi.Router,
userCtrl *user.Controller,
sysCtrl *system.Controller,
config *types.Config,
) {
cookieName := config.Token.CookieName
r.Post("/login", account.HandleLogin(userCtrl, cookieName))
r.Post("/register", account.HandleRegister(userCtrl, sysCtrl, cookieName))
}

func setupAccountWithAuth(r chi.Router, userCtrl *user.Controller, config *types.Config) {
cookieName := config.Token.CookieName
r.Post("/logout", account.HandleLogout(userCtrl, cookieName))
}

Expand Down

0 comments on commit 28b55f0

Please sign in to comment.