2021-07-24 15:46:34 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-24 15:46:34 +05:30
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-01-02 18:42:35 +05:30
|
|
|
"code.gitea.io/gitea/models/auth"
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-11 12:33:30 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 15:46:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-11-27 17:29:51 +05:30
|
|
|
"code.gitea.io/gitea/services/auth/source/oauth2"
|
|
|
|
"code.gitea.io/gitea/services/auth/source/smtp"
|
2021-07-24 15:46:34 +05:30
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
_ "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 17:29:51 +05:30
|
|
|
_ "code.gitea.io/gitea/services/auth/source/pam" // register the pam source
|
2021-11-24 15:19:20 +05:30
|
|
|
_ "code.gitea.io/gitea/services/auth/source/sspi" // register the sspi source
|
2021-07-24 15:46:34 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// UserSignIn validates user name and password.
|
2022-01-02 18:42:35 +05:30
|
|
|
func UserSignIn(username, password string) (*user_model.User, *auth.Source, error) {
|
2021-11-24 15:19:20 +05:30
|
|
|
var user *user_model.User
|
2022-03-31 18:50:25 +05:30
|
|
|
isEmail := false
|
2021-07-24 15:46:34 +05:30
|
|
|
if strings.Contains(username, "@") {
|
2022-03-31 18:50:25 +05:30
|
|
|
isEmail = true
|
2022-02-18 15:03:00 +05:30
|
|
|
emailAddress := user_model.EmailAddress{LowerEmail: strings.ToLower(strings.TrimSpace(username))}
|
2021-07-24 15:46:34 +05:30
|
|
|
// check same email
|
2022-03-31 18:50:25 +05:30
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(&emailAddress)
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
2021-09-17 17:13:47 +05:30
|
|
|
return nil, nil, err
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
2022-03-31 18:50:25 +05:30
|
|
|
if has {
|
|
|
|
if !emailAddress.IsActivated {
|
|
|
|
return nil, nil, user_model.ErrEmailAddressNotExist{
|
|
|
|
Email: username,
|
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
2022-03-31 18:50:25 +05:30
|
|
|
user = &user_model.User{ID: emailAddress.UID}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trimmedUsername := strings.TrimSpace(username)
|
|
|
|
if len(trimmedUsername) == 0 {
|
2021-11-24 15:19:20 +05:30
|
|
|
return nil, nil, user_model.ErrUserNotExist{Name: username}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
user = &user_model.User{LowerName: strings.ToLower(trimmedUsername)}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
2022-03-31 18:50:25 +05:30
|
|
|
if user != nil {
|
|
|
|
hasUser, err := user_model.GetUser(user)
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
2021-09-17 17:13:47 +05:30
|
|
|
return nil, nil, err
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
2022-03-31 18:50:25 +05:30
|
|
|
if hasUser {
|
|
|
|
source, err := auth.GetSourceByID(user.LoginSource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
|
2022-03-31 18:50:25 +05:30
|
|
|
if !source.IsActive {
|
|
|
|
return nil, nil, oauth2.ErrAuthSourceNotActived
|
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
|
2022-03-31 18:50:25 +05:30
|
|
|
authenticator, ok := source.Cfg.(PasswordAuthenticator)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, smtp.ErrUnsupportedLoginType
|
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
|
2022-03-31 18:50:25 +05:30
|
|
|
user, err := authenticator.Authenticate(user, user.LoginName, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
|
2022-03-31 18:50:25 +05:30
|
|
|
// 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 {
|
|
|
|
return nil, nil, user_model.ErrUserProhibitLogin{UID: user.ID, Name: user.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, source, nil
|
|
|
|
}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
2022-01-02 18:42:35 +05:30
|
|
|
sources, err := auth.AllActiveSources()
|
2021-07-24 15:46:34 +05:30
|
|
|
if err != nil {
|
2021-09-17 17:13:47 +05:30
|
|
|
return nil, nil, err
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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 17:13:47 +05:30
|
|
|
return authUser, source, nil
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
2021-11-24 15:19:20 +05:30
|
|
|
err = user_model.ErrUserProhibitLogin{UID: authUser.ID, Name: authUser.Name}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2021-07-24 15:46:34 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:50:25 +05:30
|
|
|
if isEmail {
|
|
|
|
return nil, nil, user_model.ErrEmailAddressNotExist{Email: username}
|
|
|
|
}
|
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
return nil, nil, user_model.ErrUserNotExist{Name: username}
|
2021-07-24 15:46:34 +05:30
|
|
|
}
|