Skip to content

Commit 2bde83b

Browse files
committed
DISABLE_ACCESS_TOKENS parameter for disabling access tokens added
Access tokens are hardcoded and cannot be disabled (i.e. when owner doesn't want this kind of authentication). This mod introduces new DISABLE_ACCESS_TOKENS parameter in app.ini section [security]. When disabled (default when parameter is not present) gitea behaves as without this mod (access tokens feature is available). When enabled, access tokens feature and its UI elements are not avaiable. This mod also hides those areas on Settings/Applications page that are disabled in config and hides menu link to Applications page if all its areas are disabled in config. Related: go-gitea#13129 Author-Change-Id: IB#1115254
1 parent b7c6457 commit 2bde83b

File tree

7 files changed

+34
-5
lines changed

7 files changed

+34
-5
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ INTERNAL_TOKEN=
372372
;; Set to true to disable webhooks feature.
373373
;DISABLE_WEBHOOKS = false
374374
;;
375+
;; Set to false to disable access tokens feature.
376+
;DISABLE_ACCESS_TOKENS = false
377+
;;
375378
;; Set to false to allow pushes to gitea repositories despite having an incomplete environment - NOT RECOMMENDED
376379
;ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET = true
377380
;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ Certain queues have defaults that override the defaults set in `[queue]` (this o
498498
Gitea instance and perform arbitrary actions in the name of the Gitea OS user.
499499
This maybe harmful to you website or your operating system.
500500
- `DISABLE_WEBHOOKS`: **false**: Set to `true` to disable webhooks feature.
501+
- `DISABLE_ACCESS_TOKENS`: **false**: Set to `true` to disable access tokens feature.
501502
- `ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET`: **true**: Set to `false` to allow local users to push to gitea-repositories without setting up the Gitea environment. This is not recommended and if you want local users to push to Gitea repositories you should set the environment appropriately.
502503
- `IMPORT_LOCAL_PATHS`: **false**: Set to `false` to prevent all users (including admin) from importing local path on server.
503504
- `INTERNAL_TOKEN`: **\<random at every install if no uri set\>**: Secret used to validate communication within Gitea binary.

modules/setting/setting.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ var (
187187
ImportLocalPaths bool
188188
DisableGitHooks bool
189189
DisableWebhooks bool
190+
DisableAccessTokens bool
190191
OnlyAllowPushIfGiteaEnvironmentSet bool
191192
PasswordComplexity []string
192193
PasswordHashAlgo string
@@ -868,6 +869,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
868869
ImportLocalPaths = sec.Key("IMPORT_LOCAL_PATHS").MustBool(false)
869870
DisableGitHooks = sec.Key("DISABLE_GIT_HOOKS").MustBool(true)
870871
DisableWebhooks = sec.Key("DISABLE_WEBHOOKS").MustBool(false)
872+
DisableAccessTokens = sec.Key("DISABLE_ACCESS_TOKENS").MustBool(false)
871873
OnlyAllowPushIfGiteaEnvironmentSet = sec.Key("ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET").MustBool(true)
872874
PasswordHashAlgo = sec.Key("PASSWORD_HASH_ALGO").MustString("pbkdf2")
873875
CSRFCookieHTTPOnly = sec.Key("CSRF_COOKIE_HTTP_ONLY").MustBool(true)

modules/templates/helper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ func NewFuncMap() []template.FuncMap {
236236
"DisableWebhooks": func() bool {
237237
return setting.DisableWebhooks
238238
},
239+
"DisableAccessTokens": func() bool {
240+
return setting.DisableAccessTokens
241+
},
242+
"DisableOAuth2": func() bool {
243+
return !setting.OAuth2.Enable
244+
},
239245
"DisableImportLocal": func() bool {
240246
return !setting.ImportLocalPaths
241247
},

routers/web/user/setting/applications.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package setting
77

88
import (
9+
"fmt"
910
"net/http"
1011

1112
"code.gitea.io/gitea/models"
@@ -44,6 +45,11 @@ func ApplicationsPost(ctx *context.Context) {
4445
return
4546
}
4647

48+
if setting.DisableAccessTokens {
49+
ctx.ServerError("AccessToken", fmt.Errorf("cannot modify access token; access tokens disabled"))
50+
return
51+
}
52+
4753
t := &models.AccessToken{
4854
UID: ctx.User.ID,
4955
Name: form.Name,
@@ -73,6 +79,10 @@ func ApplicationsPost(ctx *context.Context) {
7379

7480
// DeleteApplication response for delete user access token
7581
func DeleteApplication(ctx *context.Context) {
82+
if setting.DisableAccessTokens {
83+
ctx.ServerError("DeleteAccessToken", fmt.Errorf("cannot delete access token; access tokens disabled"))
84+
return
85+
}
7686
if err := models.DeleteAccessTokenByID(ctx.FormInt64("id"), ctx.User.ID); err != nil {
7787
ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
7888
} else {
@@ -85,14 +95,17 @@ func DeleteApplication(ctx *context.Context) {
8595
}
8696

8797
func loadApplicationsData(ctx *context.Context) {
88-
tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID})
89-
if err != nil {
90-
ctx.ServerError("ListAccessTokens", err)
91-
return
98+
if setting.DisableAccessTokens {
99+
tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID})
100+
if err != nil {
101+
ctx.ServerError("ListAccessTokens", err)
102+
return
103+
}
104+
ctx.Data["Tokens"] = tokens
92105
}
93-
ctx.Data["Tokens"] = tokens
94106
ctx.Data["EnableOAuth2"] = setting.OAuth2.Enable
95107
if setting.OAuth2.Enable {
108+
var err error
96109
ctx.Data["Applications"], err = auth.GetOAuth2ApplicationsByUserID(ctx.User.ID)
97110
if err != nil {
98111
ctx.ServerError("GetOAuth2ApplicationsByUserID", err)

templates/user/settings/applications.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{{template "user/settings/navbar" .}}
44
<div class="ui container">
55
{{template "base/alert" .}}
6+
{{if not DisableAccessTokens}}
67
<h4 class="ui top attached header">
78
{{.i18n.Tr "settings.manage_access_token"}}
89
</h4>
@@ -46,6 +47,7 @@
4647
</button>
4748
</form>
4849
</div>
50+
{{end}}
4951

5052
{{if .EnableOAuth2}}
5153
{{template "user/settings/grants_oauth2" .}}

templates/user/settings/navbar.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
<a class="{{if .PageIsSettingsSecurity}}active{{end}} item" href="{{AppSubUrl}}/user/settings/security">
1313
{{.i18n.Tr "settings.security"}}
1414
</a>
15+
{{if or (not DisableAccessTokens) (not DisableOAuth2)}}
1516
<a class="{{if .PageIsSettingsApplications}}active{{end}} item" href="{{AppSubUrl}}/user/settings/applications">
1617
{{.i18n.Tr "settings.applications"}}
1718
</a>
19+
{{end}}
1820
<a class="{{if .PageIsSettingsKeys}}active{{end}} item" href="{{AppSubUrl}}/user/settings/keys">
1921
{{.i18n.Tr "settings.ssh_gpg_keys"}}
2022
</a>

0 commit comments

Comments
 (0)