Skip to content

Commit 97d30ec

Browse files
committed
preconfigure git-credential-oauth
1 parent 2d7fe4c commit 97d30ec

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@ ENABLE = true
544544
;;
545545
;; Maximum length of oauth2 token/cookie stored on server
546546
;MAX_TOKEN_LENGTH = 32767
547+
;;
548+
;; Register OAuth applications for Git credential helpers
549+
;GIT_CREDENTIAL_HELPERS = true
547550

548551
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
549552
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ This section only does "set" config, a removed config key from this section won'
10991099
- `JWT_SECRET_URI`: **_empty_**: Instead of defining JWT_SECRET in the configuration, this configuration option can be used to give Gitea a path to a file that contains the secret (example value: `file:/etc/gitea/oauth2_jwt_secret`)
11001100
- `JWT_SIGNING_PRIVATE_KEY_FILE`: **jwt/private.pem**: Private key file path used to sign OAuth2 tokens. The path is relative to `APP_DATA_PATH`. This setting is only needed if `JWT_SIGNING_ALGORITHM` is set to `RS256`, `RS384`, `RS512`, `ES256`, `ES384` or `ES512`. The file must contain a RSA or ECDSA private key in the PKCS8 format. If no key exists a 4096 bit key will be created for you.
11011101
- `MAX_TOKEN_LENGTH`: **32767**: Maximum length of token/cookie to accept from OAuth2 provider
1102+
- `GIT_CREDENTIAL_HELPERS`: **true**: Register OAuth applications for Git credential helpers at startup.
11021103

11031104
## i18n (`i18n`)
11041105

models/auth/oauth2.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"code.gitea.io/gitea/models/db"
16+
"code.gitea.io/gitea/modules/setting"
1617
"code.gitea.io/gitea/modules/timeutil"
1718
"code.gitea.io/gitea/modules/util"
1819

@@ -46,6 +47,26 @@ func init() {
4647
db.RegisterModel(new(OAuth2Grant))
4748
}
4849

50+
func Init(ctx context.Context) error {
51+
if setting.OAuth2.GitCredentialHelpers {
52+
// the following Git credential helpers are universally useful
53+
// https://git-scm.com/doc/credential-helpers
54+
_ = db.Insert(ctx, []OAuth2Application{
55+
{
56+
Name: "git-credential-oauth",
57+
ClientID: "a4792ccc-144e-407e-86c9-5e7d8d9c3269",
58+
RedirectURIs: []string{"http://127.0.0.1", "https://127.0.0.1"},
59+
},
60+
{
61+
Name: "Git Credential Manager",
62+
ClientID: "e90ee53c-94e2-48ac-9358-a874fb9e0662",
63+
RedirectURIs: []string{"http://127.0.0.1", "https://127.0.0.1"},
64+
},
65+
})
66+
}
67+
return nil
68+
}
69+
4970
// TableName sets the table name to `oauth2_application`
5071
func (app *OAuth2Application) TableName() string {
5172
return "oauth2_application"

modules/setting/oauth2.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ var OAuth2 = struct {
100100
JWTSecretBase64 string `ini:"JWT_SECRET"`
101101
JWTSigningPrivateKeyFile string `ini:"JWT_SIGNING_PRIVATE_KEY_FILE"`
102102
MaxTokenLength int
103+
GitCredentialHelpers bool
103104
}{
104105
Enable: true,
105106
AccessTokenExpirationTime: 3600,
@@ -108,6 +109,7 @@ var OAuth2 = struct {
108109
JWTSigningAlgorithm: "RS256",
109110
JWTSigningPrivateKeyFile: "jwt/private.pem",
110111
MaxTokenLength: math.MaxInt16,
112+
GitCredentialHelpers: true,
111113
}
112114

113115
func loadOAuth2From(rootCfg ConfigProvider) {

routers/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"code.gitea.io/gitea/models"
1212
asymkey_model "code.gitea.io/gitea/models/asymkey"
13+
authmodel "code.gitea.io/gitea/models/auth"
1314
"code.gitea.io/gitea/modules/cache"
1415
"code.gitea.io/gitea/modules/eventsource"
1516
"code.gitea.io/gitea/modules/git"
@@ -138,6 +139,7 @@ func InitWebInstalled(ctx context.Context) {
138139
mustInit(oauth2.Init)
139140

140141
mustInitCtx(ctx, models.Init)
142+
mustInitCtx(ctx, authmodel.Init)
141143
mustInit(repo_service.Init)
142144

143145
// Booting long running goroutines.

routers/web/repo/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func httpBase(ctx *context.Context) *serviceHandler {
147147
// rely on the results of Contexter
148148
if !ctx.IsSigned {
149149
// TODO: support digit auth - which would be Authorization header with digit
150-
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=\".\"")
150+
ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="Gitea"`)
151151
ctx.Error(http.StatusUnauthorized)
152152
return nil
153153
}

0 commit comments

Comments
 (0)