Skip to content

Commit

Permalink
feat: allow to disable password authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb committed Dec 31, 2024
1 parent 3c89d2d commit 24a17e5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
6 changes: 6 additions & 0 deletions cmd/dashboard/controller/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,19 @@ func authenticator() func(c *gin.Context) (interface{}, error) {

var user model.User
realip := c.GetString(model.CtxKeyRealIPStr)

if err := singleton.DB.Select("id", "password").Where("username = ?", loginVals.Username).First(&user).Error; err != nil {
if err == gorm.ErrRecordNotFound {
model.BlockIP(singleton.DB, realip, model.WAFBlockReasonTypeLoginFail, model.BlockIDUnknownUser)
}
return nil, jwt.ErrFailedAuthentication
}

if user.RejectPassword {
model.BlockIP(singleton.DB, realip, model.WAFBlockReasonTypeLoginFail, int64(user.ID))
return nil, jwt.ErrForbidden
}

if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(loginVals.Password)); err != nil {
model.BlockIP(singleton.DB, realip, model.WAFBlockReasonTypeLoginFail, int64(user.ID))
return nil, jwt.ErrFailedAuthentication
Expand Down
17 changes: 15 additions & 2 deletions cmd/dashboard/controller/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func oauth2redirect(c *gin.Context) (*model.Oauth2LoginResponse, error) {
return nil, singleton.Localizer.ErrorT("provider is required")
}

rTypeInt, err := strconv.Atoi(c.Query("type"))
rTypeInt, err := strconv.ParseUint(c.Query("type"), 10, 8)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,10 +87,23 @@ func unbindOauth2(c *gin.Context) (any, error) {
return nil, singleton.Localizer.ErrorT("provider not found")
}
provider = strings.ToLower(provider)

u := c.MustGet(model.CtxKeyAuthorizedUser).(*model.User)
if err := singleton.DB.Where("provider = ? AND user_id = ?", provider, u.ID).Delete(&model.Oauth2Bind{}).Error; err != nil {
query := singleton.DB.Where("provider = ? AND user_id = ?", provider, u.ID)

var bindCount int64
if err := query.Model(&model.Oauth2Bind{}).Count(&bindCount).Error; err != nil {
return nil, newGormError("%v", err)
}

if bindCount < 2 && u.RejectPassword {
return nil, singleton.Localizer.ErrorT("operation not permitted")
}

if err := query.Delete(&model.Oauth2Bind{}).Error; err != nil {
return nil, newGormError("%v", err)
}

return nil, nil
}

Expand Down
10 changes: 10 additions & 0 deletions cmd/dashboard/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,18 @@ func updateProfile(c *gin.Context) (any, error) {
return nil, err
}

var bindCount int64
if err := singleton.DB.Where("user_id = ?", auth.(*model.User).ID).Count(&bindCount).Error; err != nil {
return nil, newGormError("%v", err)
}

if pf.RejectPassword && bindCount < 1 {
return nil, singleton.Localizer.ErrorT("you don't have any oauth2 bindings")
}

user.Username = pf.NewUsername
user.Password = string(hash)
user.RejectPassword = pf.RejectPassword
if err := singleton.DB.Save(&user).Error; err != nil {
return nil, newGormError("%v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const (

type User struct {
Common
Username string `json:"username,omitempty" gorm:"uniqueIndex"`
Password string `json:"password,omitempty" gorm:"type:char(72)"`
Role uint8 `json:"role,omitempty"`
AgentSecret string `json:"agent_secret,omitempty" gorm:"type:char(32)"`
Username string `json:"username,omitempty" gorm:"uniqueIndex"`
Password string `json:"password,omitempty" gorm:"type:char(72)"`
Role uint8 `json:"role,omitempty"`
AgentSecret string `json:"agent_secret,omitempty" gorm:"type:char(32)"`
RejectPassword bool `json:"reject_password,omitempty"`
}

type UserInfo struct {
Expand Down
1 change: 1 addition & 0 deletions model/user_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type ProfileForm struct {
OriginalPassword string `json:"original_password,omitempty"`
NewUsername string `json:"new_username,omitempty"`
NewPassword string `json:"new_password,omitempty"`
RejectPassword bool `json:"reject_password,omitempty"`
}

0 comments on commit 24a17e5

Please sign in to comment.