2021-07-24 06:16:34 -04:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-09-24 07:32:56 -04:00
|
|
|
"code.gitea.io/gitea/models/login"
|
2021-11-11 02:03:30 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 06:16:34 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
|
|
|
// Register the sources
|
|
|
|
_ "code.gitea.io/gitea/services/auth/source/db"
|
|
|
|
_ "code.gitea.io/gitea/services/auth/source/ldap"
|
|
|
|
_ "code.gitea.io/gitea/services/auth/source/oauth2"
|
|
|
|
_ "code.gitea.io/gitea/services/auth/source/pam"
|
|
|
|
_ "code.gitea.io/gitea/services/auth/source/smtp"
|
|
|
|
_ "code.gitea.io/gitea/services/auth/source/sspi"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UserSignIn validates user name and password.
|
2021-09-24 07:32:56 -04:00
|
|
|
func UserSignIn(username, password string) (*models.User, *login.Source, error) {
|
2021-07-24 06:16:34 -04:00
|
|
|
var user *models.User
|
|
|
|
if strings.Contains(username, "@") {
|
|
|
|
user = &models.User{Email: strings.ToLower(strings.TrimSpace(username))}
|
|
|
|
// check same email
|
2021-09-19 07:49:59 -04:00
|
|
|
cnt, err := db.Count(user)
|
2021-07-24 06:16:34 -04:00
|
|
|
if err != nil {
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, err
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
if cnt > 1 {
|
2021-11-11 02:03:30 -05:00
|
|
|
return nil, nil, user_model.ErrEmailAlreadyUsed{
|
2021-07-24 06:16:34 -04:00
|
|
|
Email: user.Email,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trimmedUsername := strings.TrimSpace(username)
|
|
|
|
if len(trimmedUsername) == 0 {
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, models.ErrUserNotExist{Name: username}
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
user = &models.User{LowerName: strings.ToLower(trimmedUsername)}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasUser, err := models.GetUser(user)
|
|
|
|
if err != nil {
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, err
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if hasUser {
|
2021-09-24 07:32:56 -04:00
|
|
|
source, err := login.GetSourceByID(user.LoginSource)
|
2021-07-24 06:16:34 -04:00
|
|
|
if err != nil {
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, err
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !source.IsActive {
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, models.ErrLoginSourceNotActived
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
authenticator, ok := source.Cfg.(PasswordAuthenticator)
|
|
|
|
if !ok {
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, models.ErrUnsupportedLoginType
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
user, err := authenticator.Authenticate(user, username, password)
|
|
|
|
if err != nil {
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, err
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WARN: DON'T check user.IsActive, that will be checked on reqSign so that
|
|
|
|
// user could be hint to resend confirm email.
|
|
|
|
if user.ProhibitLogin {
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, models.ErrUserProhibitLogin{UID: user.ID, Name: user.Name}
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
2021-09-17 07:43:47 -04:00
|
|
|
return user, source, nil
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
2021-09-24 07:32:56 -04:00
|
|
|
sources, err := login.AllActiveSources()
|
2021-07-24 06:16:34 -04:00
|
|
|
if err != nil {
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, err
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, source := range sources {
|
|
|
|
if !source.IsActive {
|
|
|
|
// don't try to authenticate non-active sources
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticator, ok := source.Cfg.(PasswordAuthenticator)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
authUser, err := authenticator.Authenticate(nil, username, password)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if !authUser.ProhibitLogin {
|
2021-09-17 07:43:47 -04:00
|
|
|
return authUser, source, nil
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
err = models.ErrUserProhibitLogin{UID: authUser.ID, Name: authUser.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
if models.IsErrUserNotExist(err) {
|
|
|
|
log.Debug("Failed to login '%s' via '%s': %v", username, source.Name, err)
|
|
|
|
} else {
|
|
|
|
log.Warn("Failed to login '%s' via '%s': %v", username, source.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 07:43:47 -04:00
|
|
|
return nil, nil, models.ErrUserNotExist{Name: username}
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|