You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package lfs
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "path"
  11. "regexp"
  12. "strconv"
  13. "strings"
  14. "code.gitea.io/gitea/models"
  15. "code.gitea.io/gitea/modules/context"
  16. lfs_module "code.gitea.io/gitea/modules/lfs"
  17. "code.gitea.io/gitea/modules/log"
  18. "code.gitea.io/gitea/modules/setting"
  19. "github.com/dgrijalva/jwt-go"
  20. jsoniter "github.com/json-iterator/go"
  21. )
  22. // requestContext contain variables from the HTTP request.
  23. type requestContext struct {
  24. User string
  25. Repo string
  26. Authorization string
  27. }
  28. // Claims is a JWT Token Claims
  29. type Claims struct {
  30. RepoID int64
  31. Op string
  32. UserID int64
  33. jwt.StandardClaims
  34. }
  35. // ObjectLink builds a URL linking to the object.
  36. func (rc *requestContext) ObjectLink(oid string) string {
  37. return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/objects", oid)
  38. }
  39. // VerifyLink builds a URL for verifying the object.
  40. func (rc *requestContext) VerifyLink() string {
  41. return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/verify")
  42. }
  43. var oidRegExp = regexp.MustCompile(`^[A-Fa-f0-9]+$`)
  44. func isOidValid(oid string) bool {
  45. return oidRegExp.MatchString(oid)
  46. }
  47. // ObjectOidHandler is the main request routing entry point into LFS server functions
  48. func ObjectOidHandler(ctx *context.Context) {
  49. if !setting.LFS.StartServer {
  50. log.Debug("Attempt to access LFS server but LFS server is disabled")
  51. writeStatus(ctx, 404)
  52. return
  53. }
  54. if ctx.Req.Method == "GET" || ctx.Req.Method == "HEAD" {
  55. if MetaMatcher(ctx.Req) {
  56. getMetaHandler(ctx)
  57. return
  58. }
  59. getContentHandler(ctx)
  60. return
  61. } else if ctx.Req.Method == "PUT" {
  62. PutHandler(ctx)
  63. return
  64. }
  65. log.Warn("Unhandled LFS method: %s for %s/%s OID[%s]", ctx.Req.Method, ctx.Params("username"), ctx.Params("reponame"), ctx.Params("oid"))
  66. writeStatus(ctx, 404)
  67. }
  68. func getAuthenticatedRepoAndMeta(ctx *context.Context, rc *requestContext, p lfs_module.Pointer, requireWrite bool) (*models.LFSMetaObject, *models.Repository) {
  69. if !isOidValid(p.Oid) {
  70. log.Info("Attempt to access invalid LFS OID[%s] in %s/%s", p.Oid, rc.User, rc.Repo)
  71. writeStatus(ctx, 404)
  72. return nil, nil
  73. }
  74. repository, err := models.GetRepositoryByOwnerAndName(rc.User, rc.Repo)
  75. if err != nil {
  76. log.Error("Unable to get repository: %s/%s Error: %v", rc.User, rc.Repo, err)
  77. writeStatus(ctx, 404)
  78. return nil, nil
  79. }
  80. if !authenticate(ctx, repository, rc.Authorization, requireWrite) {
  81. requireAuth(ctx)
  82. return nil, nil
  83. }
  84. meta, err := repository.GetLFSMetaObjectByOid(p.Oid)
  85. if err != nil {
  86. log.Error("Unable to get LFS OID[%s] Error: %v", p.Oid, err)
  87. writeStatus(ctx, 404)
  88. return nil, nil
  89. }
  90. return meta, repository
  91. }
  92. // getContentHandler gets the content from the content store
  93. func getContentHandler(ctx *context.Context) {
  94. rc, p := unpack(ctx)
  95. meta, _ := getAuthenticatedRepoAndMeta(ctx, rc, p, false)
  96. if meta == nil {
  97. // Status already written in getAuthenticatedRepoAndMeta
  98. return
  99. }
  100. // Support resume download using Range header
  101. var fromByte, toByte int64
  102. toByte = meta.Size - 1
  103. statusCode := 200
  104. if rangeHdr := ctx.Req.Header.Get("Range"); rangeHdr != "" {
  105. regex := regexp.MustCompile(`bytes=(\d+)\-(\d*).*`)
  106. match := regex.FindStringSubmatch(rangeHdr)
  107. if len(match) > 1 {
  108. statusCode = 206
  109. fromByte, _ = strconv.ParseInt(match[1], 10, 32)
  110. if fromByte >= meta.Size {
  111. writeStatus(ctx, http.StatusRequestedRangeNotSatisfiable)
  112. return
  113. }
  114. if match[2] != "" {
  115. _toByte, _ := strconv.ParseInt(match[2], 10, 32)
  116. if _toByte >= fromByte && _toByte < toByte {
  117. toByte = _toByte
  118. }
  119. }
  120. ctx.Resp.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", fromByte, toByte, meta.Size-fromByte))
  121. ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Range")
  122. }
  123. }
  124. contentStore := lfs_module.NewContentStore()
  125. content, err := contentStore.Get(meta.Pointer)
  126. if err != nil {
  127. // Errors are logged in contentStore.Get
  128. writeStatus(ctx, http.StatusNotFound)
  129. return
  130. }
  131. defer content.Close()
  132. if fromByte > 0 {
  133. _, err = content.Seek(fromByte, io.SeekStart)
  134. if err != nil {
  135. log.Error("Whilst trying to read LFS OID[%s]: Unable to seek to %d Error: %v", meta.Oid, fromByte, err)
  136. writeStatus(ctx, http.StatusInternalServerError)
  137. return
  138. }
  139. }
  140. contentLength := toByte + 1 - fromByte
  141. ctx.Resp.Header().Set("Content-Length", strconv.FormatInt(contentLength, 10))
  142. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  143. filename := ctx.Params("filename")
  144. if len(filename) > 0 {
  145. decodedFilename, err := base64.RawURLEncoding.DecodeString(filename)
  146. if err == nil {
  147. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename=\""+string(decodedFilename)+"\"")
  148. ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
  149. }
  150. }
  151. ctx.Resp.WriteHeader(statusCode)
  152. if written, err := io.CopyN(ctx.Resp, content, contentLength); err != nil {
  153. log.Error("Error whilst copying LFS OID[%s] to the response after %d bytes. Error: %v", meta.Oid, written, err)
  154. }
  155. logRequest(ctx.Req, statusCode)
  156. }
  157. // getMetaHandler retrieves metadata about the object
  158. func getMetaHandler(ctx *context.Context) {
  159. rc, p := unpack(ctx)
  160. meta, _ := getAuthenticatedRepoAndMeta(ctx, rc, p, false)
  161. if meta == nil {
  162. // Status already written in getAuthenticatedRepoAndMeta
  163. return
  164. }
  165. ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
  166. if ctx.Req.Method == "GET" {
  167. json := jsoniter.ConfigCompatibleWithStandardLibrary
  168. enc := json.NewEncoder(ctx.Resp)
  169. if err := enc.Encode(represent(rc, meta.Pointer, true, false)); err != nil {
  170. log.Error("Failed to encode representation as json. Error: %v", err)
  171. }
  172. }
  173. logRequest(ctx.Req, 200)
  174. }
  175. // PostHandler instructs the client how to upload data
  176. func PostHandler(ctx *context.Context) {
  177. if !setting.LFS.StartServer {
  178. log.Debug("Attempt to access LFS server but LFS server is disabled")
  179. writeStatus(ctx, 404)
  180. return
  181. }
  182. if !MetaMatcher(ctx.Req) {
  183. log.Info("Attempt to POST without accepting the correct media type: %s", lfs_module.MediaType)
  184. writeStatus(ctx, 400)
  185. return
  186. }
  187. rc, p := unpack(ctx)
  188. repository, err := models.GetRepositoryByOwnerAndName(rc.User, rc.Repo)
  189. if err != nil {
  190. log.Error("Unable to get repository: %s/%s Error: %v", rc.User, rc.Repo, err)
  191. writeStatus(ctx, 404)
  192. return
  193. }
  194. if !authenticate(ctx, repository, rc.Authorization, true) {
  195. requireAuth(ctx)
  196. return
  197. }
  198. if !isOidValid(p.Oid) {
  199. log.Info("Invalid LFS OID[%s] attempt to POST in %s/%s", p.Oid, rc.User, rc.Repo)
  200. writeStatus(ctx, 404)
  201. return
  202. }
  203. if setting.LFS.MaxFileSize > 0 && p.Size > setting.LFS.MaxFileSize {
  204. log.Info("Denied LFS OID[%s] upload of size %d to %s/%s because of LFS_MAX_FILE_SIZE=%d", p.Oid, p.Size, rc.User, rc.Repo, setting.LFS.MaxFileSize)
  205. writeStatus(ctx, 413)
  206. return
  207. }
  208. meta, err := models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: p, RepositoryID: repository.ID})
  209. if err != nil {
  210. log.Error("Unable to write LFS OID[%s] size %d meta object in %v/%v to database. Error: %v", p.Oid, p.Size, rc.User, rc.Repo, err)
  211. writeStatus(ctx, 404)
  212. return
  213. }
  214. ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
  215. sentStatus := 202
  216. contentStore := lfs_module.NewContentStore()
  217. exist, err := contentStore.Exists(p)
  218. if err != nil {
  219. log.Error("Unable to check if LFS OID[%s] exist on %s / %s. Error: %v", p.Oid, rc.User, rc.Repo, err)
  220. writeStatus(ctx, 500)
  221. return
  222. }
  223. if meta.Existing && exist {
  224. sentStatus = 200
  225. }
  226. ctx.Resp.WriteHeader(sentStatus)
  227. json := jsoniter.ConfigCompatibleWithStandardLibrary
  228. enc := json.NewEncoder(ctx.Resp)
  229. if err := enc.Encode(represent(rc, meta.Pointer, meta.Existing, true)); err != nil {
  230. log.Error("Failed to encode representation as json. Error: %v", err)
  231. }
  232. logRequest(ctx.Req, sentStatus)
  233. }
  234. // BatchHandler provides the batch api
  235. func BatchHandler(ctx *context.Context) {
  236. if !setting.LFS.StartServer {
  237. log.Debug("Attempt to access LFS server but LFS server is disabled")
  238. writeStatus(ctx, 404)
  239. return
  240. }
  241. if !MetaMatcher(ctx.Req) {
  242. log.Info("Attempt to BATCH without accepting the correct media type: %s", lfs_module.MediaType)
  243. writeStatus(ctx, 400)
  244. return
  245. }
  246. bv := unpackbatch(ctx)
  247. reqCtx := &requestContext{
  248. User: ctx.Params("username"),
  249. Repo: strings.TrimSuffix(ctx.Params("reponame"), ".git"),
  250. Authorization: ctx.Req.Header.Get("Authorization"),
  251. }
  252. var responseObjects []*lfs_module.ObjectResponse
  253. // Create a response object
  254. for _, object := range bv.Objects {
  255. if !isOidValid(object.Oid) {
  256. log.Info("Invalid LFS OID[%s] attempt to BATCH in %s/%s", object.Oid, reqCtx.User, reqCtx.Repo)
  257. continue
  258. }
  259. repository, err := models.GetRepositoryByOwnerAndName(reqCtx.User, reqCtx.Repo)
  260. if err != nil {
  261. log.Error("Unable to get repository: %s/%s Error: %v", reqCtx.User, reqCtx.Repo, err)
  262. writeStatus(ctx, 404)
  263. return
  264. }
  265. requireWrite := false
  266. if bv.Operation == "upload" {
  267. requireWrite = true
  268. }
  269. if !authenticate(ctx, repository, reqCtx.Authorization, requireWrite) {
  270. requireAuth(ctx)
  271. return
  272. }
  273. contentStore := lfs_module.NewContentStore()
  274. meta, err := repository.GetLFSMetaObjectByOid(object.Oid)
  275. if err == nil { // Object is found and exists
  276. exist, err := contentStore.Exists(meta.Pointer)
  277. if err != nil {
  278. log.Error("Unable to check if LFS OID[%s] exist on %s / %s. Error: %v", object.Oid, reqCtx.User, reqCtx.Repo, err)
  279. writeStatus(ctx, 500)
  280. return
  281. }
  282. if exist {
  283. responseObjects = append(responseObjects, represent(reqCtx, meta.Pointer, true, false))
  284. continue
  285. }
  286. }
  287. if requireWrite && setting.LFS.MaxFileSize > 0 && object.Size > setting.LFS.MaxFileSize {
  288. log.Info("Denied LFS OID[%s] upload of size %d to %s/%s because of LFS_MAX_FILE_SIZE=%d", object.Oid, object.Size, reqCtx.User, reqCtx.Repo, setting.LFS.MaxFileSize)
  289. writeStatus(ctx, 413)
  290. return
  291. }
  292. // Object is not found
  293. meta, err = models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: object, RepositoryID: repository.ID})
  294. if err == nil {
  295. exist, err := contentStore.Exists(meta.Pointer)
  296. if err != nil {
  297. log.Error("Unable to check if LFS OID[%s] exist on %s / %s. Error: %v", object.Oid, reqCtx.User, reqCtx.Repo, err)
  298. writeStatus(ctx, 500)
  299. return
  300. }
  301. responseObjects = append(responseObjects, represent(reqCtx, meta.Pointer, meta.Existing, !exist))
  302. } else {
  303. log.Error("Unable to write LFS OID[%s] size %d meta object in %v/%v to database. Error: %v", object.Oid, object.Size, reqCtx.User, reqCtx.Repo, err)
  304. }
  305. }
  306. ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
  307. respobj := &lfs_module.BatchResponse{Objects: responseObjects}
  308. json := jsoniter.ConfigCompatibleWithStandardLibrary
  309. enc := json.NewEncoder(ctx.Resp)
  310. if err := enc.Encode(respobj); err != nil {
  311. log.Error("Failed to encode representation as json. Error: %v", err)
  312. }
  313. logRequest(ctx.Req, 200)
  314. }
  315. // PutHandler receives data from the client and puts it into the content store
  316. func PutHandler(ctx *context.Context) {
  317. rc, p := unpack(ctx)
  318. meta, repository := getAuthenticatedRepoAndMeta(ctx, rc, p, true)
  319. if meta == nil {
  320. // Status already written in getAuthenticatedRepoAndMeta
  321. return
  322. }
  323. contentStore := lfs_module.NewContentStore()
  324. defer ctx.Req.Body.Close()
  325. if err := contentStore.Put(meta.Pointer, ctx.Req.Body); err != nil {
  326. // Put will log the error itself
  327. ctx.Resp.WriteHeader(500)
  328. if err == lfs_module.ErrSizeMismatch || err == lfs_module.ErrHashMismatch {
  329. fmt.Fprintf(ctx.Resp, `{"message":"%s"}`, err)
  330. } else {
  331. fmt.Fprintf(ctx.Resp, `{"message":"Internal Server Error"}`)
  332. }
  333. if _, err = repository.RemoveLFSMetaObjectByOid(p.Oid); err != nil {
  334. log.Error("Whilst removing metaobject for LFS OID[%s] due to preceding error there was another Error: %v", p.Oid, err)
  335. }
  336. return
  337. }
  338. logRequest(ctx.Req, 200)
  339. }
  340. // VerifyHandler verify oid and its size from the content store
  341. func VerifyHandler(ctx *context.Context) {
  342. if !setting.LFS.StartServer {
  343. log.Debug("Attempt to access LFS server but LFS server is disabled")
  344. writeStatus(ctx, 404)
  345. return
  346. }
  347. if !MetaMatcher(ctx.Req) {
  348. log.Info("Attempt to VERIFY without accepting the correct media type: %s", lfs_module.MediaType)
  349. writeStatus(ctx, 400)
  350. return
  351. }
  352. rc, p := unpack(ctx)
  353. meta, _ := getAuthenticatedRepoAndMeta(ctx, rc, p, true)
  354. if meta == nil {
  355. // Status already written in getAuthenticatedRepoAndMeta
  356. return
  357. }
  358. contentStore := lfs_module.NewContentStore()
  359. ok, err := contentStore.Verify(meta.Pointer)
  360. if err != nil {
  361. // Error will be logged in Verify
  362. ctx.Resp.WriteHeader(500)
  363. fmt.Fprintf(ctx.Resp, `{"message":"Internal Server Error"}`)
  364. return
  365. }
  366. if !ok {
  367. writeStatus(ctx, 422)
  368. return
  369. }
  370. logRequest(ctx.Req, 200)
  371. }
  372. // represent takes a requestContext and Meta and turns it into a ObjectResponse suitable
  373. // for json encoding
  374. func represent(rc *requestContext, pointer lfs_module.Pointer, download, upload bool) *lfs_module.ObjectResponse {
  375. rep := &lfs_module.ObjectResponse{
  376. Pointer: pointer,
  377. Actions: make(map[string]*lfs_module.Link),
  378. }
  379. header := make(map[string]string)
  380. if rc.Authorization == "" {
  381. //https://github.com/github/git-lfs/issues/1088
  382. header["Authorization"] = "Authorization: Basic dummy"
  383. } else {
  384. header["Authorization"] = rc.Authorization
  385. }
  386. if download {
  387. rep.Actions["download"] = &lfs_module.Link{Href: rc.ObjectLink(pointer.Oid), Header: header}
  388. }
  389. if upload {
  390. rep.Actions["upload"] = &lfs_module.Link{Href: rc.ObjectLink(pointer.Oid), Header: header}
  391. }
  392. if upload && !download {
  393. // Force client side verify action while gitea lacks proper server side verification
  394. verifyHeader := make(map[string]string)
  395. for k, v := range header {
  396. verifyHeader[k] = v
  397. }
  398. // This is only needed to workaround https://github.com/git-lfs/git-lfs/issues/3662
  399. verifyHeader["Accept"] = lfs_module.MediaType
  400. rep.Actions["verify"] = &lfs_module.Link{Href: rc.VerifyLink(), Header: verifyHeader}
  401. }
  402. return rep
  403. }
  404. // MetaMatcher provides a mux.MatcherFunc that only allows requests that contain
  405. // an Accept header with the lfs_module.MediaType
  406. func MetaMatcher(r *http.Request) bool {
  407. mediaParts := strings.Split(r.Header.Get("Accept"), ";")
  408. mt := mediaParts[0]
  409. return mt == lfs_module.MediaType
  410. }
  411. func unpack(ctx *context.Context) (*requestContext, lfs_module.Pointer) {
  412. r := ctx.Req
  413. rc := &requestContext{
  414. User: ctx.Params("username"),
  415. Repo: strings.TrimSuffix(ctx.Params("reponame"), ".git"),
  416. Authorization: r.Header.Get("Authorization"),
  417. }
  418. p := lfs_module.Pointer{Oid: ctx.Params("oid")}
  419. if r.Method == "POST" { // Maybe also check if +json
  420. var p2 lfs_module.Pointer
  421. bodyReader := r.Body
  422. defer bodyReader.Close()
  423. json := jsoniter.ConfigCompatibleWithStandardLibrary
  424. dec := json.NewDecoder(bodyReader)
  425. err := dec.Decode(&p2)
  426. if err != nil {
  427. // The error is logged as a WARN here because this may represent misbehaviour rather than a true error
  428. log.Warn("Unable to decode POST request vars for LFS OID[%s] in %s/%s: Error: %v", p.Oid, rc.User, rc.Repo, err)
  429. return rc, p
  430. }
  431. p.Oid = p2.Oid
  432. p.Size = p2.Size
  433. }
  434. return rc, p
  435. }
  436. // TODO cheap hack, unify with unpack
  437. func unpackbatch(ctx *context.Context) *lfs_module.BatchRequest {
  438. r := ctx.Req
  439. var bv lfs_module.BatchRequest
  440. bodyReader := r.Body
  441. defer bodyReader.Close()
  442. json := jsoniter.ConfigCompatibleWithStandardLibrary
  443. dec := json.NewDecoder(bodyReader)
  444. err := dec.Decode(&bv)
  445. if err != nil {
  446. // The error is logged as a WARN here because this may represent misbehaviour rather than a true error
  447. log.Warn("Unable to decode BATCH request vars in %s/%s: Error: %v", ctx.Params("username"), strings.TrimSuffix(ctx.Params("reponame"), ".git"), err)
  448. return &bv
  449. }
  450. return &bv
  451. }
  452. func writeStatus(ctx *context.Context, status int) {
  453. message := http.StatusText(status)
  454. mediaParts := strings.Split(ctx.Req.Header.Get("Accept"), ";")
  455. mt := mediaParts[0]
  456. if strings.HasSuffix(mt, "+json") {
  457. message = `{"message":"` + message + `"}`
  458. }
  459. ctx.Resp.WriteHeader(status)
  460. fmt.Fprint(ctx.Resp, message)
  461. logRequest(ctx.Req, status)
  462. }
  463. func logRequest(r *http.Request, status int) {
  464. log.Debug("LFS request - Method: %s, URL: %s, Status %d", r.Method, r.URL, status)
  465. }
  466. // authenticate uses the authorization string to determine whether
  467. // or not to proceed. This server assumes an HTTP Basic auth format.
  468. func authenticate(ctx *context.Context, repository *models.Repository, authorization string, requireWrite bool) bool {
  469. accessMode := models.AccessModeRead
  470. if requireWrite {
  471. accessMode = models.AccessModeWrite
  472. }
  473. // ctx.IsSigned is unnecessary here, this will be checked in perm.CanAccess
  474. perm, err := models.GetUserRepoPermission(repository, ctx.User)
  475. if err != nil {
  476. log.Error("Unable to GetUserRepoPermission for user %-v in repo %-v Error: %v", ctx.User, repository)
  477. return false
  478. }
  479. canRead := perm.CanAccess(accessMode, models.UnitTypeCode)
  480. if canRead {
  481. return true
  482. }
  483. user, repo, opStr, err := parseToken(authorization)
  484. if err != nil {
  485. // Most of these are Warn level - the true internal server errors are logged in parseToken already
  486. log.Warn("Authentication failure for provided token with Error: %v", err)
  487. return false
  488. }
  489. ctx.User = user
  490. if opStr == "basic" {
  491. perm, err = models.GetUserRepoPermission(repository, ctx.User)
  492. if err != nil {
  493. log.Error("Unable to GetUserRepoPermission for user %-v in repo %-v Error: %v", ctx.User, repository)
  494. return false
  495. }
  496. return perm.CanAccess(accessMode, models.UnitTypeCode)
  497. }
  498. if repository.ID == repo.ID {
  499. if requireWrite && opStr != "upload" {
  500. return false
  501. }
  502. return true
  503. }
  504. return false
  505. }
  506. func parseToken(authorization string) (*models.User, *models.Repository, string, error) {
  507. if authorization == "" {
  508. return nil, nil, "unknown", fmt.Errorf("No token")
  509. }
  510. if strings.HasPrefix(authorization, "Bearer ") {
  511. token, err := jwt.ParseWithClaims(authorization[7:], &Claims{}, func(t *jwt.Token) (interface{}, error) {
  512. if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
  513. return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
  514. }
  515. return setting.LFS.JWTSecretBytes, nil
  516. })
  517. if err != nil {
  518. // The error here is WARN level because it is caused by bad authorization rather than an internal server error
  519. return nil, nil, "unknown", err
  520. }
  521. claims, claimsOk := token.Claims.(*Claims)
  522. if !token.Valid || !claimsOk {
  523. return nil, nil, "unknown", fmt.Errorf("Token claim invalid")
  524. }
  525. r, err := models.GetRepositoryByID(claims.RepoID)
  526. if err != nil {
  527. log.Error("Unable to GetRepositoryById[%d]: Error: %v", claims.RepoID, err)
  528. return nil, nil, claims.Op, err
  529. }
  530. u, err := models.GetUserByID(claims.UserID)
  531. if err != nil {
  532. log.Error("Unable to GetUserById[%d]: Error: %v", claims.UserID, err)
  533. return nil, r, claims.Op, err
  534. }
  535. return u, r, claims.Op, nil
  536. }
  537. if strings.HasPrefix(authorization, "Basic ") {
  538. c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(authorization, "Basic "))
  539. if err != nil {
  540. return nil, nil, "basic", err
  541. }
  542. cs := string(c)
  543. i := strings.IndexByte(cs, ':')
  544. if i < 0 {
  545. return nil, nil, "basic", fmt.Errorf("Basic auth invalid")
  546. }
  547. user, password := cs[:i], cs[i+1:]
  548. u, err := models.GetUserByName(user)
  549. if err != nil {
  550. log.Error("Unable to GetUserByName[%d]: Error: %v", user, err)
  551. return nil, nil, "basic", err
  552. }
  553. if !u.IsPasswordSet() || !u.ValidatePassword(password) {
  554. return nil, nil, "basic", fmt.Errorf("Basic auth failed")
  555. }
  556. return u, nil, "basic", nil
  557. }
  558. return nil, nil, "unknown", fmt.Errorf("Token not found")
  559. }
  560. func requireAuth(ctx *context.Context) {
  561. ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
  562. writeStatus(ctx, 401)
  563. }