Skip to content

Commit

Permalink
Add build tag-based conditional rendering and endpoint access
Browse files Browse the repository at this point in the history
In this update, build tags have been added to enable or disable certain endpoint accesses and UI rendering based on the build environment. Specifically, the "dev" build tag determines whether the device login button is displayed on the login page, and the "register2fa" build tag configures whether the 2FA endpoints are accessible. Making these features conditional improves the flexibility of customization based on the application's deployment environment.

Signed-off-by: Christian Roessner <[email protected]>
  • Loading branch information
Christian Roessner committed May 6, 2024
1 parent f54a1ef commit 03b30cc
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
29 changes: 17 additions & 12 deletions server/core/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/croessner/nauthilus/server/lualib"
"github.com/croessner/nauthilus/server/rediscli"
"github.com/croessner/nauthilus/server/stats"
"github.com/croessner/nauthilus/server/tags"
"github.com/croessner/nauthilus/server/util"
"github.com/gin-contrib/pprof"
"github.com/gin-contrib/sessions"
Expand Down Expand Up @@ -552,15 +553,17 @@ func setupHydraEndpoints(router *gin.Engine, store sessions.Store) {
//
// setup2FAEndpoints(router, sessionStore)
func setup2FAEndpoints(router *gin.Engine, sessionStore sessions.Store) {
group := router.Group(global.TwoFAv1Root)
if tags.Register2FA {
group := router.Group(global.TwoFAv1Root)

// This page handles the user login request to do a two-factor authentication
twoFactorGroup := routerGroup(viper.GetString("login_2fa_page"), group, sessionStore, loginGET2FAHandler, loginPOST2FAHandler)
twoFactorGroup.GET("/home", register2FAHomeHandler)
twoFactorGroup.GET("/home/:languageTag", register2FAHomeHandler)
// This page handles the user login request to do a two-factor authentication
twoFactorGroup := routerGroup(viper.GetString("login_2fa_page"), group, sessionStore, loginGET2FAHandler, loginPOST2FAHandler)
twoFactorGroup.GET("/home", register2FAHomeHandler)
twoFactorGroup.GET("/home/:languageTag", register2FAHomeHandler)

// This page handles the TOTP registration
routerGroup(viper.GetString("totp_page"), group, sessionStore, registerTotpGETHandler, registerTotpPOSTHandler)
// This page handles the TOTP registration
routerGroup(viper.GetString("totp_page"), group, sessionStore, registerTotpGETHandler, registerTotpPOSTHandler)
}
}

// setupStaticContent is a function that sets up the static content endpoints in the given Gin router.
Expand Down Expand Up @@ -632,12 +635,14 @@ func setupBackChannelEndpoints(router *gin.Engine) {
// - A GET endpoint at the path "/register/begin" which is handled by the beginRegistration function.
// - A POST endpoint at the path "/register/finish" which is handled by the finishRegistration function.
func setupWebAuthnEndpoints(router *gin.Engine, sessionStore sessions.Store) {
group := router.Group(global.TwoFAv1Root)
if tags.IsDevelopment {
group := router.Group(global.TwoFAv1Root)

regGroup := group.Group(viper.GetString("webauthn_page"))
regGroup.Use(sessions.Sessions(global.SessionName, sessionStore))
regGroup.GET("/register/begin", beginRegistration)
regGroup.POST("/register/finish", finishRegistration)
regGroup := group.Group(viper.GetString("webauthn_page"))
regGroup.Use(sessions.Sessions(global.SessionName, sessionStore))
regGroup.GET("/register/begin", beginRegistration)
regGroup.POST("/register/finish", finishRegistration)
}
}

// waitForShutdown is a function that waits for the context to be done, then shuts down the provided http.Server.
Expand Down
5 changes: 5 additions & 0 deletions server/core/hydra.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
errors2 "github.com/croessner/nauthilus/server/errors"
"github.com/croessner/nauthilus/server/global"
"github.com/croessner/nauthilus/server/logging"
"github.com/croessner/nauthilus/server/tags"
"github.com/croessner/nauthilus/server/util"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -53,6 +54,9 @@ type Language struct {
}

type LoginPageData struct {
// InDevelopment is a flag that is true, if the build-tag dev is used.
InDevelopment bool

// Determines if the Welcome message should be displayed
WantWelcome bool

Expand Down Expand Up @@ -1051,6 +1055,7 @@ func (a *ApiConfig) handleLoginNoSkip() {
LanguagePassive: languagePassive,
CSRFToken: a.csrfToken,
LoginChallenge: a.challenge,
InDevelopment: tags.IsDevelopment,
}

a.ctx.HTML(http.StatusOK, "login.html", loginData)
Expand Down
5 changes: 5 additions & 0 deletions server/tags/dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build dev

package tags

const IsDevelopment = true
5 changes: 5 additions & 0 deletions server/tags/disableregistration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !register2fa

package tags

const Register2FA = false
5 changes: 5 additions & 0 deletions server/tags/enableregistration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build register2fa

package tags

const Register2FA = true
5 changes: 5 additions & 0 deletions server/tags/prod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !dev

package tags

const IsDevelopment = false
4 changes: 4 additions & 0 deletions static/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ <h2 class="center">{{ .ApplicationName }}</h2>
<button type="submit" id="submit" name="submit" value="{{ .Submit }}" data-loginurl="{{ .PostLoginEndpoint }}/post">
{{ .Submit }}
</button>
{{ if .InDevelopment }}
<p class="text center vs-5">{{ .Or }}</p>
<div class="vs-15"></div>
<button class="device" type="submit" id="device" name="device" value="{{ .Device }}" data-deviceurl="{{ .DeviceLoginEndpoint }}">
{{ .Device }}
</button>
{{ else }}
<div id="device" style="display: none;"></div>
{{ end }}
<div class="vs-5"></div>
<input type="checkbox" id="remember" name="remember" value="on"/>
<label for="remember">{{ .Remember }}</label>
Expand Down

0 comments on commit 03b30cc

Please sign in to comment.