From daad2abc33b2decb999fe9e2391da38c9206292f Mon Sep 17 00:00:00 2001 From: Stavros Date: Fri, 7 Feb 2025 17:08:39 +0200 Subject: [PATCH] feat: add basic header authorization --- internal/auth/auth.go | 37 +++++++++++++++++++++++++++++++++---- internal/hooks/hooks.go | 21 +++++++++++++-------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 8a1dfd3..3448346 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -73,7 +73,7 @@ func (auth *Auth) DeleteSessionCookie(c *gin.Context) { sessions.Save() } -func (auth *Auth) GetSessionCookie(c *gin.Context) (types.SessionCookie, error) { +func (auth *Auth) GetSessionCookie(c *gin.Context) types.SessionCookie { log.Debug().Msg("Getting session cookie") sessions := sessions.Default(c) @@ -87,13 +87,13 @@ func (auth *Auth) GetSessionCookie(c *gin.Context) (types.SessionCookie, error) if !usernameOk || !providerOk || !expiryOk { log.Warn().Msg("Session cookie invalid") - return types.SessionCookie{}, nil + return types.SessionCookie{} } if time.Now().Unix() > expiry { log.Warn().Msg("Session cookie expired") auth.DeleteSessionCookie(c) - return types.SessionCookie{}, nil + return types.SessionCookie{} } log.Debug().Str("username", username).Str("provider", provider).Int64("expiry", expiry).Msg("Parsed cookie") @@ -101,7 +101,7 @@ func (auth *Auth) GetSessionCookie(c *gin.Context) (types.SessionCookie, error) return types.SessionCookie{ Username: username, Provider: provider, - }, nil + } } func (auth *Auth) UserAuthConfigured() bool { @@ -164,3 +164,32 @@ func (auth *Auth) ResourceAllowed(context types.UserContext, host string) (bool, return true, nil } + +func (auth *Auth) GetBasicAuth(c *gin.Context) types.User { + header := c.GetHeader("Authorization") + + if header == "" { + return types.User{} + } + + headerSplit := strings.Split(header, " ") + + if len(headerSplit) != 2 { + return types.User{} + } + + if headerSplit[0] != "Basic" { + return types.User{} + } + + credentials := strings.Split(headerSplit[1], ":") + + if len(credentials) != 2 { + return types.User{} + } + + return types.User{ + Username: credentials[0], + Password: credentials[1], + } +} diff --git a/internal/hooks/hooks.go b/internal/hooks/hooks.go index 9a1a6db..642b411 100644 --- a/internal/hooks/hooks.go +++ b/internal/hooks/hooks.go @@ -22,16 +22,21 @@ type Hooks struct { } func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext { - cookie, cookiErr := hooks.Auth.GetSessionCookie(c) + cookie := hooks.Auth.GetSessionCookie(c) + basic := hooks.Auth.GetBasicAuth(c) - if cookiErr != nil { - log.Error().Err(cookiErr).Msg("Failed to get session cookie") - return types.UserContext{ - Username: "", - IsLoggedIn: false, - OAuth: false, - Provider: "", + if basic.Username != "" { + log.Debug().Msg("Got basic auth") + user := hooks.Auth.GetUser(basic.Username) + if user != nil && hooks.Auth.CheckPassword(*user, basic.Password) { + return types.UserContext{ + Username: basic.Username, + IsLoggedIn: true, + OAuth: false, + Provider: "", + } } + } if cookie.Provider == "username" {