Skip to content

Commit b00f7c3

Browse files
GiteaBotharryzcy
andauthored
Fix access token issue on some public endpoints (#24194) (#24259)
Backport #24194 by @harryzcy - [x] Identify endpoints that should be public - [x] Update integration tests Fix #24159 Co-authored-by: harryzcy <[email protected]>
1 parent 51fd730 commit b00f7c3

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

routers/api/v1/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,21 +1178,21 @@ func Routes(ctx gocontext.Context) *web.Route {
11781178
m.Get("/{org}/permissions", reqToken(auth_model.AccessTokenScopeReadOrg), org.GetUserOrgsPermissions)
11791179
}, context_service.UserAssignmentAPI())
11801180
m.Post("/orgs", reqToken(auth_model.AccessTokenScopeWriteOrg), bind(api.CreateOrgOption{}), org.Create)
1181-
m.Get("/orgs", reqToken(auth_model.AccessTokenScopeReadOrg), org.GetAll)
1181+
m.Get("/orgs", org.GetAll)
11821182
m.Group("/orgs/{org}", func() {
1183-
m.Combo("").Get(reqToken(auth_model.AccessTokenScopeReadOrg), org.Get).
1183+
m.Combo("").Get(org.Get).
11841184
Patch(reqToken(auth_model.AccessTokenScopeWriteOrg), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
11851185
Delete(reqToken(auth_model.AccessTokenScopeWriteOrg), reqOrgOwnership(), org.Delete)
1186-
m.Combo("/repos").Get(reqToken(auth_model.AccessTokenScopeReadOrg), user.ListOrgRepos).
1186+
m.Combo("/repos").Get(user.ListOrgRepos).
11871187
Post(reqToken(auth_model.AccessTokenScopeWriteOrg), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
11881188
m.Group("/members", func() {
11891189
m.Get("", reqToken(auth_model.AccessTokenScopeReadOrg), org.ListMembers)
11901190
m.Combo("/{username}").Get(reqToken(auth_model.AccessTokenScopeReadOrg), org.IsMember).
11911191
Delete(reqToken(auth_model.AccessTokenScopeWriteOrg), reqOrgOwnership(), org.DeleteMember)
11921192
})
11931193
m.Group("/public_members", func() {
1194-
m.Get("", reqToken(auth_model.AccessTokenScopeReadOrg), org.ListPublicMembers)
1195-
m.Combo("/{username}").Get(reqToken(auth_model.AccessTokenScopeReadOrg), org.IsPublicMember).
1194+
m.Get("", org.ListPublicMembers)
1195+
m.Combo("/{username}").Get(org.IsPublicMember).
11961196
Put(reqToken(auth_model.AccessTokenScopeWriteOrg), reqOrgMembership(), org.PublicizeMember).
11971197
Delete(reqToken(auth_model.AccessTokenScopeWriteOrg), reqOrgMembership(), org.ConcealMember)
11981198
})
@@ -1202,7 +1202,7 @@ func Routes(ctx gocontext.Context) *web.Route {
12021202
m.Get("/search", reqToken(auth_model.AccessTokenScopeReadOrg), org.SearchTeam)
12031203
}, reqOrgMembership())
12041204
m.Group("/labels", func() {
1205-
m.Get("", reqToken(auth_model.AccessTokenScopeReadOrg), org.ListLabels)
1205+
m.Get("", org.ListLabels)
12061206
m.Post("", reqToken(auth_model.AccessTokenScopeWriteOrg), reqOrgOwnership(), bind(api.CreateLabelOption{}), org.CreateLabel)
12071207
m.Combo("/{id}").Get(reqToken(auth_model.AccessTokenScopeReadOrg), org.GetLabel).
12081208
Patch(reqToken(auth_model.AccessTokenScopeWriteOrg), reqOrgOwnership(), bind(api.EditLabelOption{}), org.EditLabel).

tests/integration/api_org_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,14 @@ func TestAPIOrgDeny(t *testing.T) {
127127
setting.Service.RequireSignInView = false
128128
}()
129129

130-
token := getUserToken(t, "user1", auth_model.AccessTokenScopeReadOrg)
131-
132130
orgName := "user1_org"
133-
req := NewRequestf(t, "GET", "/api/v1/orgs/%s?token=%s", orgName, token)
131+
req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName)
134132
MakeRequest(t, req, http.StatusNotFound)
135133

136-
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos?token=%s", orgName, token)
134+
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", orgName)
137135
MakeRequest(t, req, http.StatusNotFound)
138136

139-
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members?token=%s", orgName, token)
137+
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", orgName)
140138
MakeRequest(t, req, http.StatusNotFound)
141139
})
142140
}
@@ -146,16 +144,24 @@ func TestAPIGetAll(t *testing.T) {
146144

147145
token := getUserToken(t, "user1", auth_model.AccessTokenScopeReadOrg)
148146

147+
// accessing with a token will return all orgs
149148
req := NewRequestf(t, "GET", "/api/v1/orgs?token=%s", token)
150149
resp := MakeRequest(t, req, http.StatusOK)
151-
152150
var apiOrgList []*api.Organization
153-
DecodeJSON(t, resp, &apiOrgList)
154151

155-
// accessing with a token will return all orgs
152+
DecodeJSON(t, resp, &apiOrgList)
156153
assert.Len(t, apiOrgList, 9)
157154
assert.Equal(t, "org25", apiOrgList[1].FullName)
158155
assert.Equal(t, "public", apiOrgList[1].Visibility)
156+
157+
// accessing without a token will return only public orgs
158+
req = NewRequestf(t, "GET", "/api/v1/orgs")
159+
resp = MakeRequest(t, req, http.StatusOK)
160+
161+
DecodeJSON(t, resp, &apiOrgList)
162+
assert.Len(t, apiOrgList, 7)
163+
assert.Equal(t, "org25", apiOrgList[0].FullName)
164+
assert.Equal(t, "public", apiOrgList[0].Visibility)
159165
}
160166

161167
func TestAPIOrgSearchEmptyTeam(t *testing.T) {

0 commit comments

Comments
 (0)