MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / RegisterRoutes

Method RegisterRoutes

pkg/account/server.go:100–151  ·  view source on GitHub ↗
(server *web.Server)

Source from the content-addressed store, hash-verified

98}
99
100func (s *server) RegisterRoutes(server *web.Server) {
101 csrfMiddleware := webmiddleware.CSRF(
102 s.config.CSRFAuthKey,
103 csrf.CookieName("_csrf"),
104 csrf.FieldName("_csrf"),
105 csrf.Path("/"),
106 )
107 router := server.PrefixWithRedirect(s.config.Mount).Subrouter()
108 router.Use(
109 func(next http.Handler) http.Handler {
110 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111 r, nonce := webui.WithNonce(r)
112 cspString := s.generateCSP(s.configFromContext(r.Context()), nonce)
113 w.Header().Set("Content-Security-Policy", cspString)
114 next.ServeHTTP(w, r)
115 })
116 },
117 ratelimit.HTTPMiddleware(s.c.RateLimiter(), "http:account"),
118 func(next http.Handler) http.Handler {
119 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
120 config := s.configFromContext(r.Context())
121 r = webui.WithTemplateData(r, config.UI.TemplateData)
122 frontendConfig := config.UI.FrontendConfig
123 frontendConfig.Language = config.UI.TemplateData.Language
124 r = webui.WithAppConfig(r, struct {
125 oauth.FrontendConfig
126 }{
127 FrontendConfig: frontendConfig,
128 })
129 next.ServeHTTP(w, r)
130 })
131 },
132 webhandlers.WithErrorHandlers(map[string]http.Handler{
133 "text/html": webui.Template,
134 }),
135 mux.MiddlewareFunc(csrfMiddleware),
136 )
137
138 logoutHandler := s.requireLogin(http.HandlerFunc(s.Logout))
139 currentUserHandler := s.requireLogin(http.HandlerFunc(s.CurrentUser))
140 api := router.NewRoute().PathPrefix("/api").Subrouter()
141 api.Path("/auth/login").HandlerFunc(s.Login).Methods(http.MethodPost)
142 api.Path("/auth/token-login").HandlerFunc(s.TokenLogin).Methods(http.MethodPost)
143 api.Path("/auth/logout").Handler(logoutHandler).Methods(http.MethodPost)
144 api.Path("/me").Handler(currentUserHandler).Methods(http.MethodGet)
145
146 loginHandler := s.redirectToNext(webui.Template)
147 page := router.NewRoute().Subrouter()
148 page.Path("/login").Handler(loginHandler).Methods(http.MethodGet)
149 page.Path("/token-login").Handler(loginHandler).Methods(http.MethodGet)
150 page.NewRoute().Handler(webui.Template)
151}

Callers

nothing calls this directly

Calls 15

configFromContextMethod · 0.95
requireLoginMethod · 0.95
redirectToNextMethod · 0.95
CSRFFunction · 0.92
WithNonceFunction · 0.92
HTTPMiddlewareFunction · 0.92
WithTemplateDataFunction · 0.92
WithAppConfigFunction · 0.92
WithErrorHandlersFunction · 0.92
PrefixWithRedirectMethod · 0.80
HeaderMethod · 0.80
HandlerMethod · 0.80

Tested by

no test coverage detected