Skip to content

Commit 62ffd2f

Browse files
committed
feat: finalize context functionality
1 parent a3ec072 commit 62ffd2f

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

internal/bootstrap/router_bootstrap.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ func (app *BootstrapApp) setupRouter() (*gin.Engine, error) {
3030
}
3131

3232
contextMiddleware := middleware.NewContextMiddleware(middleware.ContextMiddlewareConfig{
33-
CookieDomain: app.context.cookieDomain,
33+
CookieDomain: app.context.cookieDomain,
34+
SessionCookieName: app.context.sessionCookieName,
3435
}, app.services.authService, app.services.oauthBrokerService)
3536

3637
err := contextMiddleware.Init()
@@ -98,7 +99,8 @@ func (app *BootstrapApp) setupRouter() (*gin.Engine, error) {
9899
proxyController.SetupRoutes()
99100

100101
userController := controller.NewUserController(controller.UserControllerConfig{
101-
CookieDomain: app.context.cookieDomain,
102+
CookieDomain: app.context.cookieDomain,
103+
SessionCookieName: app.context.sessionCookieName,
102104
}, apiRouter, app.services.authService)
103105

104106
userController.SetupRoutes()

internal/controller/proxy_controller.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,15 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
9999
return
100100
}
101101

102+
if acls == nil {
103+
acls = &model.App{}
104+
}
105+
102106
tlog.App.Trace().Interface("acls", acls).Msg("ACLs for resource")
103107

104108
clientIP := c.ClientIP()
105109

106-
if controller.auth.IsBypassedIP(acls.IP, clientIP) {
110+
if controller.auth.IsBypassedIP(&acls.IP, clientIP) {
107111
controller.setHeaders(c, *acls)
108112
c.JSON(200, gin.H{
109113
"status": 200,
@@ -112,7 +116,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
112116
return
113117
}
114118

115-
authEnabled, err := controller.auth.IsAuthEnabled(proxyCtx.Path, acls.Path)
119+
authEnabled, err := controller.auth.IsAuthEnabled(proxyCtx.Path, &acls.Path)
116120

117121
if err != nil {
118122
tlog.App.Error().Err(err).Msg("Failed to check if auth is enabled for resource")
@@ -130,7 +134,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
130134
return
131135
}
132136

133-
if !controller.auth.CheckIP(acls.IP, clientIP) {
137+
if !controller.auth.CheckIP(&acls.IP, clientIP) {
134138
queries, err := query.Values(UnauthorizedQuery{
135139
Resource: strings.Split(proxyCtx.Host, ".")[0],
136140
IP: clientIP,
@@ -169,7 +173,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
169173
tlog.App.Trace().Interface("context", userContext).Msg("User context from request")
170174

171175
if userContext.Authenticated {
172-
userAllowed := controller.auth.IsUserAllowed(c, *userContext, *acls)
176+
userAllowed := controller.auth.IsUserAllowed(c, *userContext, acls)
173177

174178
if !userAllowed {
175179
tlog.App.Warn().Str("user", userContext.GetUsername()).Str("resource", strings.Split(proxyCtx.Host, ".")[0]).Msg("User not allowed to access resource")

internal/middleware/context_middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
8080
http.SetCookie(c.Writer, cookie)
8181
}
8282

83+
tlog.App.Trace().Msgf("Authenticated user from session cookie: %s", userContext.GetUsername())
8384
c.Set("context", userContext)
8485
c.Next()
8586
return

internal/service/auth_service.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (auth *AuthService) RefreshSession(ctx context.Context, uuid string) (*http
346346
}
347347

348348
if session.Expiry-currentTime > refreshThreshold {
349-
return nil, fmt.Errorf("session not eligible for refresh yet")
349+
return nil, nil
350350
}
351351

352352
newExpiry := session.Expiry + refreshThreshold
@@ -443,7 +443,11 @@ func (auth *AuthService) LDAPAuthConfigured() bool {
443443
return auth.ldap.IsConfigured()
444444
}
445445

446-
func (auth *AuthService) IsUserAllowed(c *gin.Context, context model.UserContext, acls model.App) bool {
446+
func (auth *AuthService) IsUserAllowed(c *gin.Context, context model.UserContext, acls *model.App) bool {
447+
if acls == nil {
448+
return true
449+
}
450+
447451
if context.Provider == model.ProviderOAuth {
448452
tlog.App.Debug().Msg("Checking OAuth whitelist")
449453
return utils.CheckFilter(acls.OAuth.Whitelist, context.OAuth.Email)
@@ -507,7 +511,11 @@ func (auth *AuthService) IsInLDAPGroup(c *gin.Context, context model.UserContext
507511
return false
508512
}
509513

510-
func (auth *AuthService) IsAuthEnabled(uri string, path model.AppPath) (bool, error) {
514+
func (auth *AuthService) IsAuthEnabled(uri string, path *model.AppPath) (bool, error) {
515+
if path == nil {
516+
return true, nil
517+
}
518+
511519
// Check for block list
512520
if path.Block != "" {
513521
regex, err := regexp.Compile(path.Block)
@@ -552,7 +560,11 @@ func (auth *AuthService) GetBasicAuth(req *http.Request) (*model.LocalUser, erro
552560
}, nil
553561
}
554562

555-
func (auth *AuthService) CheckIP(acls model.AppIP, ip string) bool {
563+
func (auth *AuthService) CheckIP(acls *model.AppIP, ip string) bool {
564+
if acls == nil {
565+
acls = &model.AppIP{}
566+
}
567+
556568
// Merge the global and app IP filter
557569
blockedIps := append(auth.config.IP.Block, acls.Block...)
558570
allowedIPs := append(auth.config.IP.Allow, acls.Allow...)
@@ -590,7 +602,11 @@ func (auth *AuthService) CheckIP(acls model.AppIP, ip string) bool {
590602
return true
591603
}
592604

593-
func (auth *AuthService) IsBypassedIP(acls model.AppIP, ip string) bool {
605+
func (auth *AuthService) IsBypassedIP(acls *model.AppIP, ip string) bool {
606+
if acls == nil {
607+
return false
608+
}
609+
594610
for _, bypassed := range acls.Bypass {
595611
res, err := utils.FilterIP(bypassed, ip)
596612
if err != nil {

0 commit comments

Comments
 (0)