Skip to content

Commit

Permalink
Support OAuth SubPath Ingress
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <[email protected]>
  • Loading branch information
Frank Jogeleit committed Apr 12, 2024
1 parent fc6af3d commit 538cb9c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
82 changes: 42 additions & 40 deletions backend/pkg/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,58 @@ func Provider(provider string) gin.HandlerFunc {
}
}

func Valid(ctx *gin.Context) {
providerName, err := gothic.GetProviderName(ctx.Request)
if err != nil {
zap.L().Error("failed to get provider name", zap.Error(err))
ctx.AbortWithError(http.StatusPreconditionFailed, errors.New("provider name not avaialable in request"))
return
}
func Valid(basePath string) gin.HandlerFunc {
return func(ctx *gin.Context) {
providerName, err := gothic.GetProviderName(ctx.Request)
if err != nil {
zap.L().Error("failed to get provider name", zap.Error(err))
ctx.AbortWithError(http.StatusPreconditionFailed, errors.New("provider name not avaialable in request"))
return
}

provider, err := goth.GetProvider(providerName)
if err != nil {
zap.L().Error("failed to get requested provider", zap.Error(err))
ctx.AbortWithError(http.StatusPreconditionFailed, errors.New("provider not available"))
return
}
provider, err := goth.GetProvider(providerName)
if err != nil {
zap.L().Error("failed to get requested provider", zap.Error(err))
ctx.AbortWithError(http.StatusPreconditionFailed, errors.New("provider not available"))
return
}

profile := ProfileFrom(ctx)
if profile == nil {
zap.L().Error("profile not found", zap.Error(err))
profile := ProfileFrom(ctx)
if profile == nil {
zap.L().Error("profile not found", zap.Error(err))

logout(ctx)
ctx.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
logout(ctx)
ctx.Redirect(http.StatusTemporaryRedirect, basePath+"login")
return
}

session := sessions.Default(ctx)
session := sessions.Default(ctx)

sess := ProviderSession(providerName, profile)
if sess == nil {
zap.L().Error("could not create session from profile", zap.Error(err))
sess := ProviderSession(providerName, profile)
if sess == nil {
zap.L().Error("could not create session from profile", zap.Error(err))

logout(ctx)
ctx.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
logout(ctx)
ctx.Redirect(http.StatusTemporaryRedirect, basePath+"login")
return
}

user, err := provider.FetchUser(sess)
if err != nil {
zap.L().Error("failed to validate session", zap.Error(err))
user, err := provider.FetchUser(sess)
if err != nil {
zap.L().Error("failed to validate session", zap.Error(err))

logout(ctx)
ctx.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
logout(ctx)
ctx.Redirect(http.StatusTemporaryRedirect, basePath+"login")
return
}

session.Set("profile", NewProfile(user))
if err := session.Save(); err != nil {
zap.L().Error("failed to save profile session", zap.Error(err))
}
session.Set("profile", NewProfile(user))
if err := session.Save(); err != nil {
zap.L().Error("failed to save profile session", zap.Error(err))
}

ctx.Next()
ctx.Next()
}
}

func Auth(basePath string) gin.HandlerFunc {
Expand Down
8 changes: 8 additions & 0 deletions backend/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,11 @@ type Config struct {
func (c *Config) AuthEnabled() bool {
return c.OAuth.Enabled || c.OpenIDConnect.Enabled
}

func (c *Config) AuthBasePath() string {
if c.OAuth.Enabled {
return c.OAuth.BasePath()
}

return c.OpenIDConnect.BasePath()
}
2 changes: 1 addition & 1 deletion backend/pkg/config/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (r *Resolver) Server(ctx context.Context) (*server.Server, error) {
if !r.config.UI.Disabled {
var uiMiddleware []gin.HandlerFunc
if r.config.AuthEnabled() {
uiMiddleware = append(uiMiddleware, auth.Valid)
uiMiddleware = append(uiMiddleware, auth.Valid(r.config.AuthBasePath()))
}

zap.L().Info("register UI", zap.String("path", r.config.UI.Path))
Expand Down
2 changes: 1 addition & 1 deletion frontend/modules/core/components/UserMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
const logout = () => {
document.cookie.split(";").forEach((c) => { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });
// @ts-ignore
window.location = '/logout'
window.location = `${window.location.pathname || '/'}logout`
}
const { data: profile } = useAPI(api => api.profile())
Expand Down

0 comments on commit 538cb9c

Please sign in to comment.