Skip to content

Commit a84f19d

Browse files
authored
Merge branch 'main' into main
2 parents 0c7e9f9 + 8e2dd5d commit a84f19d

File tree

6 files changed

+32
-25
lines changed

6 files changed

+32
-25
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ INTERNAL_TOKEN =
526526
;; HMAC to encode urls with, it **is required** if camo is enabled.
527527
;HMAC_KEY =
528528
;; Set to true to use camo for https too lese only non https urls are proxyed
529-
;ALLWAYS = false
529+
;; ALLWAYS is deprecated and will be removed in the future
530+
;ALWAYS = false
530531

531532
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
532533
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

modules/markup/camo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func camoHandleLink(link string) string {
3838
if setting.Camo.Enabled {
3939
lnkURL, err := url.Parse(link)
4040
if err == nil && lnkURL.IsAbs() && !strings.HasPrefix(link, setting.AppURL) &&
41-
(setting.Camo.Allways || lnkURL.Scheme != "https") {
41+
(setting.Camo.Always || lnkURL.Scheme != "https") {
4242
return CamoEncode(link)
4343
}
4444
}

modules/markup/camo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestCamoHandleLink(t *testing.T) {
2828
"https://image.proxy/eivin43gJwGVIjR9MiYYtFIk0mw/aHR0cDovL3Rlc3RpbWFnZXMub3JnL2ltZy5qcGc",
2929
camoHandleLink("http://testimages.org/img.jpg"))
3030

31-
setting.Camo.Allways = true
31+
setting.Camo.Always = true
3232
assert.Equal(t,
3333
"https://gitea.com/img.jpg",
3434
camoHandleLink("https://gitea.com/img.jpg"))

modules/setting/camo.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33

44
package setting
55

6-
import "code.gitea.io/gitea/modules/log"
6+
import (
7+
"strconv"
8+
9+
"code.gitea.io/gitea/modules/log"
10+
)
711

812
var Camo = struct {
913
Enabled bool
1014
ServerURL string `ini:"SERVER_URL"`
1115
HMACKey string `ini:"HMAC_KEY"`
12-
Allways bool
16+
Always bool
1317
}{}
1418

1519
func loadCamoFrom(rootCfg ConfigProvider) {
1620
mustMapSetting(rootCfg, "camo", &Camo)
1721
if Camo.Enabled {
22+
oldValue := rootCfg.Section("camo").Key("ALLWAYS").MustString("")
23+
if oldValue != "" {
24+
log.Warn("camo.ALLWAYS is deprecated, use camo.ALWAYS instead")
25+
Camo.Always, _ = strconv.ParseBool(oldValue)
26+
}
27+
1828
if Camo.ServerURL == "" || Camo.HMACKey == "" {
1929
log.Fatal(`Camo settings require "SERVER_URL" and HMAC_KEY`)
2030
}

routers/api/v1/user/app.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ func CreateAccessToken(ctx *context.APIContext) {
118118
ctx.Error(http.StatusBadRequest, "AccessTokenScope.Normalize", fmt.Errorf("invalid access token scope provided: %w", err))
119119
return
120120
}
121+
if scope == "" {
122+
ctx.Error(http.StatusBadRequest, "AccessTokenScope", "access token must have a scope")
123+
return
124+
}
121125
t.Scope = scope
122126

123127
if err := auth_model.NewAccessToken(ctx, t); err != nil {
@@ -129,6 +133,7 @@ func CreateAccessToken(ctx *context.APIContext) {
129133
Token: t.Token,
130134
ID: t.ID,
131135
TokenLastEight: t.TokenLastEight,
136+
Scopes: t.Scope.StringSlice(),
132137
})
133138
}
134139

tests/integration/api_token_test.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func TestAPICreateAndDeleteToken(t *testing.T) {
2323
defer tests.PrepareTestEnv(t)()
2424
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
2525

26-
newAccessToken := createAPIAccessTokenWithoutCleanUp(t, "test-key-1", user, nil)
26+
newAccessToken := createAPIAccessTokenWithoutCleanUp(t, "test-key-1", user, []auth_model.AccessTokenScope{auth_model.AccessTokenScopeAll})
2727
deleteAPIAccessToken(t, newAccessToken, user)
2828

29-
newAccessToken = createAPIAccessTokenWithoutCleanUp(t, "test-key-2", user, nil)
29+
newAccessToken = createAPIAccessTokenWithoutCleanUp(t, "test-key-2", user, []auth_model.AccessTokenScope{auth_model.AccessTokenScopeAll})
3030
deleteAPIAccessToken(t, newAccessToken, user)
3131
}
3232

@@ -72,19 +72,19 @@ func TestAPIDeleteTokensPermission(t *testing.T) {
7272
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
7373

7474
// admin can delete tokens for other users
75-
createAPIAccessTokenWithoutCleanUp(t, "test-key-1", user2, nil)
75+
createAPIAccessTokenWithoutCleanUp(t, "test-key-1", user2, []auth_model.AccessTokenScope{auth_model.AccessTokenScopeAll})
7676
req := NewRequest(t, "DELETE", "/api/v1/users/"+user2.LoginName+"/tokens/test-key-1").
7777
AddBasicAuth(admin.Name)
7878
MakeRequest(t, req, http.StatusNoContent)
7979

8080
// non-admin can delete tokens for himself
81-
createAPIAccessTokenWithoutCleanUp(t, "test-key-2", user2, nil)
81+
createAPIAccessTokenWithoutCleanUp(t, "test-key-2", user2, []auth_model.AccessTokenScope{auth_model.AccessTokenScopeAll})
8282
req = NewRequest(t, "DELETE", "/api/v1/users/"+user2.LoginName+"/tokens/test-key-2").
8383
AddBasicAuth(user2.Name)
8484
MakeRequest(t, req, http.StatusNoContent)
8585

8686
// non-admin can't delete tokens for other users
87-
createAPIAccessTokenWithoutCleanUp(t, "test-key-3", user2, nil)
87+
createAPIAccessTokenWithoutCleanUp(t, "test-key-3", user2, []auth_model.AccessTokenScope{auth_model.AccessTokenScopeAll})
8888
req = NewRequest(t, "DELETE", "/api/v1/users/"+user2.LoginName+"/tokens/test-key-3").
8989
AddBasicAuth(user4.Name)
9090
MakeRequest(t, req, http.StatusForbidden)
@@ -520,7 +520,7 @@ func runTestCase(t *testing.T, testCase *requiredScopeTestCase, user *user_model
520520
unauthorizedScopes = append(unauthorizedScopes, cateogoryUnauthorizedScopes...)
521521
}
522522

523-
accessToken := createAPIAccessTokenWithoutCleanUp(t, "test-token", user, &unauthorizedScopes)
523+
accessToken := createAPIAccessTokenWithoutCleanUp(t, "test-token", user, unauthorizedScopes)
524524
defer deleteAPIAccessToken(t, accessToken, user)
525525

526526
// Request the endpoint. Verify that permission is denied.
@@ -532,20 +532,12 @@ func runTestCase(t *testing.T, testCase *requiredScopeTestCase, user *user_model
532532

533533
// createAPIAccessTokenWithoutCleanUp Create an API access token and assert that
534534
// creation succeeded. The caller is responsible for deleting the token.
535-
func createAPIAccessTokenWithoutCleanUp(t *testing.T, tokenName string, user *user_model.User, scopes *[]auth_model.AccessTokenScope) api.AccessToken {
535+
func createAPIAccessTokenWithoutCleanUp(t *testing.T, tokenName string, user *user_model.User, scopes []auth_model.AccessTokenScope) api.AccessToken {
536536
payload := map[string]any{
537-
"name": tokenName,
538-
}
539-
if scopes != nil {
540-
for _, scope := range *scopes {
541-
scopes, scopesExists := payload["scopes"].([]string)
542-
if !scopesExists {
543-
scopes = make([]string, 0)
544-
}
545-
scopes = append(scopes, string(scope))
546-
payload["scopes"] = scopes
547-
}
537+
"name": tokenName,
538+
"scopes": scopes,
548539
}
540+
549541
log.Debug("Requesting creation of token with scopes: %v", scopes)
550542
req := NewRequestWithJSON(t, "POST", "/api/v1/users/"+user.LoginName+"/tokens", payload).
551543
AddBasicAuth(user.Name)
@@ -563,8 +555,7 @@ func createAPIAccessTokenWithoutCleanUp(t *testing.T, tokenName string, user *us
563555
return newAccessToken
564556
}
565557

566-
// createAPIAccessTokenWithoutCleanUp Delete an API access token and assert that
567-
// deletion succeeded.
558+
// deleteAPIAccessToken deletes an API access token and assert that deletion succeeded.
568559
func deleteAPIAccessToken(t *testing.T, accessToken api.AccessToken, user *user_model.User) {
569560
req := NewRequestf(t, "DELETE", "/api/v1/users/"+user.LoginName+"/tokens/%d", accessToken.ID).
570561
AddBasicAuth(user.Name)

0 commit comments

Comments
 (0)