Skip to content

Commit 236e660

Browse files
committed
remove ErrLFSUnauthorizedAction, add comment, fix test
1 parent 01e6831 commit 236e660

File tree

5 files changed

+18
-59
lines changed

5 files changed

+18
-59
lines changed

models/git/lfs.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99

1010
"code.gitea.io/gitea/models/db"
11-
"code.gitea.io/gitea/models/perm"
1211
repo_model "code.gitea.io/gitea/models/repo"
1312
"code.gitea.io/gitea/models/unit"
1413
user_model "code.gitea.io/gitea/models/user"
@@ -42,30 +41,6 @@ func (err ErrLFSLockNotExist) Unwrap() error {
4241
return util.ErrNotExist
4342
}
4443

45-
// ErrLFSUnauthorizedAction represents a "LFSUnauthorizedAction" kind of error.
46-
type ErrLFSUnauthorizedAction struct {
47-
RepoID int64
48-
UserName string
49-
Mode perm.AccessMode
50-
}
51-
52-
// IsErrLFSUnauthorizedAction checks if an error is a ErrLFSUnauthorizedAction.
53-
func IsErrLFSUnauthorizedAction(err error) bool {
54-
_, ok := err.(ErrLFSUnauthorizedAction)
55-
return ok
56-
}
57-
58-
func (err ErrLFSUnauthorizedAction) Error() string {
59-
if err.Mode == perm.AccessModeWrite {
60-
return fmt.Sprintf("User %s doesn't have write access for lfs lock [rid: %d]", err.UserName, err.RepoID)
61-
}
62-
return fmt.Sprintf("User %s doesn't have read access for lfs lock [rid: %d]", err.UserName, err.RepoID)
63-
}
64-
65-
func (err ErrLFSUnauthorizedAction) Unwrap() error {
66-
return util.ErrPermissionDenied
67-
}
68-
6944
// ErrLFSLockAlreadyExist represents a "LFSLockAlreadyExist" kind of error.
7045
type ErrLFSLockAlreadyExist struct {
7146
RepoID int64
@@ -93,12 +68,6 @@ type ErrLFSFileLocked struct {
9368
UserName string
9469
}
9570

96-
// IsErrLFSFileLocked checks if an error is a ErrLFSFileLocked.
97-
func IsErrLFSFileLocked(err error) bool {
98-
_, ok := err.(ErrLFSFileLocked)
99-
return ok
100-
}
101-
10271
func (err ErrLFSFileLocked) Error() string {
10372
return fmt.Sprintf("File is lfs locked [repo: %d, locked by: %s, path: %s]", err.RepoID, err.UserName, err.Path)
10473
}

services/lfs/locks.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,6 @@ func PostLockHandler(ctx *context.Context) {
187187
})
188188
return
189189
}
190-
if git_model.IsErrLFSUnauthorizedAction(err) {
191-
ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
192-
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
193-
Message: "You must have push access to create locks : " + err.Error(),
194-
})
195-
return
196-
}
197190
log.Error("Unable to CreateLFSLock in repository %-v at %s for user %-v: Error: %v", repository, req.Path, ctx.Doer, err)
198191
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
199192
Message: "internal server error : Internal Server Error",
@@ -317,13 +310,6 @@ func UnLockHandler(ctx *context.Context) {
317310

318311
lock, err := git_model.DeleteLFSLockByID(ctx, ctx.PathParamInt64("lid"), repository, ctx.Doer, req.Force)
319312
if err != nil {
320-
if git_model.IsErrLFSUnauthorizedAction(err) {
321-
ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
322-
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
323-
Message: "You must have push access to delete locks : " + err.Error(),
324-
})
325-
return
326-
}
327313
log.Error("Unable to DeleteLFSLockByID[%d] by user %-v with force %t: Error: %v", ctx.PathParamInt64("lid"), ctx.Doer, req.Force, err)
328314
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
329315
Message: "unable to delete lock : Internal Server Error",

services/lfs/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,14 @@ func authenticate(ctx *context.Context, repository *repo_model.Repository, autho
563563
return false
564564
}
565565

566-
canRead := perm.CanAccess(accessMode, unit.TypeCode)
567-
if canRead && (!requireSigned || ctx.IsSigned) {
566+
canAccess := perm.CanAccess(accessMode, unit.TypeCode)
567+
// if it doesn't require sign-in and anonymous user has access, return true
568+
// if the user is already signed in (for example: by session auth method), and the doer can access, return true
569+
if canAccess && (!requireSigned || ctx.IsSigned) {
568570
return true
569571
}
570572

573+
// now, either sign-in is required or the ctx.Doer cannot access, check the LFS token
571574
user, err := parseToken(ctx, authorization, repository, accessMode)
572575
if err != nil {
573576
// Most of these are Warn level - the true internal server errors are logged in parseToken already

tests/integration/api_repo_file_get_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"testing"
1010

1111
auth_model "code.gitea.io/gitea/models/auth"
12-
api "code.gitea.io/gitea/modules/structs"
1312
"code.gitea.io/gitea/tests"
1413

1514
"github.com/stretchr/testify/assert"
@@ -25,8 +24,9 @@ func TestAPIGetRawFileOrLFS(t *testing.T) {
2524

2625
// Test with LFS
2726
onGiteaRun(t, func(t *testing.T, u *url.URL) {
28-
httpContext := NewAPITestContext(t, "user2", "repo-lfs-test", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
29-
t.Run("repo-lfs-test", doAPICreateRepository(httpContext, false, func(t *testing.T, repository api.Repository) {
27+
createLFSTestRepository(t, "repo-lfs-test")
28+
httpContext := NewAPITestContext(t, "user2", "repo-lfs-test", auth_model.AccessTokenScopeWriteRepository)
29+
t.Run("repo-lfs-test", func(t *testing.T) {
3030
u.Path = httpContext.GitPath()
3131
dstPath := t.TempDir()
3232

@@ -44,6 +44,6 @@ func TestAPIGetRawFileOrLFS(t *testing.T) {
4444
reqLFS := NewRequest(t, "GET", "/api/v1/repos/user2/repo-lfs-test/media/"+lfs).AddTokenAuth(httpContext.Token)
4545
respLFS := MakeRequestNilResponseRecorder(t, reqLFS, http.StatusOK)
4646
assert.Equal(t, testFileSizeSmall, respLFS.Length)
47-
}))
47+
})
4848
})
4949
}

tests/integration/api_repo_lfs_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/tests"
2424

2525
"github.com/stretchr/testify/assert"
26+
"github.com/stretchr/testify/require"
2627
)
2728

2829
func TestAPILFSNotStarted(t *testing.T) {
@@ -59,12 +60,12 @@ func TestAPILFSMediaType(t *testing.T) {
5960
MakeRequest(t, req, http.StatusUnsupportedMediaType)
6061
}
6162

62-
func createLFSTestRepository(t *testing.T, name string) *repo_model.Repository {
63-
ctx := NewAPITestContext(t, "user2", "lfs-"+name+"-repo", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
63+
func createLFSTestRepository(t *testing.T, repoName string) *repo_model.Repository {
64+
ctx := NewAPITestContext(t, "user2", repoName, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
6465
t.Run("CreateRepo", doAPICreateRepository(ctx, false))
6566

66-
repo, err := repo_model.GetRepositoryByOwnerAndName(t.Context(), "user2", "lfs-"+name+"-repo")
67-
assert.NoError(t, err)
67+
repo, err := repo_model.GetRepositoryByOwnerAndName(t.Context(), "user2", repoName)
68+
require.NoError(t, err)
6869

6970
return repo
7071
}
@@ -74,7 +75,7 @@ func TestAPILFSBatch(t *testing.T) {
7475

7576
setting.LFS.StartServer = true
7677

77-
repo := createLFSTestRepository(t, "batch")
78+
repo := createLFSTestRepository(t, "lfs-batch-repo")
7879

7980
content := []byte("dummy1")
8081
oid := storeObjectInRepo(t, repo.ID, &content)
@@ -253,7 +254,7 @@ func TestAPILFSBatch(t *testing.T) {
253254
assert.NoError(t, err)
254255
assert.True(t, exist)
255256

256-
repo2 := createLFSTestRepository(t, "batch2")
257+
repo2 := createLFSTestRepository(t, "lfs-batch2-repo")
257258
content := []byte("dummy0")
258259
storeObjectInRepo(t, repo2.ID, &content)
259260

@@ -329,7 +330,7 @@ func TestAPILFSUpload(t *testing.T) {
329330

330331
setting.LFS.StartServer = true
331332

332-
repo := createLFSTestRepository(t, "upload")
333+
repo := createLFSTestRepository(t, "lfs-upload-repo")
333334

334335
content := []byte("dummy3")
335336
oid := storeObjectInRepo(t, repo.ID, &content)
@@ -433,7 +434,7 @@ func TestAPILFSVerify(t *testing.T) {
433434

434435
setting.LFS.StartServer = true
435436

436-
repo := createLFSTestRepository(t, "verify")
437+
repo := createLFSTestRepository(t, "lfs-verify-repo")
437438

438439
content := []byte("dummy3")
439440
oid := storeObjectInRepo(t, repo.ID, &content)

0 commit comments

Comments
 (0)