Move password into own package to prevent package cycle
This commit is contained in:
parent
01c6800ae8
commit
203791c63b
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gotify/server/auth"
|
"github.com/gotify/server/auth"
|
||||||
"github.com/gotify/server/model"
|
"github.com/gotify/server/model"
|
||||||
|
"github.com/gotify/server/auth/password"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The UserDatabase interface for encapsulating database access.
|
// The UserDatabase interface for encapsulating database access.
|
||||||
|
|
@ -83,7 +84,7 @@ func (a *UserAPI) ChangePassword(ctx *gin.Context) {
|
||||||
pw := model.UserExternalPass{}
|
pw := model.UserExternalPass{}
|
||||||
if err := ctx.Bind(&pw); err == nil {
|
if err := ctx.Bind(&pw); err == nil {
|
||||||
user := a.DB.GetUserByID(auth.GetUserID(ctx))
|
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)
|
a.DB.UpdateUser(user)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -111,7 +112,7 @@ func (a *UserAPI) toInternal(response *model.UserExternalWithPass, pw []byte) *m
|
||||||
Admin: response.Admin,
|
Admin: response.Admin,
|
||||||
}
|
}
|
||||||
if response.Pass != "" {
|
if response.Pass != "" {
|
||||||
user.Pass = auth.CreatePassword(response.Pass, a.PasswordStrength)
|
user.Pass = password.CreatePassword(response.Pass, a.PasswordStrength)
|
||||||
} else {
|
} else {
|
||||||
user.Pass = pw
|
user.Pass = pw
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gotify/server/model"
|
"github.com/gotify/server/model"
|
||||||
|
"github.com/gotify/server/auth/password"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -86,7 +87,7 @@ func (a *Auth) tokenFromHeader(ctx *gin.Context) string {
|
||||||
|
|
||||||
func (a *Auth) userFromBasicAuth(ctx *gin.Context) *model.User {
|
func (a *Auth) userFromBasicAuth(ctx *gin.Context) *model.User {
|
||||||
if name, pass, ok := ctx.Request.BasicAuth(); ok {
|
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
|
return user
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package auth
|
package password
|
||||||
|
|
||||||
import "golang.org/x/crypto/bcrypt"
|
import "golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
// +build !race
|
// +build !race
|
||||||
|
|
||||||
package auth
|
package password
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gotify/server/auth"
|
"github.com/gotify/server/auth/password"
|
||||||
"github.com/gotify/server/model"
|
"github.com/gotify/server/model"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/mysql" // enable the mysql dialect
|
_ "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)) &&
|
if !db.HasTable(new(model.User)) && !db.HasTable(new(model.Message)) &&
|
||||||
!db.HasTable(new(model.Client)) && !db.HasTable(new(model.Application)) {
|
!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.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
|
return &GormDatabase{DB: db}, nil
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue