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
Showing the changes to tests/integration/api_httpsig_test.go (1 of 79 files). Show all
Unified · Split
| @@ -10,6 +10,7 @@ import ( · 9 unmodified lines | ||
| 10 | 10 | "net/url" |
| 11 | 11 | "testing" |
| 12 | 12 | |
| 13 | + auth_model "code.gitea.io/gitea/models/auth" | |
| 13 | 14 | api "code.gitea.io/gitea/modules/structs" |
| 14 | 15 | "code.gitea.io/gitea/tests" |
| 15 | 16 | |
| @@ -52,7 +53,7 @@ func TestHTTPSigPubKey(t *testing.T) { · 36 unmodified lines | ||
| 52 | 53 | // Add our public key to user1 |
| 53 | 54 | defer tests.PrepareTestEnv(t)() |
| 54 | 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 | 57 | keysURL := fmt.Sprintf("/api/v1/user/keys?token=%s", token) |
| 57 | 58 | keyType := "ssh-rsa" |
| 58 | 59 | keyContent := "AAAAB3NzaC1yc2EAAAADAQABAAABAQCqOZB5vkRvXFXups1/0StDRdG8plbNSwsWEnNnP4Bvurxa0+z3W9B8GLKnDiLw5MbpbMNyBlpXw13GfuIeciy10DWTz0xUbiy3J3KabCaT36asIw2y7k6Z0jL0UBnrVENwq5/lUbZYqSZ4rRU744wkhh8TULpzM14npQCZwg6aEbG+MwjzddQ72fR+3BPBrKn5dTmmu8rH99O+U+Nuto81Tg7PA+NUupcHOmhdiEGq49plgVFXK98Vks5tiybL4GuzFyWgyX73Dg/QBMn2eMHt1EMv5Gs3i6GFhKKGo4rjDi9qI6PX5oDR4LTNe6cR8td8YhVD8WFZwLLl/vaYyIqd" |