|
|
@@ -4,7 +4,7 @@ import (
|
|
|
"net/http"
|
|
|
"strings"
|
|
|
|
|
|
- "gopkg.in/macaron.v1"
|
|
|
+ "github.com/flamego/flamego"
|
|
|
log "unknwon.dev/clog/v2"
|
|
|
|
|
|
"gogs.io/gogs/internal/auth"
|
|
|
@@ -17,7 +17,7 @@ import (
|
|
|
|
|
|
// RegisterRoutes registers LFS routes using given router, and inherits all
|
|
|
// groups and middleware.
|
|
|
-func RegisterRoutes(r *macaron.Router) {
|
|
|
+func RegisterRoutes(r flamego.Router) {
|
|
|
verifyAccept := verifyHeader("Accept", contentType, http.StatusNotAcceptable)
|
|
|
verifyContentTypeJSON := verifyHeader("Content-Type", contentType, http.StatusBadRequest)
|
|
|
verifyContentTypeStream := verifyHeader("Content-Type", "application/octet-stream", http.StatusBadRequest)
|
|
|
@@ -43,7 +43,7 @@ func RegisterRoutes(r *macaron.Router) {
|
|
|
|
|
|
// authenticate tries to authenticate user via HTTP Basic Auth. It first tries to authenticate
|
|
|
// as plain username and password, then use username as access token if previous step failed.
|
|
|
-func authenticate(store Store) macaron.Handler {
|
|
|
+func authenticate(store Store) flamego.Handler {
|
|
|
askCredentials := func(w http.ResponseWriter) {
|
|
|
w.Header().Set("Lfs-Authenticate", `Basic realm="Git LFS"`)
|
|
|
responseJSON(w, http.StatusUnauthorized, responseError{
|
|
|
@@ -51,21 +51,21 @@ func authenticate(store Store) macaron.Handler {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- return func(c *macaron.Context) {
|
|
|
- username, password := authutil.DecodeBasic(c.Req.Header)
|
|
|
+ return func(c flamego.Context) {
|
|
|
+ username, password := authutil.DecodeBasic(c.Request().Header)
|
|
|
if username == "" {
|
|
|
- askCredentials(c.Resp)
|
|
|
+ askCredentials(c.ResponseWriter())
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- user, err := store.AuthenticateUser(c.Req.Context(), username, password, -1)
|
|
|
+ user, err := store.AuthenticateUser(c.Request().Context(), username, password, -1)
|
|
|
if err != nil && !auth.IsErrBadCredentials(err) {
|
|
|
- internalServerError(c.Resp)
|
|
|
+ internalServerError(c.ResponseWriter())
|
|
|
log.Error("Failed to authenticate user [name: %s]: %v", username, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if err == nil && store.IsTwoFactorEnabled(c.Req.Context(), user.ID) {
|
|
|
+ if err == nil && store.IsTwoFactorEnabled(c.Request().Context(), user.ID) {
|
|
|
c.Error(http.StatusBadRequest, "Users with 2FA enabled are not allowed to authenticate via username and password.")
|
|
|
return
|
|
|
}
|
|
|
@@ -73,17 +73,17 @@ func authenticate(store Store) macaron.Handler {
|
|
|
// If username and password combination failed, try again using either username
|
|
|
// or password as the token.
|
|
|
if auth.IsErrBadCredentials(err) {
|
|
|
- user, err = context.AuthenticateByToken(store, c.Req.Context(), username)
|
|
|
+ user, err = context.AuthenticateByToken(store, c.Request().Context(), username)
|
|
|
if err != nil && !database.IsErrAccessTokenNotExist(err) {
|
|
|
- internalServerError(c.Resp)
|
|
|
+ internalServerError(c.ResponseWriter())
|
|
|
log.Error("Failed to authenticate by access token via username: %v", err)
|
|
|
return
|
|
|
} else if database.IsErrAccessTokenNotExist(err) {
|
|
|
// Try again using the password field as the token.
|
|
|
- user, err = context.AuthenticateByToken(store, c.Req.Context(), password)
|
|
|
+ user, err = context.AuthenticateByToken(store, c.Request().Context(), password)
|
|
|
if err != nil {
|
|
|
if database.IsErrAccessTokenNotExist(err) {
|
|
|
- askCredentials(c.Resp)
|
|
|
+ askCredentials(c.ResponseWriter())
|
|
|
} else {
|
|
|
c.Status(http.StatusInternalServerError)
|
|
|
log.Error("Failed to authenticate by access token via password: %v", err)
|
|
|
@@ -100,34 +100,34 @@ func authenticate(store Store) macaron.Handler {
|
|
|
}
|
|
|
|
|
|
// authorize tries to authorize the user to the context repository with given access mode.
|
|
|
-func authorize(store Store, mode database.AccessMode) macaron.Handler {
|
|
|
- return func(c *macaron.Context, actor *database.User) {
|
|
|
- username := c.Params(":username")
|
|
|
- reponame := strings.TrimSuffix(c.Params(":reponame"), ".git")
|
|
|
+func authorize(store Store, mode database.AccessMode) flamego.Handler {
|
|
|
+ return func(c flamego.Context, actor *database.User) {
|
|
|
+ username := c.Param(":username")
|
|
|
+ reponame := strings.TrimSuffix(c.Param(":reponame"), ".git")
|
|
|
|
|
|
- owner, err := store.GetUserByUsername(c.Req.Context(), username)
|
|
|
+ owner, err := store.GetUserByUsername(c.Request().Context(), username)
|
|
|
if err != nil {
|
|
|
if database.IsErrUserNotExist(err) {
|
|
|
c.Status(http.StatusNotFound)
|
|
|
} else {
|
|
|
- internalServerError(c.Resp)
|
|
|
+ internalServerError(c.ResponseWriter())
|
|
|
log.Error("Failed to get user [name: %s]: %v", username, err)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- repo, err := store.GetRepositoryByName(c.Req.Context(), owner.ID, reponame)
|
|
|
+ repo, err := store.GetRepositoryByName(c.Request().Context(), owner.ID, reponame)
|
|
|
if err != nil {
|
|
|
if database.IsErrRepoNotExist(err) {
|
|
|
c.Status(http.StatusNotFound)
|
|
|
} else {
|
|
|
- internalServerError(c.Resp)
|
|
|
+ internalServerError(c.ResponseWriter())
|
|
|
log.Error("Failed to get repository [owner_id: %d, name: %s]: %v", owner.ID, reponame, err)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if !store.AuthorizeRepositoryAccess(c.Req.Context(), actor.ID, repo.ID, mode,
|
|
|
+ if !store.AuthorizeRepositoryAccess(c.Request().Context(), actor.ID, repo.ID, mode,
|
|
|
database.AccessModeOptions{
|
|
|
OwnerID: repo.OwnerID,
|
|
|
Private: repo.IsPrivate,
|
|
|
@@ -146,9 +146,9 @@ func authorize(store Store, mode database.AccessMode) macaron.Handler {
|
|
|
|
|
|
// verifyHeader checks if the HTTP header contains given value.
|
|
|
// When not, response given "failCode" as status code.
|
|
|
-func verifyHeader(key, value string, failCode int) macaron.Handler {
|
|
|
- return func(c *macaron.Context) {
|
|
|
- vals := c.Req.Header.Values(key)
|
|
|
+func verifyHeader(key, value string, failCode int) flamego.Handler {
|
|
|
+ return func(c flamego.Context) {
|
|
|
+ vals := c.Request().Header.Values(key)
|
|
|
for _, val := range vals {
|
|
|
if strings.Contains(val, value) {
|
|
|
return
|
|
|
@@ -161,11 +161,11 @@ func verifyHeader(key, value string, failCode int) macaron.Handler {
|
|
|
}
|
|
|
|
|
|
// verifyOID checks if the ":oid" URL parameter is valid.
|
|
|
-func verifyOID() macaron.Handler {
|
|
|
- return func(c *macaron.Context) {
|
|
|
- oid := lfsutil.OID(c.Params(":oid"))
|
|
|
+func verifyOID() flamego.Handler {
|
|
|
+ return func(c flamego.Context) {
|
|
|
+ oid := lfsutil.OID(c.Param(":oid"))
|
|
|
if !lfsutil.ValidOID(oid) {
|
|
|
- responseJSON(c.Resp, http.StatusBadRequest, responseError{
|
|
|
+ responseJSON(c.ResponseWriter(), http.StatusBadRequest, responseError{
|
|
|
Message: "Invalid oid",
|
|
|
})
|
|
|
return
|