@@ -269,17 +269,32 @@ func tokenRequiresScopes(requiredScopeCategories ...auth_model.AccessTokenScopeC
269269 return
270270 }
271271
272+ ctx .Data ["requiredScopeCategories" ] = requiredScopeCategories
273+
272274 // check if scope only applies to public resources
273275 publicOnly , err := scope .PublicOnly ()
274276 if err != nil {
275277 ctx .Error (http .StatusForbidden , "tokenRequiresScope" , "parsing public resource scope failed: " + err .Error ())
276278 return
277279 }
278280
279- if ! publicOnly {
281+ // assign to true so that those searching should only filter public repositories/users/organizations
282+ ctx .PublicOnly = publicOnly
283+ }
284+ }
285+
286+ func checkTokenPublicOnly () func (ctx * context.APIContext ) {
287+ return func (ctx * context.APIContext ) {
288+ if ! ctx .PublicOnly {
289+ return
290+ }
291+
292+ requiredScopeCategories , ok := ctx .Data ["requiredScopeCategories" ].([]auth_model.AccessTokenScopeCategory )
293+ if ! ok || len (requiredScopeCategories ) == 0 {
280294 return
281295 }
282296
297+ // public Only permission check
283298 switch {
284299 case auth_model .ContainsCategory (requiredScopeCategories , auth_model .AccessTokenScopeCategoryRepository ),
285300 auth_model .ContainsCategory (requiredScopeCategories , auth_model .AccessTokenScopeCategoryIssue ):
@@ -893,11 +908,11 @@ func Routes() *web.Router {
893908 m .Group ("/user/{username}" , func () {
894909 m .Get ("" , activitypub .Person )
895910 m .Post ("/inbox" , activitypub .ReqHTTPSignature (), activitypub .PersonInbox )
896- }, context .UserAssignmentAPI ())
911+ }, context .UserAssignmentAPI (), checkTokenPublicOnly () )
897912 m .Group ("/user-id/{user-id}" , func () {
898913 m .Get ("" , activitypub .Person )
899914 m .Post ("/inbox" , activitypub .ReqHTTPSignature (), activitypub .PersonInbox )
900- }, context .UserIDAssignmentAPI ())
915+ }, context .UserIDAssignmentAPI (), checkTokenPublicOnly () )
901916 }, tokenRequiresScopes (auth_model .AccessTokenScopeCategoryActivityPub ))
902917 }
903918
@@ -926,13 +941,13 @@ func Routes() *web.Router {
926941 // Notifications (requires 'notifications' scope)
927942 m .Group ("/notifications" , func () {
928943 m .Combo ("" ).
929- Get (reqToken (), notify .ListNotifications ).
930- Put (reqToken (), notify .ReadNotifications )
931- m .Get ("/new" , reqToken (), notify .NewAvailable )
944+ Get (notify .ListNotifications ).
945+ Put (notify .ReadNotifications )
946+ m .Get ("/new" , notify .NewAvailable )
932947 m .Combo ("/threads/{id}" ).
933- Get (reqToken (), notify .GetThread ).
934- Patch (reqToken (), notify .ReadThread )
935- }, tokenRequiresScopes (auth_model .AccessTokenScopeCategoryNotification ))
948+ Get (notify .GetThread ).
949+ Patch (notify .ReadThread )
950+ }, reqToken (), tokenRequiresScopes (auth_model .AccessTokenScopeCategoryNotification ))
936951
937952 // Users (requires user scope)
938953 m .Group ("/users" , func () {
@@ -953,8 +968,8 @@ func Routes() *web.Router {
953968 }, reqSelfOrAdmin (), reqBasicOrRevProxyAuth ())
954969
955970 m .Get ("/activities/feeds" , user .ListUserActivityFeeds )
956- }, context .UserAssignmentAPI (), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryUser ), individualPermsChecker )
957- })
971+ }, context .UserAssignmentAPI (), checkTokenPublicOnly ( ), individualPermsChecker )
972+ }, tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryUser ) )
958973
959974 // Users (requires user scope)
960975 m .Group ("/users" , func () {
@@ -971,8 +986,8 @@ func Routes() *web.Router {
971986 m .Get ("/starred" , user .GetStarredRepos )
972987
973988 m .Get ("/subscriptions" , user .GetWatchedRepos )
974- }, reqToken (), context .UserAssignmentAPI (), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryUser ))
975- })
989+ }, context .UserAssignmentAPI (), checkTokenPublicOnly ( ))
990+ }, reqToken (), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryUser ) )
976991
977992 // Users (requires user scope)
978993 m .Group ("/user" , func () {
@@ -1058,8 +1073,8 @@ func Routes() *web.Router {
10581073 m .Get ("" , user .IsStarring )
10591074 m .Put ("" , user .Star )
10601075 m .Delete ("" , user .Unstar )
1061- }, repoAssignment (), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryRepository ))
1062- })
1076+ }, repoAssignment (), checkTokenPublicOnly ( ))
1077+ }, tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryRepository ) )
10631078 m .Get ("/times" , repo .ListMyTrackedTimes )
10641079 m .Get ("/stopwatches" , repo .GetStopwatches )
10651080 m .Get ("/subscriptions" , user .GetMyWatchedRepos )
@@ -1083,7 +1098,7 @@ func Routes() *web.Router {
10831098 m .Get ("" , user .CheckUserBlock )
10841099 m .Put ("" , user .BlockUser )
10851100 m .Delete ("" , user .UnblockUser )
1086- }, context .UserAssignmentAPI ())
1101+ }, context .UserAssignmentAPI (), checkTokenPublicOnly () )
10871102 })
10881103 }, reqToken (), tokenRequiresScopes (auth_model .AccessTokenScopeCategoryUser ))
10891104
@@ -1101,10 +1116,10 @@ func Routes() *web.Router {
11011116
11021117 // Repos (requires repo scope)
11031118 m .Group ("/repos" , func () {
1104- m .Get ("/search" , tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryRepository ), repo .Search )
1119+ m .Get ("/search" , repo .Search )
11051120
11061121 // (repo scope)
1107- m .Post ("/migrate" , reqToken (), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryRepository ), bind (api.MigrateRepoOptions {}), repo .Migrate )
1122+ m .Post ("/migrate" , reqToken (), bind (api.MigrateRepoOptions {}), repo .Migrate )
11081123
11091124 m .Group ("/{username}/{reponame}" , func () {
11101125 m .Get ("/compare/*" , reqRepoReader (unit .TypeCode ), repo .CompareDiff )
@@ -1350,21 +1365,21 @@ func Routes() *web.Router {
13501365 m .Post ("" , bind (api.UpdateRepoAvatarOption {}), repo .UpdateAvatar )
13511366 m .Delete ("" , repo .DeleteAvatar )
13521367 }, reqAdmin (), reqToken ())
1353- }, repoAssignment (), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryRepository ))
1354- })
1368+ }, repoAssignment (), checkTokenPublicOnly ( ))
1369+ }, tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryRepository ) )
13551370
13561371 // Notifications (requires notifications scope)
13571372 m .Group ("/repos" , func () {
13581373 m .Group ("/{username}/{reponame}" , func () {
13591374 m .Combo ("/notifications" , reqToken ()).
13601375 Get (notify .ListRepoNotifications ).
13611376 Put (notify .ReadRepoNotifications )
1362- }, repoAssignment (), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryNotification ))
1363- })
1377+ }, repoAssignment (), checkTokenPublicOnly ( ))
1378+ }, tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryNotification ) )
13641379
13651380 // Issue (requires issue scope)
13661381 m .Group ("/repos" , func () {
1367- m .Get ("/issues/search" , tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryIssue ), repo .SearchIssues )
1382+ m .Get ("/issues/search" , repo .SearchIssues )
13681383
13691384 m .Group ("/{username}/{reponame}" , func () {
13701385 m .Group ("/issues" , func () {
@@ -1473,8 +1488,8 @@ func Routes() *web.Router {
14731488 Patch (reqToken (), reqRepoWriter (unit .TypeIssues , unit .TypePullRequests ), bind (api.EditMilestoneOption {}), repo .EditMilestone ).
14741489 Delete (reqToken (), reqRepoWriter (unit .TypeIssues , unit .TypePullRequests ), repo .DeleteMilestone )
14751490 })
1476- }, repoAssignment (), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryIssue ))
1477- })
1491+ }, repoAssignment (), checkTokenPublicOnly ( ))
1492+ }, tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryIssue ) )
14781493
14791494 // NOTE: these are Gitea package management API - see packages.CommonRoutes and packages.DockerContainerRoutes for endpoints that implement package manager APIs
14801495 m .Group ("/packages/{username}" , func () {
@@ -1484,14 +1499,14 @@ func Routes() *web.Router {
14841499 m .Get ("/files" , reqToken (), packages .ListPackageFiles )
14851500 })
14861501 m .Get ("/" , reqToken (), packages .ListPackages )
1487- }, context .UserAssignmentAPI (), context .PackageAssignmentAPI (), reqPackageAccess (perm .AccessModeRead ), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryPackage ))
1502+ }, tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryPackage ), context .UserAssignmentAPI (), context .PackageAssignmentAPI (), reqPackageAccess (perm .AccessModeRead ), checkTokenPublicOnly ( ))
14881503
14891504 // Organizations
14901505 m .Get ("/user/orgs" , reqToken (), tokenRequiresScopes (auth_model .AccessTokenScopeCategoryUser , auth_model .AccessTokenScopeCategoryOrganization ), org .ListMyOrgs )
14911506 m .Group ("/users/{username}/orgs" , func () {
14921507 m .Get ("" , reqToken (), org .ListUserOrgs )
14931508 m .Get ("/{org}/permissions" , reqToken (), org .GetUserOrgsPermissions )
1494- }, context . UserAssignmentAPI (), tokenRequiresScopes (auth_model .AccessTokenScopeCategoryUser , auth_model .AccessTokenScopeCategoryOrganization ))
1509+ }, tokenRequiresScopes (auth_model .AccessTokenScopeCategoryUser , auth_model .AccessTokenScopeCategoryOrganization ), context . UserAssignmentAPI (), checkTokenPublicOnly ( ))
14951510 m .Post ("/orgs" , tokenRequiresScopes (auth_model .AccessTokenScopeCategoryOrganization ), reqToken (), bind (api.CreateOrgOption {}), org .Create )
14961511 m .Get ("/orgs" , org .GetAll , tokenRequiresScopes (auth_model .AccessTokenScopeCategoryOrganization ))
14971512 m .Group ("/orgs/{org}" , func () {
@@ -1549,7 +1564,7 @@ func Routes() *web.Router {
15491564 m .Delete ("" , org .UnblockUser )
15501565 })
15511566 }, reqToken (), reqOrgOwnership ())
1552- }, orgAssignment (true ), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryOrganization ))
1567+ }, tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryOrganization ), orgAssignment (true ), checkTokenPublicOnly ( ))
15531568 m .Group ("/teams/{teamid}" , func () {
15541569 m .Combo ("" ).Get (reqToken (), org .GetTeam ).
15551570 Patch (reqToken (), reqOrgOwnership (), bind (api.EditTeamOption {}), org .EditTeam ).
@@ -1569,7 +1584,7 @@ func Routes() *web.Router {
15691584 Get (reqToken (), org .GetTeamRepo )
15701585 })
15711586 m .Get ("/activities/feeds" , org .ListTeamActivityFeeds )
1572- }, orgAssignment (false , true ), reqToken (), tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryOrganization ), reqTeamMembership ())
1587+ }, tokenRequiresScopes ( auth_model . AccessTokenScopeCategoryOrganization ), orgAssignment (false , true ), reqToken (), reqTeamMembership ( ), checkTokenPublicOnly ())
15731588
15741589 m .Group ("/admin" , func () {
15751590 m .Group ("/cron" , func () {
0 commit comments