Move password into own package to prevent package cycle

This commit is contained in:
Jannis Mattheis 2018-03-21 18:05:20 +01:00 committed by Jannis Mattheis
parent 01c6800ae8
commit 203791c63b
5 changed files with 9 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/gotify/server/auth"
"github.com/gotify/server/model"
"github.com/gotify/server/auth/password"
)
// The UserDatabase interface for encapsulating database access.
@ -83,7 +84,7 @@ func (a *UserAPI) ChangePassword(ctx *gin.Context) {
pw := model.UserExternalPass{}
if err := ctx.Bind(&pw); err == nil {
user := a.DB.GetUserByID(auth.GetUserID(ctx))
user.Pass = auth.CreatePassword(pw.Pass, a.PasswordStrength)
user.Pass = password.CreatePassword(pw.Pass, a.PasswordStrength)
a.DB.UpdateUser(user)
}
}
@ -111,7 +112,7 @@ func (a *UserAPI) toInternal(response *model.UserExternalWithPass, pw []byte) *m
Admin: response.Admin,
}
if response.Pass != "" {
user.Pass = auth.CreatePassword(response.Pass, a.PasswordStrength)
user.Pass = password.CreatePassword(response.Pass, a.PasswordStrength)
} else {
user.Pass = pw
}

View File

@ -5,6 +5,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/gotify/server/model"
"github.com/gotify/server/auth/password"
)
const (
@ -86,7 +87,7 @@ func (a *Auth) tokenFromHeader(ctx *gin.Context) string {
func (a *Auth) userFromBasicAuth(ctx *gin.Context) *model.User {
if name, pass, ok := ctx.Request.BasicAuth(); ok {
if user := a.DB.GetUserByName(name); user != nil && ComparePassword(user.Pass, []byte(pass)) {
if user := a.DB.GetUserByName(name); user != nil && password.ComparePassword(user.Pass, []byte(pass)) {
return user
}
}

View File

@ -1,4 +1,4 @@
package auth
package password
import "golang.org/x/crypto/bcrypt"

View File

@ -1,6 +1,6 @@
// +build !race
package auth
package password
import (
"testing"

View File

@ -1,7 +1,7 @@
package database
import (
"github.com/gotify/server/auth"
"github.com/gotify/server/auth/password"
"github.com/gotify/server/model"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql" // enable the mysql dialect
@ -31,7 +31,7 @@ func New(dialect, connection, defaultUser, defaultPass string, strength int) (*G
if !db.HasTable(new(model.User)) && !db.HasTable(new(model.Message)) &&
!db.HasTable(new(model.Client)) && !db.HasTable(new(model.Application)) {
db.AutoMigrate(new(model.User), new(model.Application), new(model.Message), new(model.Client))
db.Create(&model.User{Name: defaultUser, Pass: auth.CreatePassword(defaultPass, strength), Admin: true})
db.Create(&model.User{Name: defaultUser, Pass: password.CreatePassword(defaultPass, strength), Admin: true})
}
return &GormDatabase{DB: db}, nil