2021-06-06 07:59:27 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2020-04-05 14:20:50 +08:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-12-26 09:16:37 +08:00
|
|
|
package lfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2021-06-06 07:59:27 +08:00
|
|
|
"errors"
|
2016-12-26 09:16:37 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2017-11-08 21:04:19 +08:00
|
|
|
"path"
|
2016-12-26 09:16:37 +08:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2021-07-25 00:03:58 +08:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-04-09 06:25:57 +08:00
|
|
|
lfs_module "code.gitea.io/gitea/modules/lfs"
|
2016-12-26 09:16:37 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-11-08 21:04:19 +08:00
|
|
|
|
2021-07-24 19:00:41 +08:00
|
|
|
"github.com/golang-jwt/jwt"
|
2016-12-26 09:16:37 +08:00
|
|
|
)
|
|
|
|
|
2021-04-09 06:25:57 +08:00
|
|
|
// requestContext contain variables from the HTTP request.
|
|
|
|
type requestContext struct {
|
2016-12-26 09:16:37 +08:00
|
|
|
User string
|
|
|
|
Repo string
|
|
|
|
Authorization string
|
|
|
|
}
|
|
|
|
|
2020-03-10 03:56:18 +08:00
|
|
|
// Claims is a JWT Token Claims
|
|
|
|
type Claims struct {
|
|
|
|
RepoID int64
|
|
|
|
Op string
|
|
|
|
UserID int64
|
|
|
|
jwt.StandardClaims
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
// DownloadLink builds a URL to download the object.
|
|
|
|
func (rc *requestContext) DownloadLink(p lfs_module.Pointer) string {
|
|
|
|
return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/objects", p.Oid)
|
2017-11-08 21:04:19 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
// UploadLink builds a URL to upload the object.
|
|
|
|
func (rc *requestContext) UploadLink(p lfs_module.Pointer) string {
|
|
|
|
return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/objects", p.Oid, strconv.FormatInt(p.Size, 10))
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
// VerifyLink builds a URL for verifying the object.
|
|
|
|
func (rc *requestContext) VerifyLink(p lfs_module.Pointer) string {
|
|
|
|
return setting.AppURL + path.Join(rc.User, rc.Repo+".git", "info/lfs/verify")
|
2018-07-19 23:39:19 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
// CheckAcceptMediaType checks if the client accepts the LFS media type.
|
|
|
|
func CheckAcceptMediaType(ctx *context.Context) {
|
|
|
|
mediaParts := strings.Split(ctx.Req.Header.Get("Accept"), ";")
|
2019-05-25 05:21:00 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
if mediaParts[0] != lfs_module.MediaType {
|
|
|
|
log.Trace("Calling a LFS method without accepting the correct media type: %s", lfs_module.MediaType)
|
|
|
|
writeStatus(ctx, http.StatusUnsupportedMediaType)
|
2016-12-26 09:16:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
// DownloadHandler gets the content from the content store
|
|
|
|
func DownloadHandler(ctx *context.Context) {
|
|
|
|
rc := getRequestContext(ctx)
|
|
|
|
p := lfs_module.Pointer{Oid: ctx.Params("oid")}
|
2017-10-30 20:11:56 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
meta := getAuthenticatedMeta(ctx, rc, p, false)
|
2017-10-30 20:11:56 +08:00
|
|
|
if meta == nil {
|
2016-12-26 09:16:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support resume download using Range header
|
2020-05-11 16:37:59 +08:00
|
|
|
var fromByte, toByte int64
|
|
|
|
toByte = meta.Size - 1
|
2021-06-06 07:59:27 +08:00
|
|
|
statusCode := http.StatusOK
|
2016-12-26 09:16:37 +08:00
|
|
|
if rangeHdr := ctx.Req.Header.Get("Range"); rangeHdr != "" {
|
2020-05-11 16:37:59 +08:00
|
|
|
regex := regexp.MustCompile(`bytes=(\d+)\-(\d*).*`)
|
2016-12-26 09:16:37 +08:00
|
|
|
match := regex.FindStringSubmatch(rangeHdr)
|
2019-06-13 03:41:28 +08:00
|
|
|
if len(match) > 1 {
|
2021-06-06 07:59:27 +08:00
|
|
|
statusCode = http.StatusPartialContent
|
2016-12-26 09:16:37 +08:00
|
|
|
fromByte, _ = strconv.ParseInt(match[1], 10, 32)
|
2020-05-11 16:37:59 +08:00
|
|
|
|
2021-04-06 21:22:34 +08:00
|
|
|
if fromByte >= meta.Size {
|
|
|
|
writeStatus(ctx, http.StatusRequestedRangeNotSatisfiable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-11 16:37:59 +08:00
|
|
|
if match[2] != "" {
|
|
|
|
_toByte, _ := strconv.ParseInt(match[2], 10, 32)
|
|
|
|
if _toByte >= fromByte && _toByte < toByte {
|
|
|
|
toByte = _toByte
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Resp.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", fromByte, toByte, meta.Size-fromByte))
|
2020-08-14 01:18:18 +08:00
|
|
|
ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Range")
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 06:25:57 +08:00
|
|
|
contentStore := lfs_module.NewContentStore()
|
|
|
|
content, err := contentStore.Get(meta.Pointer)
|
2016-12-26 09:16:37 +08:00
|
|
|
if err != nil {
|
2021-04-06 21:22:34 +08:00
|
|
|
writeStatus(ctx, http.StatusNotFound)
|
2016-12-26 09:16:37 +08:00
|
|
|
return
|
|
|
|
}
|
2020-03-10 03:56:18 +08:00
|
|
|
defer content.Close()
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-04-06 21:22:34 +08:00
|
|
|
if fromByte > 0 {
|
|
|
|
_, err = content.Seek(fromByte, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Whilst trying to read LFS OID[%s]: Unable to seek to %d Error: %v", meta.Oid, fromByte, err)
|
|
|
|
|
|
|
|
writeStatus(ctx, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 16:37:59 +08:00
|
|
|
contentLength := toByte + 1 - fromByte
|
|
|
|
ctx.Resp.Header().Set("Content-Length", strconv.FormatInt(contentLength, 10))
|
2016-12-26 09:16:37 +08:00
|
|
|
ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
|
|
|
|
filename := ctx.Params("filename")
|
|
|
|
if len(filename) > 0 {
|
|
|
|
decodedFilename, err := base64.RawURLEncoding.DecodeString(filename)
|
|
|
|
if err == nil {
|
|
|
|
ctx.Resp.Header().Set("Content-Disposition", "attachment; filename=\""+string(decodedFilename)+"\"")
|
2020-08-14 01:18:18 +08:00
|
|
|
ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Resp.WriteHeader(statusCode)
|
2020-05-11 16:37:59 +08:00
|
|
|
if written, err := io.CopyN(ctx.Resp, content, contentLength); err != nil {
|
2020-03-10 03:56:18 +08:00
|
|
|
log.Error("Error whilst copying LFS OID[%s] to the response after %d bytes. Error: %v", meta.Oid, written, err)
|
|
|
|
}
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
// BatchHandler provides the batch api
|
|
|
|
func BatchHandler(ctx *context.Context) {
|
|
|
|
var br lfs_module.BatchRequest
|
|
|
|
if err := decodeJSON(ctx.Req, &br); err != nil {
|
|
|
|
log.Trace("Unable to decode BATCH request vars: Error: %v", err)
|
|
|
|
writeStatus(ctx, http.StatusBadRequest)
|
2018-05-01 09:46:04 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
var isUpload bool
|
|
|
|
if br.Operation == "upload" {
|
|
|
|
isUpload = true
|
|
|
|
} else if br.Operation == "download" {
|
|
|
|
isUpload = false
|
|
|
|
} else {
|
|
|
|
log.Trace("Attempt to BATCH with invalid operation: %s", br.Operation)
|
|
|
|
writeStatus(ctx, http.StatusBadRequest)
|
2018-05-01 09:46:04 +08:00
|
|
|
return
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
rc := getRequestContext(ctx)
|
2020-02-28 12:46:57 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
repository := getAuthenticatedRepository(ctx, rc, isUpload)
|
|
|
|
if repository == nil {
|
2016-12-26 09:16:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-09 06:25:57 +08:00
|
|
|
contentStore := lfs_module.NewContentStore()
|
|
|
|
|
|
|
|
var responseObjects []*lfs_module.ObjectResponse
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
for _, p := range br.Objects {
|
|
|
|
if !p.IsValid() {
|
|
|
|
responseObjects = append(responseObjects, buildObjectResponse(rc, p, false, false, &lfs_module.ObjectError{
|
|
|
|
Code: http.StatusUnprocessableEntity,
|
|
|
|
Message: "Oid or size are invalid",
|
|
|
|
}))
|
2018-07-19 23:39:19 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
exists, err := contentStore.Exists(p)
|
2016-12-26 09:16:37 +08:00
|
|
|
if err != nil {
|
2021-06-06 07:59:27 +08:00
|
|
|
log.Error("Unable to check if LFS OID[%s] exist. Error: %v", p.Oid, rc.User, rc.Repo, err)
|
|
|
|
writeStatus(ctx, http.StatusInternalServerError)
|
2016-12-26 09:16:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
meta, err := repository.GetLFSMetaObjectByOid(p.Oid)
|
|
|
|
if err != nil && err != models.ErrLFSObjectNotExist {
|
|
|
|
log.Error("Unable to get LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err)
|
|
|
|
writeStatus(ctx, http.StatusInternalServerError)
|
2016-12-26 09:16:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
if meta != nil && p.Size != meta.Size {
|
|
|
|
responseObjects = append(responseObjects, buildObjectResponse(rc, p, false, false, &lfs_module.ObjectError{
|
|
|
|
Code: http.StatusUnprocessableEntity,
|
|
|
|
Message: fmt.Sprintf("Object %s is not %d bytes", p.Oid, p.Size),
|
|
|
|
}))
|
|
|
|
continue
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
var responseObject *lfs_module.ObjectResponse
|
|
|
|
if isUpload {
|
|
|
|
var err *lfs_module.ObjectError
|
|
|
|
if !exists && setting.LFS.MaxFileSize > 0 && p.Size > setting.LFS.MaxFileSize {
|
|
|
|
err = &lfs_module.ObjectError{
|
|
|
|
Code: http.StatusUnprocessableEntity,
|
|
|
|
Message: fmt.Sprintf("Size must be less than or equal to %d", setting.LFS.MaxFileSize),
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 04:57:27 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
if exists {
|
|
|
|
if meta == nil {
|
|
|
|
_, err := models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: p, RepositoryID: repository.ID})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to create LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err)
|
|
|
|
writeStatus(ctx, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 23:45:10 +08:00
|
|
|
}
|
2021-06-06 07:59:27 +08:00
|
|
|
|
|
|
|
responseObject = buildObjectResponse(rc, p, false, !exists, err)
|
2020-03-10 03:56:18 +08:00
|
|
|
} else {
|
2021-06-06 07:59:27 +08:00
|
|
|
var err *lfs_module.ObjectError
|
|
|
|
if !exists || meta == nil {
|
|
|
|
err = &lfs_module.ObjectError{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
Message: http.StatusText(http.StatusNotFound),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
responseObject = buildObjectResponse(rc, p, true, false, err)
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
2021-06-06 07:59:27 +08:00
|
|
|
responseObjects = append(responseObjects, responseObject)
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-04-09 06:25:57 +08:00
|
|
|
respobj := &lfs_module.BatchResponse{Objects: responseObjects}
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
|
|
|
|
|
2021-07-25 00:03:58 +08:00
|
|
|
enc := json.NewEncoder(ctx.Resp)
|
2020-03-10 03:56:18 +08:00
|
|
|
if err := enc.Encode(respobj); err != nil {
|
|
|
|
log.Error("Failed to encode representation as json. Error: %v", err)
|
|
|
|
}
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
// UploadHandler receives data from the client and puts it into the content store
|
|
|
|
func UploadHandler(ctx *context.Context) {
|
|
|
|
rc := getRequestContext(ctx)
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
p := lfs_module.Pointer{Oid: ctx.Params("oid")}
|
|
|
|
var err error
|
|
|
|
if p.Size, err = strconv.ParseInt(ctx.Params("size"), 10, 64); err != nil {
|
|
|
|
writeStatusMessage(ctx, http.StatusUnprocessableEntity, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.IsValid() {
|
|
|
|
log.Trace("Attempt to access invalid LFS OID[%s] in %s/%s", p.Oid, rc.User, rc.Repo)
|
|
|
|
writeStatus(ctx, http.StatusUnprocessableEntity)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
repository := getAuthenticatedRepository(ctx, rc, true)
|
|
|
|
if repository == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: p, RepositoryID: repository.ID})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to create LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err)
|
|
|
|
writeStatus(ctx, http.StatusInternalServerError)
|
2016-12-26 09:16:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-09 06:25:57 +08:00
|
|
|
contentStore := lfs_module.NewContentStore()
|
2021-06-06 07:59:27 +08:00
|
|
|
|
|
|
|
exists, err := contentStore.Exists(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to check if LFS OID[%s] exist. Error: %v", p.Oid, err)
|
|
|
|
writeStatus(ctx, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if meta.Existing || exists {
|
|
|
|
ctx.Resp.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 23:36:53 +08:00
|
|
|
defer ctx.Req.Body.Close()
|
2021-04-09 06:25:57 +08:00
|
|
|
if err := contentStore.Put(meta.Pointer, ctx.Req.Body); err != nil {
|
2021-06-06 07:59:27 +08:00
|
|
|
if errors.Is(err, lfs_module.ErrSizeMismatch) || errors.Is(err, lfs_module.ErrHashMismatch) {
|
|
|
|
writeStatusMessage(ctx, http.StatusUnprocessableEntity, err.Error())
|
2020-03-10 03:56:18 +08:00
|
|
|
} else {
|
2021-06-06 07:59:27 +08:00
|
|
|
writeStatus(ctx, http.StatusInternalServerError)
|
2020-03-10 03:56:18 +08:00
|
|
|
}
|
2021-04-09 06:25:57 +08:00
|
|
|
if _, err = repository.RemoveLFSMetaObjectByOid(p.Oid); err != nil {
|
2021-06-06 07:59:27 +08:00
|
|
|
log.Error("Error whilst removing metaobject for LFS OID[%s]: %v", p.Oid, err)
|
2017-10-30 20:11:56 +08:00
|
|
|
}
|
2016-12-26 09:16:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
writeStatus(ctx, http.StatusOK)
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2017-11-08 21:04:19 +08:00
|
|
|
// VerifyHandler verify oid and its size from the content store
|
|
|
|
func VerifyHandler(ctx *context.Context) {
|
2021-06-06 07:59:27 +08:00
|
|
|
var p lfs_module.Pointer
|
|
|
|
if err := decodeJSON(ctx.Req, &p); err != nil {
|
|
|
|
writeStatus(ctx, http.StatusUnprocessableEntity)
|
2017-11-08 21:04:19 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
rc := getRequestContext(ctx)
|
2017-11-08 21:04:19 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
meta := getAuthenticatedMeta(ctx, rc, p, true)
|
2017-11-08 21:04:19 +08:00
|
|
|
if meta == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-09 06:25:57 +08:00
|
|
|
contentStore := lfs_module.NewContentStore()
|
|
|
|
ok, err := contentStore.Verify(meta.Pointer)
|
2021-06-06 07:59:27 +08:00
|
|
|
|
|
|
|
status := http.StatusOK
|
2017-11-08 21:04:19 +08:00
|
|
|
if err != nil {
|
2021-06-06 07:59:27 +08:00
|
|
|
status = http.StatusInternalServerError
|
|
|
|
} else if !ok {
|
|
|
|
status = http.StatusNotFound
|
2017-11-08 21:04:19 +08:00
|
|
|
}
|
2021-06-06 07:59:27 +08:00
|
|
|
writeStatus(ctx, status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeJSON(req *http.Request, v interface{}) error {
|
|
|
|
defer req.Body.Close()
|
2017-11-08 21:04:19 +08:00
|
|
|
|
2021-07-25 00:03:58 +08:00
|
|
|
dec := json.NewDecoder(req.Body)
|
2021-06-06 07:59:27 +08:00
|
|
|
return dec.Decode(v)
|
2017-11-08 21:04:19 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
func getRequestContext(ctx *context.Context) *requestContext {
|
|
|
|
return &requestContext{
|
|
|
|
User: ctx.Params("username"),
|
|
|
|
Repo: strings.TrimSuffix(ctx.Params("reponame"), ".git"),
|
|
|
|
Authorization: ctx.Req.Header.Get("Authorization"),
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
2021-06-06 07:59:27 +08:00
|
|
|
}
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
func getAuthenticatedMeta(ctx *context.Context, rc *requestContext, p lfs_module.Pointer, requireWrite bool) *models.LFSMetaObject {
|
|
|
|
if !p.IsValid() {
|
|
|
|
log.Info("Attempt to access invalid LFS OID[%s] in %s/%s", p.Oid, rc.User, rc.Repo)
|
|
|
|
writeStatusMessage(ctx, http.StatusUnprocessableEntity, "Oid or size are invalid")
|
|
|
|
return nil
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
repository := getAuthenticatedRepository(ctx, rc, requireWrite)
|
|
|
|
if repository == nil {
|
|
|
|
return nil
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
meta, err := repository.GetLFSMetaObjectByOid(p.Oid)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to get LFS OID[%s] Error: %v", p.Oid, err)
|
|
|
|
writeStatus(ctx, http.StatusNotFound)
|
|
|
|
return nil
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
return meta
|
|
|
|
}
|
2019-05-25 05:21:00 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
func getAuthenticatedRepository(ctx *context.Context, rc *requestContext, requireWrite bool) *models.Repository {
|
|
|
|
repository, err := models.GetRepositoryByOwnerAndName(rc.User, rc.Repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to get repository: %s/%s Error: %v", rc.User, rc.Repo, err)
|
|
|
|
writeStatus(ctx, http.StatusNotFound)
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-25 05:21:00 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
if !authenticate(ctx, repository, rc.Authorization, false, requireWrite) {
|
|
|
|
requireAuth(ctx)
|
|
|
|
return nil
|
2017-11-08 21:04:19 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
return repository
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
func buildObjectResponse(rc *requestContext, pointer lfs_module.Pointer, download, upload bool, err *lfs_module.ObjectError) *lfs_module.ObjectResponse {
|
|
|
|
rep := &lfs_module.ObjectResponse{Pointer: pointer}
|
|
|
|
if err != nil {
|
|
|
|
rep.Error = err
|
|
|
|
} else {
|
|
|
|
rep.Actions = make(map[string]*lfs_module.Link)
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
header := make(map[string]string)
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
if len(rc.Authorization) > 0 {
|
|
|
|
header["Authorization"] = rc.Authorization
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
if download {
|
|
|
|
rep.Actions["download"] = &lfs_module.Link{Href: rc.DownloadLink(pointer), Header: header}
|
|
|
|
}
|
|
|
|
if upload {
|
|
|
|
rep.Actions["upload"] = &lfs_module.Link{Href: rc.UploadLink(pointer), Header: header}
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
verifyHeader := make(map[string]string)
|
|
|
|
for key, value := range header {
|
|
|
|
verifyHeader[key] = value
|
|
|
|
}
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
// This is only needed to workaround https://github.com/git-lfs/git-lfs/issues/3662
|
|
|
|
verifyHeader["Accept"] = lfs_module.MediaType
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
rep.Actions["verify"] = &lfs_module.Link{Href: rc.VerifyLink(pointer), Header: verifyHeader}
|
|
|
|
}
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
2021-06-06 07:59:27 +08:00
|
|
|
return rep
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeStatus(ctx *context.Context, status int) {
|
2021-06-06 07:59:27 +08:00
|
|
|
writeStatusMessage(ctx, status, http.StatusText(status))
|
|
|
|
}
|
2016-12-26 09:16:37 +08:00
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
func writeStatusMessage(ctx *context.Context, status int, message string) {
|
|
|
|
ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
|
2016-12-26 09:16:37 +08:00
|
|
|
ctx.Resp.WriteHeader(status)
|
|
|
|
|
2021-06-06 07:59:27 +08:00
|
|
|
er := lfs_module.ErrorResponse{Message: message}
|
|
|
|
|
2021-07-25 00:03:58 +08:00
|
|
|
enc := json.NewEncoder(ctx.Resp)
|
2021-06-06 07:59:27 +08:00
|
|
|
if err := enc.Encode(er); err != nil {
|
|
|
|
log.Error("Failed to encode error response as json. Error: %v", err)
|
|
|
|
}
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// authenticate uses the authorization string to determine whether
|
|
|
|
// or not to proceed. This server assumes an HTTP Basic auth format.
|
2021-05-15 23:32:09 +08:00
|
|
|
func authenticate(ctx *context.Context, repository *models.Repository, authorization string, requireSigned, requireWrite bool) bool {
|
2016-12-26 09:16:37 +08:00
|
|
|
accessMode := models.AccessModeRead
|
|
|
|
if requireWrite {
|
|
|
|
accessMode = models.AccessModeWrite
|
|
|
|
}
|
|
|
|
|
2019-01-31 21:36:57 +08:00
|
|
|
// ctx.IsSigned is unnecessary here, this will be checked in perm.CanAccess
|
2018-11-28 19:26:14 +08:00
|
|
|
perm, err := models.GetUserRepoPermission(repository, ctx.User)
|
|
|
|
if err != nil {
|
2020-03-10 03:56:18 +08:00
|
|
|
log.Error("Unable to GetUserRepoPermission for user %-v in repo %-v Error: %v", ctx.User, repository)
|
2018-11-28 19:26:14 +08:00
|
|
|
return false
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
2019-01-31 21:36:57 +08:00
|
|
|
|
|
|
|
canRead := perm.CanAccess(accessMode, models.UnitTypeCode)
|
2021-05-15 23:32:09 +08:00
|
|
|
if canRead && (!requireSigned || ctx.IsSigned) {
|
2019-01-31 21:36:57 +08:00
|
|
|
return true
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-15 23:32:09 +08:00
|
|
|
user, err := parseToken(authorization, repository, accessMode)
|
2016-12-26 09:16:37 +08:00
|
|
|
if err != nil {
|
2020-03-10 03:56:18 +08:00
|
|
|
// Most of these are Warn level - the true internal server errors are logged in parseToken already
|
|
|
|
log.Warn("Authentication failure for provided token with Error: %v", err)
|
2016-12-26 09:16:37 +08:00
|
|
|
return false
|
|
|
|
}
|
2018-01-28 00:48:15 +08:00
|
|
|
ctx.User = user
|
2021-05-15 23:32:09 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLFSToken(tokenSHA string, target *models.Repository, mode models.AccessMode) (*models.User, error) {
|
|
|
|
if !strings.Contains(tokenSHA, ".") {
|
|
|
|
return nil, nil
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
2021-05-15 23:32:09 +08:00
|
|
|
token, err := jwt.ParseWithClaims(tokenSHA, &Claims{}, func(t *jwt.Token) (interface{}, error) {
|
|
|
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
|
2018-01-28 00:48:15 +08:00
|
|
|
}
|
2021-05-15 23:32:09 +08:00
|
|
|
return setting.LFS.JWTSecretBytes, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-15 23:32:09 +08:00
|
|
|
claims, claimsOk := token.Claims.(*Claims)
|
|
|
|
if !token.Valid || !claimsOk {
|
|
|
|
return nil, fmt.Errorf("invalid token claim")
|
2018-01-28 00:48:15 +08:00
|
|
|
}
|
2021-05-15 23:32:09 +08:00
|
|
|
|
|
|
|
if claims.RepoID != target.ID {
|
|
|
|
return nil, fmt.Errorf("invalid token claim")
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-15 23:32:09 +08:00
|
|
|
if mode == models.AccessModeWrite && claims.Op != "upload" {
|
|
|
|
return nil, fmt.Errorf("invalid token claim")
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-15 23:32:09 +08:00
|
|
|
u, err := models.GetUserByID(claims.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to GetUserById[%d]: Error: %v", claims.UserID, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseToken(authorization string, target *models.Repository, mode models.AccessMode) (*models.User, error) {
|
|
|
|
if authorization == "" {
|
|
|
|
return nil, fmt.Errorf("no token")
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(authorization, " ", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, fmt.Errorf("no token")
|
|
|
|
}
|
|
|
|
tokenSHA := parts[1]
|
|
|
|
switch strings.ToLower(parts[0]) {
|
|
|
|
case "bearer":
|
|
|
|
fallthrough
|
|
|
|
case "token":
|
|
|
|
return handleLFSToken(tokenSHA, target, mode)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("token not found")
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func requireAuth(ctx *context.Context) {
|
|
|
|
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
|
2021-06-06 07:59:27 +08:00
|
|
|
writeStatus(ctx, http.StatusUnauthorized)
|
2016-12-26 09:16:37 +08:00
|
|
|
}
|