2018-11-28 19:26:14 +08:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
|
package context
|
|
|
|
|
|
|
|
|
|
import (
|
2023-04-26 19:24:03 -05:00
|
|
|
"net/http"
|
2025-06-18 03:48:09 +02:00
|
|
|
"slices"
|
2023-04-26 19:24:03 -05:00
|
|
|
|
|
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2018-11-28 19:26:14 +08:00
|
|
|
)
|
|
|
|
|
|
2021-01-29 23:35:30 +08:00
|
|
|
// RequireRepoAdmin returns a middleware for requiring repository admin permission
|
2021-01-26 23:36:53 +08:00
|
|
|
func RequireRepoAdmin() func(ctx *Context) {
|
2018-11-28 19:26:14 +08:00
|
|
|
return func(ctx *Context) {
|
|
|
|
|
if !ctx.IsSigned || !ctx.Repo.IsAdmin() {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2022-04-28 17:45:33 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 23:35:34 -08:00
|
|
|
// CanWriteToBranch checks if the user is allowed to write to the branch of the repo
|
|
|
|
|
func CanWriteToBranch() func(ctx *Context) {
|
2022-04-28 17:45:33 +02:00
|
|
|
return func(ctx *Context) {
|
2023-07-22 22:14:27 +08:00
|
|
|
if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2018-11-28 19:26:14 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-14 09:53:34 +08:00
|
|
|
// RequireUnitWriter returns a middleware for requiring repository write to one of the unit permission
|
|
|
|
|
func RequireUnitWriter(unitTypes ...unit.Type) func(ctx *Context) {
|
2018-11-28 19:26:14 +08:00
|
|
|
return func(ctx *Context) {
|
2025-06-18 03:48:09 +02:00
|
|
|
if slices.ContainsFunc(unitTypes, ctx.Repo.CanWrite) {
|
|
|
|
|
return
|
2018-11-28 19:26:14 +08:00
|
|
|
}
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2018-11-28 19:26:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-14 09:53:34 +08:00
|
|
|
// RequireUnitReader returns a middleware for requiring repository write to one of the unit permission
|
|
|
|
|
func RequireUnitReader(unitTypes ...unit.Type) func(ctx *Context) {
|
2018-11-28 19:26:14 +08:00
|
|
|
return func(ctx *Context) {
|
|
|
|
|
for _, unitType := range unitTypes {
|
|
|
|
|
if ctx.Repo.CanRead(unitType) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-01-14 09:53:34 +08:00
|
|
|
if unitType == unit.TypeCode && canWriteAsMaintainer(ctx) {
|
|
|
|
|
return
|
2019-04-22 21:40:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2018-11-28 19:26:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-26 19:24:03 -05:00
|
|
|
|
2023-06-04 14:57:16 -04:00
|
|
|
// CheckRepoScopedToken check whether personal access token has repo scope
|
|
|
|
|
func CheckRepoScopedToken(ctx *Context, repo *repo_model.Repository, level auth_model.AccessTokenScopeLevel) {
|
2023-04-26 19:24:03 -05:00
|
|
|
if !ctx.IsBasicAuth || ctx.Data["IsApiToken"] != true {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope, ok := ctx.Data["ApiTokenScope"].(auth_model.AccessTokenScope)
|
|
|
|
|
if ok { // it's a personal access token but not oauth2 token
|
|
|
|
|
var scopeMatched bool
|
2023-06-04 14:57:16 -04:00
|
|
|
|
|
|
|
|
requiredScopes := auth_model.GetRequiredScopes(level, auth_model.AccessTokenScopeCategoryRepository)
|
|
|
|
|
|
|
|
|
|
// check if scope only applies to public resources
|
|
|
|
|
publicOnly, err := scope.PublicOnly()
|
2023-04-26 19:24:03 -05:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("HasScope", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-06-04 14:57:16 -04:00
|
|
|
|
|
|
|
|
if publicOnly && repo.IsPrivate {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusForbidden)
|
2023-06-04 14:57:16 -04:00
|
|
|
return
|
2023-04-26 19:24:03 -05:00
|
|
|
}
|
2023-06-04 14:57:16 -04:00
|
|
|
|
|
|
|
|
scopeMatched, err = scope.HasScope(requiredScopes...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("HasScope", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 19:24:03 -05:00
|
|
|
if !scopeMatched {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusForbidden)
|
2023-04-26 19:24:03 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|