aboutsummaryrefslogtreecommitdiff
path: root/internal/route
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-04-14 09:41:54 +0800
committerGitHub <noreply@github.com>2020-04-14 09:41:54 +0800
commitcb439a126aa6a2728e423bcfd0d5e948337b8ddb (patch)
treef7d09181fe5b96ea444f7544091673b3c668b9fe /internal/route
parent659acd48b1a131476fd98a54604fa6416b1cef9d (diff)
db: add tests for two factors (#6099)
* Rename to TwoFactors.Create * Use GORM to execute queries * TwoFactor.GetByUserID * Add tests * Fix failing tests * Add MD5 tests * Add tests for RandomChars
Diffstat (limited to 'internal/route')
-rw-r--r--internal/route/install.go4
-rw-r--r--internal/route/repo/tasks.go4
-rw-r--r--internal/route/user/auth.go2
-rw-r--r--internal/route/user/setting.go9
4 files changed, 10 insertions, 9 deletions
diff --git a/internal/route/install.go b/internal/route/install.go
index 5bbf943a..8c9d2eda 100644
--- a/internal/route/install.go
+++ b/internal/route/install.go
@@ -27,8 +27,8 @@ import (
"gogs.io/gogs/internal/markup"
"gogs.io/gogs/internal/osutil"
"gogs.io/gogs/internal/ssh"
+ "gogs.io/gogs/internal/strutil"
"gogs.io/gogs/internal/template/highlight"
- "gogs.io/gogs/internal/tool"
)
const (
@@ -365,7 +365,7 @@ func InstallPost(c *context.Context, f form.Install) {
cfg.Section("log").Key("ROOT_PATH").SetValue(f.LogRootPath)
cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
- secretKey, err := tool.RandomString(15)
+ secretKey, err := strutil.RandomChars(15)
if err != nil {
c.RenderWithErr(c.Tr("install.secret_key_failed", err), INSTALL, &f)
return
diff --git a/internal/route/repo/tasks.go b/internal/route/repo/tasks.go
index e06d3ee7..81e85e2a 100644
--- a/internal/route/repo/tasks.go
+++ b/internal/route/repo/tasks.go
@@ -10,8 +10,8 @@ import (
"gopkg.in/macaron.v1"
log "unknwon.dev/clog/v2"
+ "gogs.io/gogs/internal/cryptoutil"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/tool"
)
func TriggerTask(c *macaron.Context) {
@@ -39,7 +39,7 @@ func TriggerTask(c *macaron.Context) {
// 🚨 SECURITY: No need to check existence of the repository if the client
// can't even get the valid secret. Mostly likely not a legitimate request.
- if secret != tool.MD5(owner.Salt) {
+ if secret != cryptoutil.MD5(owner.Salt) {
c.Error(http.StatusBadRequest, "Invalid secret")
return
}
diff --git a/internal/route/user/auth.go b/internal/route/user/auth.go
index be23b78d..852c9679 100644
--- a/internal/route/user/auth.go
+++ b/internal/route/user/auth.go
@@ -209,7 +209,7 @@ func LoginTwoFactorPost(c *context.Context) {
return
}
- t, err := db.GetTwoFactorByUserID(userID)
+ t, err := db.TwoFactors.GetByUserID(userID)
if err != nil {
c.Error(err, "get two factor by user ID")
return
diff --git a/internal/route/user/setting.go b/internal/route/user/setting.go
index 7e49505d..15848ef5 100644
--- a/internal/route/user/setting.go
+++ b/internal/route/user/setting.go
@@ -20,6 +20,7 @@ import (
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
+ "gogs.io/gogs/internal/cryptoutil"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/email"
@@ -118,7 +119,7 @@ func SettingsPost(c *context.Context, f form.UpdateProfile) {
func UpdateAvatarSetting(c *context.Context, f form.Avatar, ctxUser *db.User) error {
ctxUser.UseCustomAvatar = f.Source == form.AVATAR_LOCAL
if len(f.Gravatar) > 0 {
- ctxUser.Avatar = tool.MD5(f.Gravatar)
+ ctxUser.Avatar = cryptoutil.MD5(f.Gravatar)
ctxUser.AvatarEmail = f.Gravatar
}
@@ -381,8 +382,8 @@ func SettingsSecurity(c *context.Context) {
c.Title("settings.security")
c.PageIs("SettingsSecurity")
- t, err := db.GetTwoFactorByUserID(c.UserID())
- if err != nil && !errors.IsTwoFactorNotFound(err) {
+ t, err := db.TwoFactors.GetByUserID(c.UserID())
+ if err != nil && !db.IsErrTwoFactorNotFound(err) {
c.Errorf(err, "get two factor by user ID")
return
}
@@ -449,7 +450,7 @@ func SettingsTwoFactorEnablePost(c *context.Context) {
return
}
- if err := db.NewTwoFactor(c.UserID(), secret); err != nil {
+ if err := db.TwoFactors.Create(c.UserID(), conf.Security.SecretKey, secret); err != nil {
c.Flash.Error(c.Tr("settings.two_factor_enable_error", err))
c.RedirectSubpath("/user/settings/security/two_factor_enable")
return