loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add auth-required to config.json for Cargo http registry (#26729)

Cargo registry-auth feature requires config.json to have a property
auth-required set to true in order to send token to all registry
requests.
This is ok for git index because you can manually edit the config.json
file to add the auth-required, but when using sparse
(setting index url to
"sparse+https://git.example.com/api/packages/{owner}/cargo/"), the
config.json is dynamically rendered, and does not reflect changes to the
config.json file in the repo.

I see two approaches:
- Serve the real config.json file when fetching the config.json on the
cargo service.
- Automatically detect if the registry requires authorization. (This is
what I implemented in this PR).

What the PR does:
- When a cargo index repository is created, on the config.json, set
auth-required to wether or not the repository is private.
- When the cargo/config.json endpoint is called, set auth-required to
wether or not the request was authorized using an API token.

authored by

merlleu and committed by
GitHub
a587d252 8cd46024

+12 -7
+3 -1
routers/api/packages/cargo/cargo.go
··· 16 16 "code.gitea.io/gitea/modules/log" 17 17 packages_module "code.gitea.io/gitea/modules/packages" 18 18 cargo_module "code.gitea.io/gitea/modules/packages/cargo" 19 + "code.gitea.io/gitea/modules/setting" 20 + "code.gitea.io/gitea/modules/structs" 19 21 "code.gitea.io/gitea/modules/util" 20 22 "code.gitea.io/gitea/routers/api/packages/helper" 21 23 "code.gitea.io/gitea/services/convert" ··· 48 50 49 51 // https://rust-lang.github.io/rfcs/2789-sparse-index.html 50 52 func RepositoryConfig(ctx *context.Context) { 51 - ctx.JSON(http.StatusOK, cargo_service.BuildConfig(ctx.Package.Owner)) 53 + ctx.JSON(http.StatusOK, cargo_service.BuildConfig(ctx.Package.Owner, setting.Service.RequireSignInView || ctx.Package.Owner.Visibility != structs.VisibleTypePublic)) 52 54 } 53 55 54 56 func EnumeratePackageVersions(ctx *context.Context) {
+9 -6
services/packages/cargo/index.go
··· 21 21 cargo_module "code.gitea.io/gitea/modules/packages/cargo" 22 22 repo_module "code.gitea.io/gitea/modules/repository" 23 23 "code.gitea.io/gitea/modules/setting" 24 + "code.gitea.io/gitea/modules/structs" 24 25 "code.gitea.io/gitea/modules/util" 25 26 files_service "code.gitea.io/gitea/services/repository/files" 26 27 ) ··· 220 221 } 221 222 222 223 type Config struct { 223 - DownloadURL string `json:"dl"` 224 - APIURL string `json:"api"` 224 + DownloadURL string `json:"dl"` 225 + APIURL string `json:"api"` 226 + AuthRequired bool `json:"auth-required"` 225 227 } 226 228 227 - func BuildConfig(owner *user_model.User) *Config { 229 + func BuildConfig(owner *user_model.User, isPrivate bool) *Config { 228 230 return &Config{ 229 - DownloadURL: setting.AppURL + "api/packages/" + owner.Name + "/cargo/api/v1/crates", 230 - APIURL: setting.AppURL + "api/packages/" + owner.Name + "/cargo", 231 + DownloadURL: setting.AppURL + "api/packages/" + owner.Name + "/cargo/api/v1/crates", 232 + APIURL: setting.AppURL + "api/packages/" + owner.Name + "/cargo", 233 + AuthRequired: isPrivate, 231 234 } 232 235 } 233 236 ··· 239 242 "Initialize Cargo Config", 240 243 func(t *files_service.TemporaryUploadRepository) error { 241 244 var b bytes.Buffer 242 - err := json.NewEncoder(&b).Encode(BuildConfig(owner)) 245 + err := json.NewEncoder(&b).Encode(BuildConfig(owner, setting.Service.RequireSignInView || owner.Visibility != structs.VisibleTypePublic || repo.IsPrivate)) 243 246 if err != nil { 244 247 return err 245 248 }