@@ -13,24 +13,35 @@ import (
13
13
"code.gitea.io/gitea/modules/context"
14
14
"code.gitea.io/gitea/modules/setting"
15
15
api "code.gitea.io/sdk/gitea"
16
-
17
- "gopkg.in/macaron.v1"
18
16
)
19
17
20
- func checkRequest (req macaron.Request , post bool ) int {
18
+ //checkIsValidRequest check if it a valid request in case of bad request it write the response to ctx.
19
+ func checkIsValidRequest (ctx * context.Context , post bool ) bool {
21
20
if ! setting .LFS .StartServer {
22
- return 404
21
+ writeStatus (ctx , 404 )
22
+ return false
23
+ }
24
+ if ! MetaMatcher (ctx .Req ) {
25
+ writeStatus (ctx , 400 )
26
+ return false
23
27
}
24
- if ! MetaMatcher (req ) {
25
- return 400
28
+ if ! ctx .IsSigned {
29
+ user , _ , _ , err := parseToken (ctx .Req .Header .Get ("Authorization" ))
30
+ if err != nil {
31
+ ctx .Resp .Header ().Set ("WWW-Authenticate" , "Basic realm=gitea-lfs" )
32
+ writeStatus (ctx , 401 )
33
+ return false
34
+ }
35
+ ctx .User = user
26
36
}
27
37
if post {
28
- mediaParts := strings .Split (req .Header .Get ("Content-Type" ), ";" )
38
+ mediaParts := strings .Split (ctx . Req .Header .Get ("Content-Type" ), ";" )
29
39
if mediaParts [0 ] != metaMediaType {
30
- return 400
40
+ writeStatus (ctx , 400 )
41
+ return false
31
42
}
32
43
}
33
- return 200
44
+ return true
34
45
}
35
46
36
47
func handleLockListOut (ctx * context.Context , lock * models.LFSLock , err error ) {
@@ -59,17 +70,16 @@ func handleLockListOut(ctx *context.Context, lock *models.LFSLock, err error) {
59
70
60
71
// GetListLockHandler list locks
61
72
func GetListLockHandler (ctx * context.Context ) {
62
- status := checkRequest (ctx .Req , false )
63
- if status != 200 {
64
- writeStatus (ctx , status )
73
+ if ! checkIsValidRequest (ctx , false ) {
65
74
return
66
75
}
67
76
ctx .Resp .Header ().Set ("Content-Type" , metaMediaType )
68
77
69
- err := models .CheckLFSAccessForRepo (ctx .User , ctx .Repo .Repository . ID , "list" )
78
+ err := models .CheckLFSAccessForRepo (ctx .User , ctx .Repo .Repository , models . AccessModeRead )
70
79
if err != nil {
71
- if models .IsErrLFSLockUnauthorizedAction (err ) {
72
- ctx .JSON (403 , api.LFSLockError {
80
+ if models .IsErrLFSUnauthorizedAction (err ) {
81
+ ctx .Resp .Header ().Set ("WWW-Authenticate" , "Basic realm=gitea-lfs" )
82
+ ctx .JSON (401 , api.LFSLockError {
73
83
Message : "You must have pull access to list locks : " + err .Error (),
74
84
})
75
85
return
@@ -96,7 +106,7 @@ func GetListLockHandler(ctx *context.Context) {
96
106
97
107
path := ctx .Query ("path" )
98
108
if path != "" { //Case where we request a specific id
99
- lock , err := models .GetLFSLock (ctx .Repo .Repository . ID , path )
109
+ lock , err := models .GetLFSLock (ctx .Repo .Repository , path )
100
110
handleLockListOut (ctx , lock , err )
101
111
return
102
112
}
@@ -120,9 +130,7 @@ func GetListLockHandler(ctx *context.Context) {
120
130
121
131
// PostLockHandler create lock
122
132
func PostLockHandler (ctx * context.Context ) {
123
- status := checkRequest (ctx .Req , true )
124
- if status != 200 {
125
- writeStatus (ctx , status )
133
+ if ! checkIsValidRequest (ctx , false ) {
126
134
return
127
135
}
128
136
ctx .Resp .Header ().Set ("Content-Type" , metaMediaType )
@@ -136,9 +144,9 @@ func PostLockHandler(ctx *context.Context) {
136
144
}
137
145
138
146
lock , err := models .CreateLFSLock (& models.LFSLock {
139
- RepoID : ctx .Repo .Repository . ID ,
140
- Path : req .Path ,
141
- Owner : ctx .User ,
147
+ Repo : ctx .Repo .Repository ,
148
+ Path : req .Path ,
149
+ Owner : ctx .User ,
142
150
})
143
151
if err != nil {
144
152
if models .IsErrLFSLockAlreadyExist (err ) {
@@ -148,8 +156,9 @@ func PostLockHandler(ctx *context.Context) {
148
156
})
149
157
return
150
158
}
151
- if models .IsErrLFSLockUnauthorizedAction (err ) {
152
- ctx .JSON (403 , api.LFSLockError {
159
+ if models .IsErrLFSUnauthorizedAction (err ) {
160
+ ctx .Resp .Header ().Set ("WWW-Authenticate" , "Basic realm=gitea-lfs" )
161
+ ctx .JSON (401 , api.LFSLockError {
153
162
Message : "You must have push access to create locks : " + err .Error (),
154
163
})
155
164
return
@@ -164,18 +173,16 @@ func PostLockHandler(ctx *context.Context) {
164
173
165
174
// VerifyLockHandler list locks for verification
166
175
func VerifyLockHandler (ctx * context.Context ) {
167
- status := checkRequest (ctx .Req , true )
168
- if status != 200 {
169
- writeStatus (ctx , status )
176
+ if ! checkIsValidRequest (ctx , false ) {
170
177
return
171
178
}
172
-
173
179
ctx .Resp .Header ().Set ("Content-Type" , metaMediaType )
174
180
175
- err := models .CheckLFSAccessForRepo (ctx .User , ctx .Repo .Repository . ID , "verify" )
181
+ err := models .CheckLFSAccessForRepo (ctx .User , ctx .Repo .Repository , models . AccessModeWrite )
176
182
if err != nil {
177
- if models .IsErrLFSLockUnauthorizedAction (err ) {
178
- ctx .JSON (403 , api.LFSLockError {
183
+ if models .IsErrLFSUnauthorizedAction (err ) {
184
+ ctx .Resp .Header ().Set ("WWW-Authenticate" , "Basic realm=gitea-lfs" )
185
+ ctx .JSON (401 , api.LFSLockError {
179
186
Message : "You must have push access to verify locks : " + err .Error (),
180
187
})
181
188
return
@@ -211,9 +218,7 @@ func VerifyLockHandler(ctx *context.Context) {
211
218
212
219
// UnLockHandler delete locks
213
220
func UnLockHandler (ctx * context.Context ) {
214
- status := checkRequest (ctx .Req , true )
215
- if status != 200 {
216
- writeStatus (ctx , status )
221
+ if ! checkIsValidRequest (ctx , false ) {
217
222
return
218
223
}
219
224
ctx .Resp .Header ().Set ("Content-Type" , metaMediaType )
@@ -228,8 +233,9 @@ func UnLockHandler(ctx *context.Context) {
228
233
229
234
lock , err := models .DeleteLFSLockByID (ctx .ParamsInt64 ("lid" ), ctx .User , req .Force )
230
235
if err != nil {
231
- if models .IsErrLFSLockUnauthorizedAction (err ) {
232
- ctx .JSON (403 , api.LFSLockError {
236
+ if models .IsErrLFSUnauthorizedAction (err ) {
237
+ ctx .Resp .Header ().Set ("WWW-Authenticate" , "Basic realm=gitea-lfs" )
238
+ ctx .JSON (401 , api.LFSLockError {
233
239
Message : "You must have push access to delete locks : " + err .Error (),
234
240
})
235
241
return
0 commit comments