diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-11-13 16:58:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-13 16:58:09 +0800 |
commit | 0aedb03996d7bdce88b1f0086151f8778b10c1a4 (patch) | |
tree | a6586b4efd51cb7c392fde331062005f9c3a9b86 /services/lfs | |
parent | 840ad7eefe2b49ab453b9a89b153a264a8c9f8a2 (diff) | |
download | gitea-0aedb03996d7bdce88b1f0086151f8778b10c1a4.tar.gz gitea-0aedb03996d7bdce88b1f0086151f8778b10c1a4.zip |
Fix LFS route mock, realm, middleware names (#32488)
1. move "internal-lfs" route mock to "common-lfs"
2. fine tune tests
3. fix "realm" strings, according to RFC:
https://datatracker.ietf.org/doc/html/rfc2617:
* realm = "realm" "=" realm-value
* realm-value = quoted-string
4. clarify some names of the middlewares, rename `ignXxx` to `optXxx` to
match `reqXxx`, and rename ambiguous `requireSignIn` to `reqGitSignIn`
Diffstat (limited to 'services/lfs')
-rw-r--r-- | services/lfs/locks.go | 20 | ||||
-rw-r--r-- | services/lfs/server.go | 22 |
2 files changed, 21 insertions, 21 deletions
diff --git a/services/lfs/locks.go b/services/lfs/locks.go index 4254c99383..1d464f4a66 100644 --- a/services/lfs/locks.go +++ b/services/lfs/locks.go @@ -51,7 +51,7 @@ func GetListLockHandler(ctx *context.Context) { repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, rv.User, rv.Repo) if err != nil { log.Debug("Could not find repository: %s/%s - %s", rv.User, rv.Repo, err) - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have pull access to list locks", }) @@ -66,7 +66,7 @@ func GetListLockHandler(ctx *context.Context) { authenticated := authenticate(ctx, repository, rv.Authorization, true, false) if !authenticated { - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have pull access to list locks", }) @@ -143,7 +143,7 @@ func PostLockHandler(ctx *context.Context) { repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, userName, repoName) if err != nil { log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err) - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to create locks", }) @@ -158,7 +158,7 @@ func PostLockHandler(ctx *context.Context) { authenticated := authenticate(ctx, repository, authorization, true, true) if !authenticated { - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to create locks", }) @@ -191,7 +191,7 @@ func PostLockHandler(ctx *context.Context) { return } if git_model.IsErrLFSUnauthorizedAction(err) { - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to create locks : " + err.Error(), }) @@ -215,7 +215,7 @@ func VerifyLockHandler(ctx *context.Context) { repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, userName, repoName) if err != nil { log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err) - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to verify locks", }) @@ -230,7 +230,7 @@ func VerifyLockHandler(ctx *context.Context) { authenticated := authenticate(ctx, repository, authorization, true, true) if !authenticated { - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to verify locks", }) @@ -286,7 +286,7 @@ func UnLockHandler(ctx *context.Context) { repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, userName, repoName) if err != nil { log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err) - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to delete locks", }) @@ -301,7 +301,7 @@ func UnLockHandler(ctx *context.Context) { authenticated := authenticate(ctx, repository, authorization, true, true) if !authenticated { - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to delete locks", }) @@ -324,7 +324,7 @@ func UnLockHandler(ctx *context.Context) { lock, err := git_model.DeleteLFSLockByID(ctx, ctx.PathParamInt64("lid"), repository, ctx.Doer, req.Force) if err != nil { if git_model.IsErrLFSUnauthorizedAction(err) { - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) ctx.JSON(http.StatusUnauthorized, api.LFSLockError{ Message: "You must have push access to delete locks : " + err.Error(), }) diff --git a/services/lfs/server.go b/services/lfs/server.go index f8ef177387..a77623fdc1 100644 --- a/services/lfs/server.go +++ b/services/lfs/server.go @@ -21,7 +21,7 @@ import ( actions_model "code.gitea.io/gitea/models/actions" auth_model "code.gitea.io/gitea/models/auth" git_model "code.gitea.io/gitea/models/git" - "code.gitea.io/gitea/models/perm" + perm_model "code.gitea.io/gitea/models/perm" access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" @@ -77,7 +77,7 @@ func CheckAcceptMediaType(ctx *context.Context) { } } -var rangeHeaderRegexp = regexp.MustCompile(`bytes=(\d+)\-(\d*).*`) +var rangeHeaderRegexp = regexp.MustCompile(`bytes=(\d+)-(\d*).*`) // DownloadHandler gets the content from the content store func DownloadHandler(ctx *context.Context) { @@ -507,11 +507,11 @@ func writeStatusMessage(ctx *context.Context, status int, message string) { } // authenticate uses the authorization string to determine whether -// or not to proceed. This server assumes an HTTP Basic auth format. +// to proceed. This server assumes an HTTP Basic auth format. func authenticate(ctx *context.Context, repository *repo_model.Repository, authorization string, requireSigned, requireWrite bool) bool { - accessMode := perm.AccessModeRead + accessMode := perm_model.AccessModeRead if requireWrite { - accessMode = perm.AccessModeWrite + accessMode = perm_model.AccessModeWrite } if ctx.Data["IsActionsToken"] == true { @@ -526,9 +526,9 @@ func authenticate(ctx *context.Context, repository *repo_model.Repository, autho } if task.IsForkPullRequest { - return accessMode <= perm.AccessModeRead + return accessMode <= perm_model.AccessModeRead } - return accessMode <= perm.AccessModeWrite + return accessMode <= perm_model.AccessModeWrite } // ctx.IsSigned is unnecessary here, this will be checked in perm.CanAccess @@ -553,7 +553,7 @@ func authenticate(ctx *context.Context, repository *repo_model.Repository, autho return true } -func handleLFSToken(ctx stdCtx.Context, tokenSHA string, target *repo_model.Repository, mode perm.AccessMode) (*user_model.User, error) { +func handleLFSToken(ctx stdCtx.Context, tokenSHA string, target *repo_model.Repository, mode perm_model.AccessMode) (*user_model.User, error) { if !strings.Contains(tokenSHA, ".") { return nil, nil } @@ -576,7 +576,7 @@ func handleLFSToken(ctx stdCtx.Context, tokenSHA string, target *repo_model.Repo return nil, fmt.Errorf("invalid token claim") } - if mode == perm.AccessModeWrite && claims.Op != "upload" { + if mode == perm_model.AccessModeWrite && claims.Op != "upload" { return nil, fmt.Errorf("invalid token claim") } @@ -588,7 +588,7 @@ func handleLFSToken(ctx stdCtx.Context, tokenSHA string, target *repo_model.Repo return u, nil } -func parseToken(ctx stdCtx.Context, authorization string, target *repo_model.Repository, mode perm.AccessMode) (*user_model.User, error) { +func parseToken(ctx stdCtx.Context, authorization string, target *repo_model.Repository, mode perm_model.AccessMode) (*user_model.User, error) { if authorization == "" { return nil, fmt.Errorf("no token") } @@ -608,6 +608,6 @@ func parseToken(ctx stdCtx.Context, authorization string, target *repo_model.Rep } func requireAuth(ctx *context.Context) { - ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") + ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`) writeStatus(ctx, http.StatusUnauthorized) } |