History

Support scoped access tokens (#20908)

This PR adds the support for scopes of access tokens, mimicking the design of GitHub OAuth scopes. The changes of the core logic are in `models/auth` that `AccessToken` struct will have a `Scope` field. The normalized (no duplication of scope), comma-separated scope string will be stored in `access_token` table in the database. In `services/auth`, the scope will be stored in context, which will be used by `reqToken` middleware in API calls. Only OAuth2 tokens will have granular token scopes, while others like BasicAuth will default to scope `all`. A large amount of work happens in `routers/api/v1/api.go` and the corresponding `tests/integration` tests, that is adding necessary scopes to each of the API calls as they fit. - [x] Add `Scope` field to `AccessToken` - [x] Add access control to all API endpoints - [x] Update frontend & backend for when creating tokens - [x] Add a database migration for `scope` column (enable 'all' access to past tokens) I'm aiming to complete it before Gitea 1.19 release. Fixes #4300

Chongyi Zheng committed 3 years ago

Commit de484e81 parent db2286b79 files · +1220 −448Browse files

Showing the changes to tests/integration/api_httpsig_test.go (1 of 79 files). Show all

Unified · Split

tests/integration/api_httpsig_test.go+2−1
@@ -10,6 +10,7 @@ import ( · 9 unmodified lines
10 "net/url"10 "net/url"
11 "testing"11 "testing"
12 12
13 auth_model "code.gitea.io/gitea/models/auth"
13 api "code.gitea.io/gitea/modules/structs"14 api "code.gitea.io/gitea/modules/structs"
14 "code.gitea.io/gitea/tests"15 "code.gitea.io/gitea/tests"
15 16
@@ -52,7 +53,7 @@ func TestHTTPSigPubKey(t *testing.T) { · 36 unmodified lines
52 // Add our public key to user153 // Add our public key to user1
53 defer tests.PrepareTestEnv(t)()54 defer tests.PrepareTestEnv(t)()
54 session := loginUser(t, "user1")55 session := loginUser(t, "user1")
55 token := url.QueryEscape(getTokenForLoggedInUser(t, session))56 token := url.QueryEscape(getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAdminPublicKey, auth_model.AccessTokenScopeSudo))
56 keysURL := fmt.Sprintf("/api/v1/user/keys?token=%s", token)57 keysURL := fmt.Sprintf("/api/v1/user/keys?token=%s", token)
57 keyType := "ssh-rsa"58 keyType := "ssh-rsa"
58 keyContent := "AAAAB3NzaC1yc2EAAAADAQABAAABAQCqOZB5vkRvXFXups1/0StDRdG8plbNSwsWEnNnP4Bvurxa0+z3W9B8GLKnDiLw5MbpbMNyBlpXw13GfuIeciy10DWTz0xUbiy3J3KabCaT36asIw2y7k6Z0jL0UBnrVENwq5/lUbZYqSZ4rRU744wkhh8TULpzM14npQCZwg6aEbG+MwjzddQ72fR+3BPBrKn5dTmmu8rH99O+U+Nuto81Tg7PA+NUupcHOmhdiEGq49plgVFXK98Vks5tiybL4GuzFyWgyX73Dg/QBMn2eMHt1EMv5Gs3i6GFhKKGo4rjDi9qI6PX5oDR4LTNe6cR8td8YhVD8WFZwLLl/vaYyIqd"59 keyContent := "AAAAB3NzaC1yc2EAAAADAQABAAABAQCqOZB5vkRvXFXups1/0StDRdG8plbNSwsWEnNnP4Bvurxa0+z3W9B8GLKnDiLw5MbpbMNyBlpXw13GfuIeciy10DWTz0xUbiy3J3KabCaT36asIw2y7k6Z0jL0UBnrVENwq5/lUbZYqSZ4rRU744wkhh8TULpzM14npQCZwg6aEbG+MwjzddQ72fR+3BPBrKn5dTmmu8rH99O+U+Nuto81Tg7PA+NUupcHOmhdiEGq49plgVFXK98Vks5tiybL4GuzFyWgyX73Dg/QBMn2eMHt1EMv5Gs3i6GFhKKGo4rjDi9qI6PX5oDR4LTNe6cR8td8YhVD8WFZwLLl/vaYyIqd"