pages-server/server/upstream/auth.go
2023-01-31 15:14:31 -05:00

35 lines
1.1 KiB
Go

package upstream
import (
"strings"
"time"
"github.com/rs/zerolog/log"
"codeberg.org/codeberg/pages/server/cache"
"codeberg.org/codeberg/pages/server/gitea"
)
// authCacheTimeout specifies the timeout for the auth cache.
var authCacheTimeout = 5 * time.Minute
const authConfig = ".auth"
// CheckAuth returns the username and password for basic HTTP Auth specified in the repo (using the `.auth` file).
func (o *Options) CheckAuth(giteaClient *gitea.Client, authCache cache.SetGetKey) []string {
var credentials []string
if cachedValue, ok := authCache.Get(o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch); ok {
credentials = cachedValue.([]string)
} else {
body, err := giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, authConfig)
if err == nil {
for _, authLine := range strings.Split(string(body), "\n") {
credentials = append(credentials, authLine)
}
} else {
log.Error().Err(err).Msgf("could not read %s of %s/%s", authConfig, o.TargetOwner, o.TargetRepo)
}
authCache.Set(o.TargetOwner+"/"+o.TargetRepo+"/"+o.TargetBranch, credentials, authCacheTimeout)
}
return credentials
}