Skip to content

Commit

Permalink
Avoid making Temporal API calls if unauthenticated (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
feedmeapples authored Feb 28, 2022
1 parent 9804b43 commit fc5178e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions server/routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func SetAPIRoutes(e *echo.Echo, cfgProvider *config.ConfigProviderWithRefresh) e

func temporalAPIHandler(cfgProvider *config.ConfigProviderWithRefresh) echo.HandlerFunc {
return func(c echo.Context) error {
err := validateAuth(c, cfgProvider)
if err != nil {
return err
}

cfg, err := cfgProvider.GetConfig()
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
Expand Down Expand Up @@ -165,3 +170,27 @@ func withAuth(c echo.Context) runtime.ServeMuxOption {
},
)
}

func validateAuth(c echo.Context, cfgProvider *config.ConfigProviderWithRefresh) error {
cfg, err := cfgProvider.GetConfig()
if err != nil {
return err
}

isEnabled := cfg.Auth.Enabled
if !isEnabled {
return nil
}

sess, _ := session.Get("auth", c)
if sess == nil {
return echo.NewHTTPError(http.StatusUnauthorized, "unauthorized")
}

token := sess.Values["access-token"]
if token == nil {
return echo.NewHTTPError(http.StatusUnauthorized, "unauthorized")
}

return nil
}
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var uiHTML []byte
// https://github.com/sveltejs/kit/pull/1370#issuecomment-853306453
//go:embed generated/ui/*
//go:embed generated/ui/_app/assets/pages/*
//go:embed generated/ui/_app/assets/_*
//go:embed generated/ui/_app/chunks/*
//go:embed generated/ui/_app/pages/*
//go:embed generated/ui/_app/assets/pages/namespaces/\[namespace\]/workflows/\[workflow\]/\[run\]/history/compact/activity-\[eventId\]/*
Expand Down

0 comments on commit fc5178e

Please sign in to comment.