aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/context/permission.go28
1 files changed, 18 insertions, 10 deletions
diff --git a/modules/context/permission.go b/modules/context/permission.go
index cc53fb99ed..0f72b8e244 100644
--- a/modules/context/permission.go
+++ b/modules/context/permission.go
@@ -111,28 +111,36 @@ func RequireRepoReaderOr(unitTypes ...unit.Type) func(ctx *Context) {
}
}
-// RequireRepoScopedToken check whether personal access token has repo scope
-func CheckRepoScopedToken(ctx *Context, repo *repo_model.Repository) {
+// CheckRepoScopedToken check whether personal access token has repo scope
+func CheckRepoScopedToken(ctx *Context, repo *repo_model.Repository, level auth_model.AccessTokenScopeLevel) {
if !ctx.IsBasicAuth || ctx.Data["IsApiToken"] != true {
return
}
- var err error
scope, ok := ctx.Data["ApiTokenScope"].(auth_model.AccessTokenScope)
if ok { // it's a personal access token but not oauth2 token
var scopeMatched bool
- scopeMatched, err = scope.HasScope(auth_model.AccessTokenScopeRepo)
+
+ requiredScopes := auth_model.GetRequiredScopes(level, auth_model.AccessTokenScopeCategoryRepository)
+
+ // check if scope only applies to public resources
+ publicOnly, err := scope.PublicOnly()
if err != nil {
ctx.ServerError("HasScope", err)
return
}
- if !scopeMatched && !repo.IsPrivate {
- scopeMatched, err = scope.HasScope(auth_model.AccessTokenScopePublicRepo)
- if err != nil {
- ctx.ServerError("HasScope", err)
- return
- }
+
+ if publicOnly && repo.IsPrivate {
+ ctx.Error(http.StatusForbidden)
+ return
}
+
+ scopeMatched, err = scope.HasScope(requiredScopes...)
+ if err != nil {
+ ctx.ServerError("HasScope", err)
+ return
+ }
+
if !scopeMatched {
ctx.Error(http.StatusForbidden)
return