Skip to content

Commit

Permalink
fixup! console: Remove echo from console
Browse files Browse the repository at this point in the history
  • Loading branch information
pgalic96 committed Nov 26, 2021
1 parent 32f042d commit c869669
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/account/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *server) RegisterRoutes(server *web.Server) {
csrf.FieldName("_csrf"),
csrf.Path("/"),
)
router := server.Router().NewRoute().Path(s.config.Mount).Subrouter()
router := server.Router().NewRoute().PathPrefix(s.config.Mount).Subrouter()
router.Use(
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
19 changes: 9 additions & 10 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func templateMiddleware(console *Console) webmiddleware.MiddlewareFunc {

// RegisterRoutes implements web.Registerer. It registers the Console to the web server.
func (console *Console) RegisterRoutes(server *web.Server) {
consoleRouter := server.Router().NewRoute().Subrouter()
consoleRouter.Use(
router := server.Router().PathPrefix(console.config.Mount).Subrouter()
router.Use(
mux.MiddlewareFunc(cspHeader(console)),
mux.MiddlewareFunc(templateMiddleware(console)),
mux.MiddlewareFunc(
Expand All @@ -170,12 +170,11 @@ func (console *Console) RegisterRoutes(server *web.Server) {
),
),
)
server.Router().PathPrefix(console.config.Mount).Handler(consoleRouter)
oauthRouter := consoleRouter.NewRoute().Subrouter()
oauthRouter.Path("/token").HandlerFunc(console.oc.HandleToken).Methods(http.MethodGet)
oauthRouter.Path("/logout").HandlerFunc(console.oc.HandleLogout).Methods(http.MethodPost)
oauthRouter.Path("/oauth/callback").HandlerFunc(console.oc.HandleCallback).Methods(http.MethodGet)
oauthRouter.Path("/login/ttn-stack").HandlerFunc(console.oc.HandleLogin).Methods(http.MethodGet)
oauthRouter.PathPrefix("/").HandlerFunc(webui.Template.Render).Methods(http.MethodGet)
consoleRouter.PathPrefix("/api/auth").Handler(oauthRouter)
api := router.NewRoute().PathPrefix("/api/auth/").Subrouter()
api.Path("/token").HandlerFunc(console.oc.HandleToken).Methods(http.MethodGet)
api.Path("/logout").HandlerFunc(console.oc.HandleLogout).Methods(http.MethodPost)

router.Path("/login/ttn-stack").HandlerFunc(console.oc.HandleLogin).Methods(http.MethodGet)
router.Path("/oauth/callback").HandlerFunc(console.oc.HandleCallback).Methods(http.MethodGet)
router.NewRoute().HandlerFunc(webui.Template.Render)
}
3 changes: 2 additions & 1 deletion pkg/web/oauthclient/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (oc *OAuthClient) HandleCallback(w http.ResponseWriter, r *http.Request) {
var response oauthAuthorizeResponse
err := json.NewDecoder(r.Body).Decode(&response)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
webhandlers.Error(w, r, err)
return
}
if err := response.ValidateContext(r.Context()); err != nil {
Expand All @@ -81,6 +81,7 @@ func (oc *OAuthClient) HandleCallback(w http.ResponseWriter, r *http.Request) {
if value.AccessToken != "" {
config := oc.configFromContext(r.Context())
http.Redirect(w, r, config.RootURL, http.StatusFound)
return
}
webhandlers.Error(w, r, err)
return
Expand Down
1 change: 0 additions & 1 deletion pkg/web/oauthclient/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
// endpoint.
func (oc *OAuthClient) HandleLogin(w http.ResponseWriter, r *http.Request) {
next := r.URL.Query().Get(oc.nextKey)

// Only allow relative paths.
if !strings.HasPrefix(next, "/") && !strings.HasPrefix(next, "#") && !strings.HasPrefix(next, "?") {
next = ""
Expand Down
1 change: 0 additions & 1 deletion pkg/web/oauthclient/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func (oc *OAuthClient) HandleLogout(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(struct {
OpLogoutURI string `json:"op_logout_uri"`
}{
Expand Down
1 change: 0 additions & 1 deletion pkg/web/oauthclient/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func (oc *OAuthClient) HandleToken(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")

if err := enc.Encode(struct {
AccessToken string `json:"access_token"`
Expand Down

0 comments on commit c869669

Please sign in to comment.