2021-07-24 19:16:34 +09: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"
|
|
|
|
|
2022-01-02 22:12:35 +09:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2021-09-19 20:49:59 +09:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-11 16:03:30 +09:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 19:16:34 +09:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-11-27 20:59:51 +09:00
|
|
|
"code.gitea.io/gitea/services/auth/source/oauth2"
|
|
|
|
"code.gitea.io/gitea/services/auth/source/smtp"
|
2021-07-24 19:16:34 +09:00
|
|
|
|
2021-11-24 18:49:20 +09:00
|
|
|
_ "code.gitea.io/gitea/services/auth/source/db" // register the sources (and below)
|
|
|
|
_ "code.gitea.io/gitea/services/auth/source/ldap" // register the ldap source
|
2021-11-27 20:59:51 +09:00
|
|
|
_ "code.gitea.io/gitea/services/auth/source/pam" // register the pam source
|
2021-11-24 18:49:20 +09:00
|
|
|
_ "code.gitea.io/gitea/services/auth/source/sspi" // register the sspi source
|
2021-07-24 19:16:34 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// UserSignIn validates user name and password.
|
2022-01-02 22:12:35 +09:00
|
|
|
func UserSignIn(username, password string) (*user_model.User, *auth.Source, error) {
|
2021-11-24 18:49:20 +09:00
|
|
|
var user *user_model.User
|
2021-07-24 19:16:34 +09:00
|
|
|
if strings.Contains(username, "@") {
|
2022-02-18 18:33:00 +09:00
|
|
|
emailAddress := user_model.EmailAddress{LowerEmail: strings.ToLower(strings.TrimSpace(username))}
|
2021-07-24 19:16:34 +09:00
|
|
|
// check same email
|
2022-02-18 18:33:00 +09:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Where("is_activated=?", true).Get(&emailAddress)
|
2021-07-24 19:16:34 +09:00
|
|
|
if err != nil {
|
2021-09-17 20:43:47 +09:00
|
|
|
return nil, nil, err
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
2022-02-18 18:33:00 +09:00
|
|
|
if !has {
|
|
|
|
return nil, nil, user_model.ErrEmailAddressNotExist{
|
2022-03-01 02:33:33 +09:00
|
|
|
Email: username,
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
}
|
2022-02-18 18:33:00 +09:00
|
|
|
user = &user_model.User{ID: emailAddress.UID}
|
2021-07-24 19:16:34 +09:00
|
|
|
} else {
|
|
|
|
trimmedUsername := strings.TrimSpace(username)
|
|
|
|
if len(trimmedUsername) == 0 {
|
2021-11-24 18:49:20 +09:00
|
|
|
return nil, nil, user_model.ErrUserNotExist{Name: username}
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:20 +09:00
|
|
|
user = &user_model.User{LowerName: strings.ToLower(trimmedUsername)}
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:20 +09:00
|
|
|
hasUser, err := user_model.GetUser(user)
|
2021-07-24 19:16:34 +09:00
|
|
|
if err != nil {
|
2021-09-17 20:43:47 +09:00
|
|
|
return nil, nil, err
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if hasUser {
|
2022-01-02 22:12:35 +09:00
|
|
|
source, err := auth.GetSourceByID(user.LoginSource)
|
2021-07-24 19:16:34 +09:00
|
|
|
if err != nil {
|
2021-09-17 20:43:47 +09:00
|
|
|
return nil, nil, err
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if !source.IsActive {
|
2022-01-02 22:12:35 +09:00
|
|
|
return nil, nil, oauth2.ErrAuthSourceNotActived
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
authenticator, ok := source.Cfg.(PasswordAuthenticator)
|
|
|
|
if !ok {
|
2021-11-24 18:49:20 +09:00
|
|
|
return nil, nil, smtp.ErrUnsupportedLoginType
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
|
2022-02-20 19:28:58 +09:00
|
|
|
user, err := authenticator.Authenticate(user, user.LoginName, password)
|
2021-07-24 19:16:34 +09:00
|
|
|
if err != nil {
|
2021-09-17 20:43:47 +09:00
|
|
|
return nil, nil, err
|
2021-07-24 19:16:34 +09: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-11-24 18:49:20 +09:00
|
|
|
return nil, nil, user_model.ErrUserProhibitLogin{UID: user.ID, Name: user.Name}
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
|
2021-09-17 20:43:47 +09:00
|
|
|
return user, source, nil
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
|
2022-01-02 22:12:35 +09:00
|
|
|
sources, err := auth.AllActiveSources()
|
2021-07-24 19:16:34 +09:00
|
|
|
if err != nil {
|
2021-09-17 20:43:47 +09:00
|
|
|
return nil, nil, err
|
2021-07-24 19:16:34 +09: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 20:43:47 +09:00
|
|
|
return authUser, source, nil
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
2021-11-24 18:49:20 +09:00
|
|
|
err = user_model.ErrUserProhibitLogin{UID: authUser.ID, Name: authUser.Name}
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:20 +09:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2021-07-24 19:16:34 +09:00
|
|
|
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-11-24 18:49:20 +09:00
|
|
|
return nil, nil, user_model.ErrUserNotExist{Name: username}
|
2021-07-24 19:16:34 +09:00
|
|
|
}
|