feat: add basic header authorization

This commit is contained in:
Stavros
2025-02-07 17:08:39 +02:00
parent ce567ae3de
commit daad2abc33
2 changed files with 46 additions and 12 deletions

View File

@@ -73,7 +73,7 @@ func (auth *Auth) DeleteSessionCookie(c *gin.Context) {
sessions.Save() 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") log.Debug().Msg("Getting session cookie")
sessions := sessions.Default(c) sessions := sessions.Default(c)
@@ -87,13 +87,13 @@ func (auth *Auth) GetSessionCookie(c *gin.Context) (types.SessionCookie, error)
if !usernameOk || !providerOk || !expiryOk { if !usernameOk || !providerOk || !expiryOk {
log.Warn().Msg("Session cookie invalid") log.Warn().Msg("Session cookie invalid")
return types.SessionCookie{}, nil return types.SessionCookie{}
} }
if time.Now().Unix() > expiry { if time.Now().Unix() > expiry {
log.Warn().Msg("Session cookie expired") log.Warn().Msg("Session cookie expired")
auth.DeleteSessionCookie(c) auth.DeleteSessionCookie(c)
return types.SessionCookie{}, nil return types.SessionCookie{}
} }
log.Debug().Str("username", username).Str("provider", provider).Int64("expiry", expiry).Msg("Parsed cookie") 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{ return types.SessionCookie{
Username: username, Username: username,
Provider: provider, Provider: provider,
}, nil }
} }
func (auth *Auth) UserAuthConfigured() bool { func (auth *Auth) UserAuthConfigured() bool {
@@ -164,3 +164,32 @@ func (auth *Auth) ResourceAllowed(context types.UserContext, host string) (bool,
return true, nil 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],
}
}

View File

@@ -22,16 +22,21 @@ type Hooks struct {
} }
func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext { 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 { if basic.Username != "" {
log.Error().Err(cookiErr).Msg("Failed to get session cookie") log.Debug().Msg("Got basic auth")
return types.UserContext{ user := hooks.Auth.GetUser(basic.Username)
Username: "", if user != nil && hooks.Auth.CheckPassword(*user, basic.Password) {
IsLoggedIn: false, return types.UserContext{
OAuth: false, Username: basic.Username,
Provider: "", IsLoggedIn: true,
OAuth: false,
Provider: "",
}
} }
} }
if cookie.Provider == "username" { if cookie.Provider == "username" {