2021-07-24 12:16:34 +02: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 13:49:59 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-07-24 12:16:34 +02: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-17 13:43:47 +02:00
|
|
|
func UserSignIn(username, password string) (*models.User, *models.LoginSource, error) {
|
2021-07-24 12:16:34 +02:00
|
|
|
var user *models.User
|
|
|
|
if strings.Contains(username, "@") {
|
|
|
|
user = &models.User{Email: strings.ToLower(strings.TrimSpace(username))}
|
|
|
|
// check same email
|
2021-09-19 13:49:59 +02:00
|
|
|
cnt, err := db.Count(user)
|
2021-07-24 12:16:34 +02:00
|
|
|
if err != nil {
|
2021-09-17 13:43:47 +02:00
|
|
|
return nil, nil, err
|
2021-07-24 12:16:34 +02:00
|
|
|
}
|
|
|
|
if cnt > 1 {
|
2021-09-17 13:43:47 +02:00
|
|
|
return nil, nil, models.ErrEmailAlreadyUsed{
|
2021-07-24 12:16:34 +02:00
|
|
|
Email: user.Email,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trimmedUsername := strings.TrimSpace(username)
|
|
|
|
if len(trimmedUsername) == 0 {
|
2021-09-17 13:43:47 +02:00
|
|
|
return nil, nil, models.ErrUserNotExist{Name: username}
|
2021-07-24 12:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
user = &models.User{LowerName: strings.ToLower(trimmedUsername)}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasUser, err := models.GetUser(user)
|
|
|
|
if err != nil {
|
2021-09-17 13:43:47 +02:00
|
|
|
return nil, nil, err
|
2021-07-24 12:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if hasUser {
|
|
|
|
source, err := models.GetLoginSourceByID(user.LoginSource)
|
|
|
|
if err != nil {
|
2021-09-17 13:43:47 +02:00
|
|
|
return nil, nil, err
|
2021-07-24 12:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !source.IsActive {
|
2021-09-17 13:43:47 +02:00
|
|
|
return nil, nil, models.ErrLoginSourceNotActived
|
2021-07-24 12:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
authenticator, ok := source.Cfg.(PasswordAuthenticator)
|
|
|
|
if !ok {
|
2021-09-17 13:43:47 +02:00
|
|
|
return nil, nil, models.ErrUnsupportedLoginType
|
2021-07-24 12:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
user, err := authenticator.Authenticate(user, username, password)
|
|
|
|
if err != nil {
|
2021-09-17 13:43:47 +02:00
|
|
|
return nil, nil, err
|
2021-07-24 12:16:34 +02: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 13:43:47 +02:00
|
|
|
return nil, nil, models.ErrUserProhibitLogin{UID: user.ID, Name: user.Name}
|
2021-07-24 12:16:34 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 13:43:47 +02:00
|
|
|
return user, source, nil
|
2021-07-24 12:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sources, err := models.AllActiveLoginSources()
|
|
|
|
if err != nil {
|
2021-09-17 13:43:47 +02:00
|
|
|
return nil, nil, err
|
2021-07-24 12:16:34 +02: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 13:43:47 +02:00
|
|
|
return authUser, source, nil
|
2021-07-24 12:16:34 +02: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 13:43:47 +02:00
|
|
|
return nil, nil, models.ErrUserNotExist{Name: username}
|
2021-07-24 12:16:34 +02:00
|
|
|
}
|