From 4e69644021b5b01971fb6c9c1f34ecb1546dfd97 Mon Sep 17 00:00:00 2001 From: Zherphy <1123678689@qq.com> Date: Fri, 29 Nov 2024 14:04:26 +0800 Subject: [PATCH] add: add default token add default token --- auth/gitee.go | 12 ++++++++++++ config/config.go | 1 + 2 files changed, 13 insertions(+) diff --git a/auth/gitee.go b/auth/gitee.go index 40d400a..ebe5415 100644 --- a/auth/gitee.go +++ b/auth/gitee.go @@ -15,6 +15,7 @@ import ( var ( clientId string clientSecret string + defaultToken string ) var ( @@ -64,6 +65,13 @@ func Init(cfg *config.Config) error { return errors.New("client secret required") } } + defaultToken = cfg.DefaultToken + if defaultToken == "" { + defaultToken = os.Getenv("DEFAULT_TOKEN") + if defaultToken == "" { + return errors.New("default token required") + } + } return nil } @@ -96,6 +104,8 @@ func CheckRepoOwner(userInRepo UserInRepo) error { ) if userInRepo.Token != "" { path += fmt.Sprintf("?access_token=%s", userInRepo.Token) + } else { + path += fmt.Sprintf("?access_token=%s", defaultToken) } headers := http.Header{"Content-Type": []string{"application/json;charset=UTF-8"}} repo := new(Repo) @@ -154,6 +164,8 @@ func verifyUser(userInRepo UserInRepo) error { ) if userInRepo.Token != "" { path += fmt.Sprintf("?access_token=%s", userInRepo.Token) + } else { + path += fmt.Sprintf("?access_token=%s", defaultToken) } headers := http.Header{"Content-Type": []string{"application/json;charset=UTF-8"}} giteeUser := new(giteeUser) diff --git a/config/config.go b/config/config.go index 63952db..adfa5d3 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ type Config struct { ObsAccessKeyId string `json:"OBS_ACCESS_KEY_ID"` ObsSecretAccessKey string `json:"OBS_SECRET_ACCESS_KEY"` ValidateConfig ValidateConfig `json:"VALIDATE_REGEXP"` + DefaultToken string `json:"DEFAULT_TOKEN"` } type ValidateConfig struct {