Skip to content

Commit cf6cd4c

Browse files
alexlyplukebp
andauthored
politeiawww: Add login to auth router. (#1482)
This diff adds the login route to the auth router so that it is CSRF protected. Co-authored-by: lukebp <lukebp@users.noreply.github.com>
1 parent a060068 commit cf6cd4c

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

politeiawww/userwww.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,6 @@ func (p *politeiawww) setUserWWWRoutes() {
740740
p.addRoute(http.MethodPost, www.PoliteiaWWWAPIRoute,
741741
www.RouteResendVerification, p.handleResendVerification,
742742
permissionPublic)
743-
p.addRoute(http.MethodPost, www.PoliteiaWWWAPIRoute,
744-
www.RouteLogin, p.handleLogin,
745-
permissionPublic)
746743
p.addRoute(http.MethodPost, www.PoliteiaWWWAPIRoute,
747744
www.RouteLogout, p.handleLogout,
748745
permissionPublic)
@@ -759,6 +756,10 @@ func (p *politeiawww) setUserWWWRoutes() {
759756
www.RouteUsers, p.handleUsers,
760757
permissionPublic)
761758

759+
// Setup the login route.
760+
p.addLoginRoute(http.MethodPost, www.PoliteiaWWWAPIRoute,
761+
www.RouteLogin, p.handleLogin)
762+
762763
// Routes that require being logged in.
763764
p.addRoute(http.MethodPost, www.PoliteiaWWWAPIRoute,
764765
www.RouteSecret, p.handleSecret,
@@ -803,9 +804,6 @@ func (p *politeiawww) setUserWWWRoutes() {
803804
// setCMSUserWWWRoutes setsup the user routes for cms mode
804805
func (p *politeiawww) setCMSUserWWWRoutes() {
805806
// Public routes
806-
p.addRoute(http.MethodPost, www.PoliteiaWWWAPIRoute,
807-
www.RouteLogin, p.handleLogin,
808-
permissionPublic)
809807
p.addRoute(http.MethodPost, www.PoliteiaWWWAPIRoute,
810808
www.RouteLogout, p.handleLogout,
811809
permissionPublic)
@@ -819,6 +817,10 @@ func (p *politeiawww) setCMSUserWWWRoutes() {
819817
cms.RouteRegisterUser, p.handleRegisterUser,
820818
permissionPublic)
821819

820+
// Setup the login route.
821+
p.addLoginRoute(http.MethodPost, www.PoliteiaWWWAPIRoute,
822+
www.RouteLogin, p.handleLogin)
823+
822824
// Routes that require being logged in.
823825
p.addRoute(http.MethodPost, www.PoliteiaWWWAPIRoute,
824826
www.RouteSecret, p.handleSecret,

politeiawww/www.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,13 @@ func RespondWithError(w http.ResponseWriter, r *http.Request, userHttpCode int,
187187
// specified it adds a websocket. The routeVersion should be in the format
188188
// "/v1".
189189
func (p *politeiawww) addRoute(method string, routeVersion string, route string, handler http.HandlerFunc, perm permission) {
190-
fullRoute := routeVersion + route
190+
// Sanity check. The login route is special. It must be registered
191+
// using the addLoginRoute() function.
192+
if strings.Contains(route, "login") {
193+
panic("you cannot use this function to register the login route")
194+
}
191195

196+
fullRoute := routeVersion + route
192197
switch perm {
193198
case permissionAdmin:
194199
handler = p.isLoggedInAsAdmin(handler)
@@ -213,6 +218,20 @@ func (p *politeiawww) addRoute(method string, routeVersion string, route string,
213218
}
214219
}
215220

221+
// addLoginRoute sets up a handler for the login route. The login route is
222+
// special. It is the only public route that requires CSRF protection, so we
223+
// use a separate function to register it.
224+
func (p *politeiawww) addLoginRoute(method string, routeVersion string, route string, handler http.HandlerFunc) {
225+
// Sanity check
226+
if !strings.Contains(route, "login") {
227+
panic("you cannot use this function to register non login routes")
228+
}
229+
230+
// Add login route to the auth router
231+
fullRoute := routeVersion + route
232+
p.auth.StrictSlash(true).HandleFunc(fullRoute, handler).Methods(method)
233+
}
234+
216235
// makeRequest makes an http request to the method and route provided,
217236
// serializing the provided object as the request body.
218237
//

0 commit comments

Comments
 (0)