Skip to content

Commit ec738fa

Browse files
Add nginx auth_request support (#109)
* Introduce AuthVerifyRequireLogin --------- Co-authored-by: Geoff Bourne <[email protected]>
1 parent f62fc4d commit ec738fa

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

server/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Config struct {
2525
AllowIdpInitiated bool `usage:"If set, allows for IdP initiated authentication flow"`
2626
AuthVerify bool `usage:"Enables verify path endpoint for forward auth and trusts X-Forwarded headers"`
2727
AuthVerifyPath string `default:"/_verify" usage:"Path under BaseUrl that will respond with a 200 when authenticated"`
28+
AuthVerifyRequireLogin bool `usage:"If set, trigger a login if the user is not authenticated during verify"`
2829
Debug bool `usage:"Enable debug logs"`
2930
StaticRelayState string `usage:"A fixed RelayState value, such as a short URL. Will be trimmed to 80 characters to conform with SAML. The default generates random bytes that are Base64 encoded."`
3031
InitiateSessionPath string `usage:"If set, initiates a SAML authentication flow only when a user visits this path. This will allow anonymous users to access to the backend."`

server/server.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ func Start(ctx context.Context, listener net.Listener, logger *zap.Logger, cfg *
124124

125125
app := http.HandlerFunc(proxy.handler)
126126
if cfg.AuthVerify {
127-
http.Handle(cfg.AuthVerifyPath, middleware.RequireAccount(http.HandlerFunc(noContentHandler)))
127+
if cfg.AuthVerifyRequireLogin {
128+
http.Handle(cfg.AuthVerifyPath, middleware.RequireAccount(http.HandlerFunc(noContentHandler)))
129+
} else {
130+
http.Handle(cfg.AuthVerifyPath, authVerify(middleware))
131+
}
128132
}
129133

130134
http.Handle("/saml/sign_in", http.HandlerFunc(middleware.HandleStartAuthFlow))
@@ -188,6 +192,25 @@ func setupHttpClient(idpCaFile string) (*http.Client, error) {
188192
}
189193

190194
// HTTP handler that replies to each request with a “204 no content”.
191-
func noContentHandler(w http.ResponseWriter, r *http.Request) {
195+
func noContentHandler(w http.ResponseWriter, _ *http.Request) {
192196
w.WriteHeader(http.StatusNoContent)
193197
}
198+
199+
func authVerify(middleware *samlsp.Middleware) http.Handler {
200+
201+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
202+
203+
session, err := middleware.Session.GetSession(r)
204+
205+
if session != nil {
206+
w.WriteHeader(http.StatusNoContent)
207+
return
208+
}
209+
210+
if err == samlsp.ErrNoSession {
211+
w.WriteHeader(http.StatusUnauthorized)
212+
return
213+
}
214+
215+
})
216+
}

0 commit comments

Comments
 (0)