mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 06:16:58 +00:00
Basic HTTP Auth (#163).
This commit is contained in:
parent
513e79832a
commit
faeb8ae499
4 changed files with 125 additions and 4 deletions
35
server/upstream/auth.go
Normal file
35
server/upstream/auth.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue