Skip to content

Commit f5fa22a

Browse files
lunnylafriks
authored andcommitted
Fix prohibit login check on authorization (#6106)
* fix bug prohibit login not applied on dashboard * fix tests * fix bug user status leak * fix typo * return after render
1 parent 538a26d commit f5fa22a

File tree

7 files changed

+83
-9
lines changed

7 files changed

+83
-9
lines changed

integrations/release_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestCreateReleasePaging(t *testing.T) {
112112

113113
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.12", i18n.Tr("en", "repo.release.draft"), 10)
114114

115-
// Check that user3 does not see draft and still see 10 latest releases
116-
session2 := loginUser(t, "user3")
115+
// Check that user4 does not see draft and still see 10 latest releases
116+
session2 := loginUser(t, "user4")
117117
checkLatestReleaseAndCount(t, session2, "/user2/repo1", "v0.0.11", i18n.Tr("en", "repo.release.stable"), 10)
118118
}

integrations/repo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestViewRepoWithSymlinks(t *testing.T) {
104104
func TestViewAsRepoAdmin(t *testing.T) {
105105
for user, expectedNoDescription := range map[string]bool{
106106
"user2": true,
107-
"user3": false,
107+
"user4": false,
108108
} {
109109
prepareTestEnv(t)
110110

models/error.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,38 @@ func (err ErrUserNotExist) Error() string {
9090
return fmt.Sprintf("user does not exist [uid: %d, name: %s, keyid: %d]", err.UID, err.Name, err.KeyID)
9191
}
9292

93+
// ErrUserProhibitLogin represents a "ErrUserProhibitLogin" kind of error.
94+
type ErrUserProhibitLogin struct {
95+
UID int64
96+
Name string
97+
}
98+
99+
// IsErrUserProhibitLogin checks if an error is a ErrUserProhibitLogin
100+
func IsErrUserProhibitLogin(err error) bool {
101+
_, ok := err.(ErrUserProhibitLogin)
102+
return ok
103+
}
104+
105+
func (err ErrUserProhibitLogin) Error() string {
106+
return fmt.Sprintf("user is not allowed login [uid: %d, name: %s]", err.UID, err.Name)
107+
}
108+
109+
// ErrUserInactive represents a "ErrUserInactive" kind of error.
110+
type ErrUserInactive struct {
111+
UID int64
112+
Name string
113+
}
114+
115+
// IsErrUserInactive checks if an error is a ErrUserInactive
116+
func IsErrUserInactive(err error) bool {
117+
_, ok := err.(ErrUserInactive)
118+
return ok
119+
}
120+
121+
func (err ErrUserInactive) Error() string {
122+
return fmt.Sprintf("user is inactive [uid: %d, name: %s]", err.UID, err.Name)
123+
}
124+
93125
// ErrEmailAlreadyUsed represents a "EmailAlreadyUsed" kind of error.
94126
type ErrEmailAlreadyUsed struct {
95127
Email string

models/login_source.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,16 +600,29 @@ func ExternalUserLogin(user *User, login, password string, source *LoginSource,
600600
return nil, ErrLoginSourceNotActived
601601
}
602602

603+
var err error
603604
switch source.Type {
604605
case LoginLDAP, LoginDLDAP:
605-
return LoginViaLDAP(user, login, password, source, autoRegister)
606+
user, err = LoginViaLDAP(user, login, password, source, autoRegister)
606607
case LoginSMTP:
607-
return LoginViaSMTP(user, login, password, source.ID, source.Cfg.(*SMTPConfig), autoRegister)
608+
user, err = LoginViaSMTP(user, login, password, source.ID, source.Cfg.(*SMTPConfig), autoRegister)
608609
case LoginPAM:
609-
return LoginViaPAM(user, login, password, source.ID, source.Cfg.(*PAMConfig), autoRegister)
610+
user, err = LoginViaPAM(user, login, password, source.ID, source.Cfg.(*PAMConfig), autoRegister)
611+
default:
612+
return nil, ErrUnsupportedLoginType
613+
}
614+
615+
if err != nil {
616+
return nil, err
617+
}
618+
619+
if !user.IsActive {
620+
return nil, ErrUserInactive{user.ID, user.Name}
621+
} else if user.ProhibitLogin {
622+
return nil, ErrUserProhibitLogin{user.ID, user.Name}
610623
}
611624

612-
return nil, ErrUnsupportedLoginType
625+
return user, nil
613626
}
614627

615628
// UserSignIn validates user name and password.
@@ -645,6 +658,12 @@ func UserSignIn(username, password string) (*User, error) {
645658
switch user.LoginType {
646659
case LoginNoType, LoginPlain, LoginOAuth2:
647660
if user.IsPasswordSet() && user.ValidatePassword(password) {
661+
if !user.IsActive {
662+
return nil, ErrUserInactive{user.ID, user.Name}
663+
} else if user.ProhibitLogin {
664+
return nil, ErrUserProhibitLogin{user.ID, user.Name}
665+
}
666+
648667
return user, nil
649668
}
650669

modules/context/auth.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/url"
99

1010
"code.gitea.io/gitea/modules/auth"
11+
"code.gitea.io/gitea/modules/log"
1112
"code.gitea.io/gitea/modules/setting"
1213
"github.com/go-macaron/csrf"
1314
macaron "gopkg.in/macaron.v1"
@@ -32,8 +33,12 @@ func Toggle(options *ToggleOptions) macaron.Handler {
3233

3334
// Check prohibit login users.
3435
if ctx.IsSigned {
35-
36-
if ctx.User.ProhibitLogin {
36+
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
37+
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
38+
ctx.HTML(200, "user/auth/activate")
39+
return
40+
} else if !ctx.User.IsActive || ctx.User.ProhibitLogin {
41+
log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
3742
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
3843
ctx.HTML(200, "user/auth/prohibit_login")
3944
return

routers/home.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/base"
1414
"code.gitea.io/gitea/modules/context"
15+
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/search"
1617
"code.gitea.io/gitea/modules/setting"
1718
"code.gitea.io/gitea/modules/util"
@@ -39,6 +40,10 @@ func Home(ctx *context.Context) {
3940
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
4041
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
4142
ctx.HTML(200, user.TplActivate)
43+
} else if !ctx.User.IsActive || ctx.User.ProhibitLogin {
44+
log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
45+
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
46+
ctx.HTML(200, "user/auth/prohibit_login")
4247
} else {
4348
user.Dashboard(ctx)
4449
}

routers/user/auth.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) {
161161
} else if models.IsErrEmailAlreadyUsed(err) {
162162
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignIn, &form)
163163
log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
164+
} else if models.IsErrUserProhibitLogin(err) {
165+
log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
166+
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
167+
ctx.HTML(200, "user/auth/prohibit_login")
168+
} else if models.IsErrUserInactive(err) {
169+
if setting.Service.RegisterEmailConfirm {
170+
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
171+
ctx.HTML(200, TplActivate)
172+
} else {
173+
log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr())
174+
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
175+
ctx.HTML(200, "user/auth/prohibit_login")
176+
}
164177
} else {
165178
ctx.ServerError("UserSignIn", err)
166179
}

0 commit comments

Comments
 (0)