Skip to content

Commit eaca81f

Browse files
Loïc Dacharyearl-warren
authored andcommitted
[BRANDING] X-Forgejo-OTP can be used instead of X-Gitea-OTP
(cherry picked from commit 7b0549cd70aa7cafec853e15b25270847c59850b) (cherry picked from commit 13e10a65d974c7b594681bfa36402a6144862116) (cherry picked from commit 89982e6c4a7f9cf7024b2db3ed14b2b79db29064) (cherry picked from commit a4acf6343d9f5c6dedeb261c524cd7ac5ae1b3c1) (cherry picked from commit 9886aec9f8b09b58c73c55598a2017417a51843d) (cherry picked from commit 1ee9bd7549eaa094f5cfa9636a89d8f13766ccc8) (cherry picked from commit f343cf5597d666f937c582677f4d62ac2137dc4e)
1 parent 0545142 commit eaca81f

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

modules/context/api.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,20 @@ func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
188188
}
189189
}
190190

191+
func getOtpHeader(header http.Header) string {
192+
otpHeader := header.Get("X-Gitea-OTP")
193+
if forgejoHeader := header.Get("X-Forgejo-OTP"); forgejoHeader != "" {
194+
otpHeader = forgejoHeader
195+
}
196+
return otpHeader
197+
}
198+
191199
// CheckForOTP validates OTP
192200
func (ctx *APIContext) CheckForOTP() {
193201
if skip, ok := ctx.Data["SkipLocalTwoFA"]; ok && skip.(bool) {
194202
return // Skip 2FA
195203
}
196204

197-
otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
198205
twofa, err := auth.GetTwoFactorByUID(ctx.Context.Doer.ID)
199206
if err != nil {
200207
if auth.IsErrTwoFactorNotEnrolled(err) {
@@ -203,7 +210,7 @@ func (ctx *APIContext) CheckForOTP() {
203210
ctx.Context.Error(http.StatusInternalServerError)
204211
return
205212
}
206-
ok, err := twofa.ValidateTOTP(otpHeader)
213+
ok, err := twofa.ValidateTOTP(getOtpHeader(ctx.Req.Header))
207214
if err != nil {
208215
ctx.Context.Error(http.StatusInternalServerError)
209216
return

modules/context/api_forgejo_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
package context
4+
5+
import (
6+
"net/http"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestGetOtpHeader(t *testing.T) {
13+
header := http.Header{}
14+
assert.EqualValues(t, "", getOtpHeader(header))
15+
// Gitea
16+
giteaOtp := "123456"
17+
header.Set("X-Gitea-OTP", giteaOtp)
18+
assert.EqualValues(t, giteaOtp, getOtpHeader(header))
19+
// Forgejo has precedence
20+
forgejoOtp := "abcdef"
21+
header.Set("X-Forgejo-OTP", forgejoOtp)
22+
assert.EqualValues(t, forgejoOtp, getOtpHeader(header))
23+
}

routers/api/v1/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
// description: Sudo API request as the user provided as the key. Admin privileges are required.
5757
// TOTPHeader:
5858
// type: apiKey
59-
// name: X-GITEA-OTP
59+
// name: X-FORGEJO-OTP
6060
// in: header
6161
// description: Must be used in combination with BasicAuth if two-factor authentication is enabled.
6262
//

templates/swagger/v1_json.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21018,7 +21018,7 @@
2101821018
"TOTPHeader": {
2101921019
"description": "Must be used in combination with BasicAuth if two-factor authentication is enabled.",
2102021020
"type": "apiKey",
21021-
"name": "X-GITEA-OTP",
21021+
"name": "X-FORGEJO-OTP",
2102221022
"in": "header"
2102321023
},
2102421024
"Token": {

0 commit comments

Comments
 (0)