Skip to content

Commit a587d25

Browse files
authored
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.
1 parent 8cd4602 commit a587d25

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

routers/api/packages/cargo/cargo.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"code.gitea.io/gitea/modules/log"
1717
packages_module "code.gitea.io/gitea/modules/packages"
1818
cargo_module "code.gitea.io/gitea/modules/packages/cargo"
19+
"code.gitea.io/gitea/modules/setting"
20+
"code.gitea.io/gitea/modules/structs"
1921
"code.gitea.io/gitea/modules/util"
2022
"code.gitea.io/gitea/routers/api/packages/helper"
2123
"code.gitea.io/gitea/services/convert"
@@ -48,7 +50,7 @@ func apiError(ctx *context.Context, status int, obj any) {
4850

4951
// https://rust-lang.github.io/rfcs/2789-sparse-index.html
5052
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))
5254
}
5355

5456
func EnumeratePackageVersions(ctx *context.Context) {

services/packages/cargo/index.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
cargo_module "code.gitea.io/gitea/modules/packages/cargo"
2222
repo_module "code.gitea.io/gitea/modules/repository"
2323
"code.gitea.io/gitea/modules/setting"
24+
"code.gitea.io/gitea/modules/structs"
2425
"code.gitea.io/gitea/modules/util"
2526
files_service "code.gitea.io/gitea/services/repository/files"
2627
)
@@ -220,14 +221,16 @@ func getOrCreateIndexRepository(ctx context.Context, doer, owner *user_model.Use
220221
}
221222

222223
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"`
225227
}
226228

227-
func BuildConfig(owner *user_model.User) *Config {
229+
func BuildConfig(owner *user_model.User, isPrivate bool) *Config {
228230
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,
231234
}
232235
}
233236

@@ -239,7 +242,7 @@ func createOrUpdateConfigFile(ctx context.Context, repo *repo_model.Repository,
239242
"Initialize Cargo Config",
240243
func(t *files_service.TemporaryUploadRepository) error {
241244
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))
243246
if err != nil {
244247
return err
245248
}

0 commit comments

Comments
 (0)