Do not use oauth client without token (#1803)

Closes https://github.com/woodpecker-ci/woodpecker/issues/1370
This commit is contained in:
qwerty287
2023-06-03 03:03:06 +02:00
committed by GitHub
parent 259d970faf
commit b59d654f45
5 changed files with 37 additions and 38 deletions

View File

@@ -15,9 +15,15 @@
package common
import (
"context"
"net"
"net/url"
"strings"
"github.com/rs/zerolog/log"
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/server/store"
)
func ExtractHostFromCloneURL(cloneURL string) (string, error) {
@@ -37,3 +43,24 @@ func ExtractHostFromCloneURL(cloneURL string) (string, error) {
return host, nil
}
func UserToken(ctx context.Context, r *model.Repo, u *model.User) string {
if u != nil {
return u.Token
}
_store, ok := store.TryFromContext(ctx)
if !ok {
log.Error().Msg("could not get store from context")
return ""
}
if r == nil {
log.Error().Msg("can not get user token by empty repo")
return ""
}
user, err := _store.GetUser(r.UserID)
if err != nil {
return ""
}
return user.Token
}