Skip to content

Commit e9fc57f

Browse files
committed
Fix build
1 parent d0719d0 commit e9fc57f

File tree

10 files changed

+60
-61
lines changed

10 files changed

+60
-61
lines changed

models/login/main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ func TestMain(m *testing.M) {
1717
"oauth2_application.yml",
1818
"oauth2_authorization_code.yml",
1919
"oauth2_grant.yml",
20+
"u2f_registration.yml",
2021
)
2122
}

models/login/twofactor.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,12 @@ func (t *TwoFactor) GenerateScratchToken() (string, error) {
6363
return "", err
6464
}
6565
t.ScratchSalt, _ = util.RandomString(10)
66-
t.ScratchHash = hashToken(token, t.ScratchSalt)
66+
t.ScratchHash = HashToken(token, t.ScratchSalt)
6767
return token, nil
6868
}
6969

7070
// HashToken return the hashable salt
7171
func HashToken(token, salt string) string {
72-
return hashToken(token, salt)
73-
}
74-
75-
func hashToken(token, salt string) string {
7672
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
7773
return fmt.Sprintf("%x", tempHash)
7874
}
@@ -82,7 +78,7 @@ func (t *TwoFactor) VerifyScratchToken(token string) bool {
8278
if len(token) == 0 {
8379
return false
8480
}
85-
tempHash := hashToken(token, t.ScratchSalt)
81+
tempHash := HashToken(token, t.ScratchSalt)
8682
return subtle.ConstantTimeCompare([]byte(t.ScratchHash), []byte(tempHash)) == 1
8783
}
8884

modules/context/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"code.gitea.io/gitea/models"
17+
"code.gitea.io/gitea/models/login"
1718
"code.gitea.io/gitea/modules/git"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/setting"
@@ -219,9 +220,9 @@ func (ctx *APIContext) CheckForOTP() {
219220
}
220221

221222
otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
222-
twofa, err := models.GetTwoFactorByUID(ctx.Context.User.ID)
223+
twofa, err := login.GetTwoFactorByUID(ctx.Context.User.ID)
223224
if err != nil {
224-
if models.IsErrTwoFactorNotEnrolled(err) {
225+
if login.IsErrTwoFactorNotEnrolled(err) {
225226
return // No 2FA enrollment for this user
226227
}
227228
ctx.Context.Error(http.StatusInternalServerError)

modules/context/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package context
88
import (
99
"net/http"
1010

11-
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/models/login"
1212
"code.gitea.io/gitea/modules/log"
1313
"code.gitea.io/gitea/modules/setting"
1414
"code.gitea.io/gitea/modules/web/middleware"
@@ -154,9 +154,9 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
154154
if skip, ok := ctx.Data["SkipLocalTwoFA"]; ok && skip.(bool) {
155155
return // Skip 2FA
156156
}
157-
twofa, err := models.GetTwoFactorByUID(ctx.User.ID)
157+
twofa, err := login.GetTwoFactorByUID(ctx.User.ID)
158158
if err != nil {
159-
if models.IsErrTwoFactorNotEnrolled(err) {
159+
if login.IsErrTwoFactorNotEnrolled(err) {
160160
return // No 2FA enrollment for this user
161161
}
162162
ctx.InternalServerError(err)

routers/web/admin/users.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ func prepareUserInfo(ctx *context.Context) *models.User {
195195
ctx.Data["Sources"] = sources
196196

197197
ctx.Data["TwoFactorEnabled"] = true
198-
_, err = models.GetTwoFactorByUID(u.ID)
198+
_, err = login.GetTwoFactorByUID(u.ID)
199199
if err != nil {
200-
if !models.IsErrTwoFactorNotEnrolled(err) {
200+
if !login.IsErrTwoFactorNotEnrolled(err) {
201201
ctx.ServerError("IsErrTwoFactorNotEnrolled", err)
202202
return nil
203203
}
@@ -295,13 +295,13 @@ func EditUserPost(ctx *context.Context) {
295295
}
296296

297297
if form.Reset2FA {
298-
tf, err := models.GetTwoFactorByUID(u.ID)
299-
if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
298+
tf, err := login.GetTwoFactorByUID(u.ID)
299+
if err != nil && !login.IsErrTwoFactorNotEnrolled(err) {
300300
ctx.ServerError("GetTwoFactorByUID", err)
301301
return
302302
}
303303

304-
if err = models.DeleteTwoFactorByID(tf.ID, u.ID); err != nil {
304+
if err = login.DeleteTwoFactorByID(tf.ID, u.ID); err != nil {
305305
ctx.ServerError("DeleteTwoFactorByID", err)
306306
return
307307
}

routers/web/repo/http.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"code.gitea.io/gitea/models"
24+
"code.gitea.io/gitea/models/login"
2425
"code.gitea.io/gitea/modules/context"
2526
"code.gitea.io/gitea/modules/git"
2627
"code.gitea.io/gitea/modules/log"
@@ -174,12 +175,12 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
174175
}
175176

176177
if ctx.IsBasicAuth && ctx.Data["IsApiToken"] != true {
177-
_, err = models.GetTwoFactorByUID(ctx.User.ID)
178+
_, err = login.GetTwoFactorByUID(ctx.User.ID)
178179
if err == nil {
179180
// TODO: This response should be changed to "invalid credentials" for security reasons once the expectation behind it (creating an app token to authenticate) is properly documented
180181
ctx.HandleText(http.StatusUnauthorized, "Users with two-factor authentication enabled cannot perform HTTP/HTTPS operations via plain username and password. Please create and use a personal access token on the user settings page")
181182
return
182-
} else if !models.IsErrTwoFactorNotEnrolled(err) {
183+
} else if !login.IsErrTwoFactorNotEnrolled(err) {
183184
ctx.ServerError("IsErrTwoFactorNotEnrolled", err)
184185
return
185186
}

routers/web/user/auth.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ func SignInPost(ctx *context.Context) {
213213

214214
// If this user is enrolled in 2FA, we can't sign the user in just yet.
215215
// Instead, redirect them to the 2FA authentication page.
216-
_, err = models.GetTwoFactorByUID(u.ID)
216+
_, err = login.GetTwoFactorByUID(u.ID)
217217
if err != nil {
218-
if models.IsErrTwoFactorNotEnrolled(err) {
218+
if login.IsErrTwoFactorNotEnrolled(err) {
219219
handleSignIn(ctx, u, form.Remember)
220220
} else {
221221
ctx.ServerError("UserSignIn", err)
@@ -237,7 +237,7 @@ func SignInPost(ctx *context.Context) {
237237
return
238238
}
239239

240-
regs, err := models.GetU2FRegistrationsByUID(u.ID)
240+
regs, err := login.GetU2FRegistrationsByUID(u.ID)
241241
if err == nil && len(regs) > 0 {
242242
ctx.Redirect(setting.AppSubURL + "/user/u2f")
243243
return
@@ -277,7 +277,7 @@ func TwoFactorPost(ctx *context.Context) {
277277
}
278278

279279
id := idSess.(int64)
280-
twofa, err := models.GetTwoFactorByUID(id)
280+
twofa, err := login.GetTwoFactorByUID(id)
281281
if err != nil {
282282
ctx.ServerError("UserSignIn", err)
283283
return
@@ -313,7 +313,7 @@ func TwoFactorPost(ctx *context.Context) {
313313
}
314314

315315
twofa.LastUsedPasscode = form.Passcode
316-
if err = models.UpdateTwoFactor(twofa); err != nil {
316+
if err = login.UpdateTwoFactor(twofa); err != nil {
317317
ctx.ServerError("UserSignIn", err)
318318
return
319319
}
@@ -356,7 +356,7 @@ func TwoFactorScratchPost(ctx *context.Context) {
356356
}
357357

358358
id := idSess.(int64)
359-
twofa, err := models.GetTwoFactorByUID(id)
359+
twofa, err := login.GetTwoFactorByUID(id)
360360
if err != nil {
361361
ctx.ServerError("UserSignIn", err)
362362
return
@@ -370,7 +370,7 @@ func TwoFactorScratchPost(ctx *context.Context) {
370370
ctx.ServerError("UserSignIn", err)
371371
return
372372
}
373-
if err = models.UpdateTwoFactor(twofa); err != nil {
373+
if err = login.UpdateTwoFactor(twofa); err != nil {
374374
ctx.ServerError("UserSignIn", err)
375375
return
376376
}
@@ -418,7 +418,7 @@ func U2FChallenge(ctx *context.Context) {
418418
return
419419
}
420420
id := idSess.(int64)
421-
regs, err := models.GetU2FRegistrationsByUID(id)
421+
regs, err := login.GetU2FRegistrationsByUID(id)
422422
if err != nil {
423423
ctx.ServerError("UserSignIn", err)
424424
return
@@ -454,7 +454,7 @@ func U2FSign(ctx *context.Context) {
454454
}
455455
challenge := challSess.(*u2f.Challenge)
456456
id := idSess.(int64)
457-
regs, err := models.GetU2FRegistrationsByUID(id)
457+
regs, err := login.GetU2FRegistrationsByUID(id)
458458
if err != nil {
459459
ctx.ServerError("UserSignIn", err)
460460
return
@@ -717,8 +717,8 @@ func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *models.Us
717717

718718
needs2FA := false
719719
if !source.Cfg.(*oauth2.Source).SkipLocalTwoFA {
720-
_, err := models.GetTwoFactorByUID(u.ID)
721-
if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
720+
_, err := login.GetTwoFactorByUID(u.ID)
721+
if err != nil && !login.IsErrTwoFactorNotEnrolled(err) {
722722
ctx.ServerError("UserSignIn", err)
723723
return
724724
}
@@ -775,7 +775,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *models.Us
775775
}
776776

777777
// If U2F is enrolled -> Redirect to U2F instead
778-
regs, err := models.GetU2FRegistrationsByUID(u.ID)
778+
regs, err := login.GetU2FRegistrationsByUID(u.ID)
779779
if err == nil && len(regs) > 0 {
780780
ctx.Redirect(setting.AppSubURL + "/user/u2f")
781781
return
@@ -935,9 +935,9 @@ func linkAccount(ctx *context.Context, u *models.User, gothUser goth.User, remem
935935
// If this user is enrolled in 2FA, we can't sign the user in just yet.
936936
// Instead, redirect them to the 2FA authentication page.
937937
// We deliberately ignore the skip local 2fa setting here because we are linking to a previous user here
938-
_, err := models.GetTwoFactorByUID(u.ID)
938+
_, err := login.GetTwoFactorByUID(u.ID)
939939
if err != nil {
940-
if !models.IsErrTwoFactorNotEnrolled(err) {
940+
if !login.IsErrTwoFactorNotEnrolled(err) {
941941
ctx.ServerError("UserLinkAccount", err)
942942
return
943943
}
@@ -967,7 +967,7 @@ func linkAccount(ctx *context.Context, u *models.User, gothUser goth.User, remem
967967
}
968968

969969
// If U2F is enrolled -> Redirect to U2F instead
970-
regs, err := models.GetU2FRegistrationsByUID(u.ID)
970+
regs, err := login.GetU2FRegistrationsByUID(u.ID)
971971
if err == nil && len(regs) > 0 {
972972
ctx.Redirect(setting.AppSubURL + "/user/u2f")
973973
return
@@ -1561,7 +1561,7 @@ func ForgotPasswdPost(ctx *context.Context) {
15611561
ctx.HTML(http.StatusOK, tplForgotPassword)
15621562
}
15631563

1564-
func commonResetPassword(ctx *context.Context) (*models.User, *models.TwoFactor) {
1564+
func commonResetPassword(ctx *context.Context) (*models.User, *login.TwoFactor) {
15651565
code := ctx.FormString("code")
15661566

15671567
ctx.Data["Title"] = ctx.Tr("auth.reset_password")
@@ -1583,9 +1583,9 @@ func commonResetPassword(ctx *context.Context) (*models.User, *models.TwoFactor)
15831583
return nil, nil
15841584
}
15851585

1586-
twofa, err := models.GetTwoFactorByUID(u.ID)
1586+
twofa, err := login.GetTwoFactorByUID(u.ID)
15871587
if err != nil {
1588-
if !models.IsErrTwoFactorNotEnrolled(err) {
1588+
if !login.IsErrTwoFactorNotEnrolled(err) {
15891589
ctx.Error(http.StatusInternalServerError, "CommonResetPassword", err.Error())
15901590
return nil, nil
15911591
}
@@ -1680,7 +1680,7 @@ func ResetPasswdPost(ctx *context.Context) {
16801680
}
16811681

16821682
twofa.LastUsedPasscode = passcode
1683-
if err = models.UpdateTwoFactor(twofa); err != nil {
1683+
if err = login.UpdateTwoFactor(twofa); err != nil {
16841684
ctx.ServerError("ResetPasswdPost: UpdateTwoFactor", err)
16851685
return
16861686
}
@@ -1712,7 +1712,7 @@ func ResetPasswdPost(ctx *context.Context) {
17121712
ctx.ServerError("UserSignIn", err)
17131713
return
17141714
}
1715-
if err = models.UpdateTwoFactor(twofa); err != nil {
1715+
if err = login.UpdateTwoFactor(twofa); err != nil {
17161716
ctx.ServerError("UserSignIn", err)
17171717
return
17181718
}

routers/web/user/setting/security.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ func DeleteAccountLink(ctx *context.Context) {
5656

5757
func loadSecurityData(ctx *context.Context) {
5858
enrolled := true
59-
_, err := models.GetTwoFactorByUID(ctx.User.ID)
59+
_, err := login.GetTwoFactorByUID(ctx.User.ID)
6060
if err != nil {
61-
if models.IsErrTwoFactorNotEnrolled(err) {
61+
if login.IsErrTwoFactorNotEnrolled(err) {
6262
enrolled = false
6363
} else {
6464
ctx.ServerError("SettingsTwoFactor", err)
@@ -67,7 +67,7 @@ func loadSecurityData(ctx *context.Context) {
6767
}
6868
ctx.Data["TwofaEnrolled"] = enrolled
6969
if enrolled {
70-
ctx.Data["U2FRegistrations"], err = models.GetU2FRegistrationsByUID(ctx.User.ID)
70+
ctx.Data["U2FRegistrations"], err = login.GetU2FRegistrationsByUID(ctx.User.ID)
7171
if err != nil {
7272
ctx.ServerError("GetU2FRegistrationsByUID", err)
7373
return

0 commit comments

Comments
 (0)