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 (
|
2023-09-14 22:39:32 +05:30
|
|
|
"context"
|
2021-07-24 15:46:34 +05:30
|
|
|
"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"
|
2023-11-03 07:11:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
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.
|
2023-09-14 22:39:32 +05:30
|
|
|
func UserSignIn(ctx context.Context, 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
|
2023-09-14 22:39:32 +05:30
|
|
|
has, err := db.GetEngine(ctx).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 {
|
2023-09-14 22:39:32 +05:30
|
|
|
hasUser, err := user_model.GetUser(ctx, 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 {
|
2023-10-11 09:54:07 +05:30
|
|
|
source, err := auth.GetSourceByID(ctx, user.LoginSource)
|
2022-03-31 18:50:25 +05:30
|
|
|
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 {
|
2023-06-08 22:05:29 +05:30
|
|
|
return nil, nil, oauth2.ErrAuthSourceNotActivated
|
2022-03-31 18:50:25 +05:30
|
|
|
}
|
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
|
|
|
|
2023-09-14 22:39:32 +05:30
|
|
|
user, err := authenticator.Authenticate(ctx, user, user.LoginName, password)
|
2022-03-31 18:50:25 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-03 07:11:00 +05:30
|
|
|
sources, err := auth.FindSources(ctx, auth.FindSourcesOptions{
|
|
|
|
IsActive: util.OptionalBoolTrue,
|
|
|
|
})
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:39:32 +05:30
|
|
|
authUser, err := authenticator.Authenticate(ctx, nil, username, password)
|
2021-07-24 15:46:34 +05:30
|
|
|
|
|
|
|
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
|
|
|
}
|