2019-11-22 16:33:31 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-22 16:33:31 -07:00
|
|
|
|
2021-06-09 11:53:16 -06:00
|
|
|
package auth
|
2019-11-22 16:33:31 -07:00
|
|
|
|
|
|
|
import (
|
2023-09-14 11:09:32 -06:00
|
|
|
"context"
|
2019-11-22 16:33:31 -07:00
|
|
|
"errors"
|
2021-01-05 06:05:40 -07:00
|
|
|
"net/http"
|
2019-11-22 16:33:31 -07:00
|
|
|
"strings"
|
2023-06-18 01:59:09 -06:00
|
|
|
"sync"
|
2019-11-22 16:33:31 -07:00
|
|
|
|
2022-01-02 06:12:35 -07:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2023-11-23 20:49:41 -07:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-11-22 16:33:31 -07:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-22 19:18:33 -07:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2019-11-22 16:33:31 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-30 01:55:53 -07:00
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2021-07-24 04:16:34 -06:00
|
|
|
"code.gitea.io/gitea/services/auth/source/sspi"
|
2024-02-27 00:12:22 -07:00
|
|
|
gitea_context "code.gitea.io/gitea/services/context"
|
2019-11-22 16:33:31 -07:00
|
|
|
|
2020-06-18 03:18:44 -06:00
|
|
|
gouuid "github.com/google/uuid"
|
2019-11-22 16:33:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplSignIn base.TplName = "user/auth/signin"
|
|
|
|
)
|
|
|
|
|
2023-09-17 17:32:56 -06:00
|
|
|
type SSPIAuth interface {
|
|
|
|
AppendAuthenticateHeader(w http.ResponseWriter, data string)
|
|
|
|
Authenticate(r *http.Request, w http.ResponseWriter) (userInfo *SSPIUserInfo, outToken string, err error)
|
|
|
|
}
|
|
|
|
|
2019-11-22 16:33:31 -07:00
|
|
|
var (
|
2023-09-17 17:32:56 -06:00
|
|
|
sspiAuth SSPIAuth // a global instance of the websspi authenticator to avoid acquiring the server credential handle on every request
|
|
|
|
sspiAuthOnce sync.Once
|
|
|
|
sspiAuthErrInit error
|
2019-11-22 16:33:31 -07:00
|
|
|
|
|
|
|
// Ensure the struct implements the interface.
|
2023-06-18 01:59:09 -06:00
|
|
|
_ Method = &SSPI{}
|
2019-11-22 16:33:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// SSPI implements the SingleSignOn interface and authenticates requests
|
|
|
|
// via the built-in SSPI module in Windows for SPNEGO authentication.
|
2023-09-17 17:32:56 -06:00
|
|
|
// The SSPI plugin is expected to be executed last, as it returns 401 status code if negotiation
|
|
|
|
// fails (or if negotiation should continue), which would prevent other authentication methods
|
|
|
|
// to execute at all.
|
2023-04-13 13:45:33 -06:00
|
|
|
type SSPI struct{}
|
2019-11-22 16:33:31 -07:00
|
|
|
|
2021-06-09 11:53:16 -06:00
|
|
|
// Name represents the name of auth method
|
|
|
|
func (s *SSPI) Name() string {
|
|
|
|
return "sspi"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify uses SSPI (Windows implementation of SPNEGO) to authenticate the request.
|
2021-07-08 05:38:13 -06:00
|
|
|
// If authentication is successful, returns the corresponding user object.
|
2019-11-22 16:33:31 -07:00
|
|
|
// If negotiation should continue or authentication fails, immediately returns a 401 HTTP
|
|
|
|
// response code, as required by the SPNEGO protocol.
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-27 22:53:28 -07:00
|
|
|
func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
|
2023-09-17 17:32:56 -06:00
|
|
|
sspiAuthOnce.Do(func() { sspiAuthErrInit = sspiAuthInit() })
|
|
|
|
if sspiAuthErrInit != nil {
|
|
|
|
return nil, sspiAuthErrInit
|
2023-06-18 01:59:09 -06:00
|
|
|
}
|
2021-01-05 18:38:00 -07:00
|
|
|
if !s.shouldAuthenticate(req) {
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-27 22:53:28 -07:00
|
|
|
return nil, nil
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
|
2023-10-10 22:24:07 -06:00
|
|
|
cfg, err := s.getConfig(req.Context())
|
2019-11-22 16:33:31 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Error("could not get SSPI config: %v", err)
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-27 22:53:28 -07:00
|
|
|
return nil, err
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 10:04:53 -06:00
|
|
|
log.Trace("SSPI Authorization: Attempting to authenticate")
|
2021-01-05 18:38:00 -07:00
|
|
|
userInfo, outToken, err := sspiAuth.Authenticate(req, w)
|
2019-11-22 16:33:31 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("Authentication failed with error: %v\n", err)
|
2021-01-05 18:38:00 -07:00
|
|
|
sspiAuth.AppendAuthenticateHeader(w, outToken)
|
2019-11-22 16:33:31 -07:00
|
|
|
|
|
|
|
// Include the user login page in the 401 response to allow the user
|
|
|
|
// to login with another authentication method if SSPI authentication
|
|
|
|
// fails
|
2021-01-05 18:38:00 -07:00
|
|
|
store.GetData()["Flash"] = map[string]string{
|
2021-01-14 08:35:10 -07:00
|
|
|
"ErrorMsg": err.Error(),
|
2021-01-05 18:38:00 -07:00
|
|
|
}
|
|
|
|
store.GetData()["EnableOpenIDSignIn"] = setting.Service.EnableOpenIDSignIn
|
|
|
|
store.GetData()["EnableSSPI"] = true
|
2023-07-18 11:32:49 -06:00
|
|
|
// in this case, the Verify function is called in Gitea's web context
|
2023-04-13 13:45:33 -06:00
|
|
|
// FIXME: it doesn't look good to render the page here, why not redirect?
|
2023-07-18 11:32:49 -06:00
|
|
|
gitea_context.GetWebContext(req).HTML(http.StatusUnauthorized, tplSignIn)
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-27 22:53:28 -07:00
|
|
|
return nil, err
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
if outToken != "" {
|
2021-01-05 18:38:00 -07:00
|
|
|
sspiAuth.AppendAuthenticateHeader(w, outToken)
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
username := sanitizeUsername(userInfo.Username, cfg)
|
|
|
|
if len(username) == 0 {
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-27 22:53:28 -07:00
|
|
|
return nil, nil
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
log.Info("Authenticated as %s\n", username)
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
user, err := user_model.GetUserByName(req.Context(), username)
|
2019-11-22 16:33:31 -07:00
|
|
|
if err != nil {
|
2021-11-24 02:49:20 -07:00
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2019-11-22 16:33:31 -07:00
|
|
|
log.Error("GetUserByName: %v", err)
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-27 22:53:28 -07:00
|
|
|
return nil, err
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
if !cfg.AutoCreateUsers {
|
|
|
|
log.Error("User '%s' not found", username)
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-27 22:53:28 -07:00
|
|
|
return nil, nil
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
2023-09-14 11:09:32 -06:00
|
|
|
user, err = s.newUser(req.Context(), username, cfg)
|
2019-11-22 16:33:31 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Error("CreateUser: %v", err)
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-27 22:53:28 -07:00
|
|
|
return nil, err
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure requests to API paths and PWA resources do not create a new session
|
2021-01-30 01:55:53 -07:00
|
|
|
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) {
|
2021-01-05 18:38:00 -07:00
|
|
|
handleSignIn(w, req, sess, user)
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 10:04:53 -06:00
|
|
|
log.Trace("SSPI Authorization: Logged in user %-v", user)
|
refactor auth interface to return error when verify failure (#22119)
This PR changed the Auth interface signature from
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) *user_model.User`
to
`Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess
SessionStore) (*user_model.User, error)`.
There is a new return argument `error` which means the verification
condition matched but verify process failed, we should stop the auth
process.
Before this PR, when return a `nil` user, we don't know the reason why
it returned `nil`. If the match condition is not satisfied or it
verified failure? For these two different results, we should have
different handler. If the match condition is not satisfied, we should
try next auth method and if there is no more auth method, it's an
anonymous user. If the condition matched but verify failed, the auth
process should be stop and return immediately.
This will fix #20563
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Jason Song <i@wolfogre.com>
2022-12-27 22:53:28 -07:00
|
|
|
return user, nil
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// getConfig retrieves the SSPI configuration from login sources
|
2023-10-10 22:24:07 -06:00
|
|
|
func (s *SSPI) getConfig(ctx context.Context) (*sspi.Source, error) {
|
2023-11-23 20:49:41 -07:00
|
|
|
sources, err := db.Find[auth.Source](ctx, auth.FindSourcesOptions{
|
2024-03-02 08:42:31 -07:00
|
|
|
IsActive: optional.Some(true),
|
2023-11-02 19:41:00 -06:00
|
|
|
LoginType: auth.SSPI,
|
|
|
|
})
|
2019-11-22 16:33:31 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(sources) == 0 {
|
|
|
|
return nil, errors.New("no active login sources of type SSPI found")
|
|
|
|
}
|
|
|
|
if len(sources) > 1 {
|
|
|
|
return nil, errors.New("more than one active login source of type SSPI found")
|
|
|
|
}
|
2021-07-24 04:16:34 -06:00
|
|
|
return sources[0].Cfg.(*sspi.Source), nil
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
|
2021-01-05 06:05:40 -07:00
|
|
|
func (s *SSPI) shouldAuthenticate(req *http.Request) (shouldAuth bool) {
|
2019-11-22 16:33:31 -07:00
|
|
|
shouldAuth = false
|
2021-01-05 06:05:40 -07:00
|
|
|
path := strings.TrimSuffix(req.URL.Path, "/")
|
2019-11-22 16:33:31 -07:00
|
|
|
if path == "/user/login" {
|
2021-01-05 06:05:40 -07:00
|
|
|
if req.FormValue("user_name") != "" && req.FormValue("password") != "" {
|
2019-11-22 16:33:31 -07:00
|
|
|
shouldAuth = false
|
2021-01-05 18:38:00 -07:00
|
|
|
} else if req.FormValue("auth_with_sspi") == "1" {
|
2019-11-22 16:33:31 -07:00
|
|
|
shouldAuth = true
|
|
|
|
}
|
2021-01-30 01:55:53 -07:00
|
|
|
} else if middleware.IsAPIPath(req) || isAttachmentDownload(req) {
|
2019-11-22 16:33:31 -07:00
|
|
|
shouldAuth = true
|
|
|
|
}
|
2022-06-20 04:02:49 -06:00
|
|
|
return shouldAuth
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// newUser creates a new user object for the purpose of automatic registration
|
|
|
|
// and populates its name and email with the information present in request headers.
|
2023-09-14 11:09:32 -06:00
|
|
|
func (s *SSPI) newUser(ctx context.Context, username string, cfg *sspi.Source) (*user_model.User, error) {
|
2020-06-18 03:18:44 -06:00
|
|
|
email := gouuid.New().String() + "@localhost.localdomain"
|
2021-11-24 02:49:20 -07:00
|
|
|
user := &user_model.User{
|
2024-01-27 02:27:34 -07:00
|
|
|
Name: username,
|
|
|
|
Email: email,
|
|
|
|
Language: cfg.DefaultLanguage,
|
2019-11-22 16:33:31 -07:00
|
|
|
}
|
2022-04-29 13:38:11 -06:00
|
|
|
emailNotificationPreference := user_model.EmailNotificationsDisabled
|
|
|
|
overwriteDefault := &user_model.CreateUserOverwriteOptions{
|
2024-02-22 19:18:33 -07:00
|
|
|
IsActive: optional.Some(cfg.AutoActivateUsers),
|
|
|
|
KeepEmailPrivate: optional.Some(true),
|
2022-04-29 13:38:11 -06:00
|
|
|
EmailNotificationsPreference: &emailNotificationPreference,
|
|
|
|
}
|
2024-09-09 15:05:16 -06:00
|
|
|
if err := user_model.CreateUser(ctx, user, &user_model.Meta{}, overwriteDefault); err != nil {
|
2019-11-22 16:33:31 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-12 01:26:33 -06:00
|
|
|
|
2019-11-22 16:33:31 -07:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stripDomainNames removes NETBIOS domain name and separator from down-level logon names
|
|
|
|
// (eg. "DOMAIN\user" becomes "user"), and removes the UPN suffix (domain name) and separator
|
|
|
|
// from UPNs (eg. "user@domain.local" becomes "user")
|
|
|
|
func stripDomainNames(username string) string {
|
|
|
|
if strings.Contains(username, "\\") {
|
|
|
|
parts := strings.SplitN(username, "\\", 2)
|
|
|
|
if len(parts) > 1 {
|
|
|
|
username = parts[1]
|
|
|
|
}
|
|
|
|
} else if strings.Contains(username, "@") {
|
|
|
|
parts := strings.Split(username, "@")
|
|
|
|
if len(parts) > 1 {
|
|
|
|
username = parts[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return username
|
|
|
|
}
|
|
|
|
|
2021-07-24 04:16:34 -06:00
|
|
|
func replaceSeparators(username string, cfg *sspi.Source) string {
|
2019-11-22 16:33:31 -07:00
|
|
|
newSep := cfg.SeparatorReplacement
|
|
|
|
username = strings.ReplaceAll(username, "\\", newSep)
|
|
|
|
username = strings.ReplaceAll(username, "/", newSep)
|
|
|
|
username = strings.ReplaceAll(username, "@", newSep)
|
|
|
|
return username
|
|
|
|
}
|
|
|
|
|
2021-07-24 04:16:34 -06:00
|
|
|
func sanitizeUsername(username string, cfg *sspi.Source) string {
|
2019-11-22 16:33:31 -07:00
|
|
|
if len(username) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if cfg.StripDomainNames {
|
|
|
|
username = stripDomainNames(username)
|
|
|
|
}
|
|
|
|
// Replace separators even if we have already stripped the domain name part,
|
|
|
|
// as the username can contain several separators: eg. "MICROSOFT\useremail@live.com"
|
|
|
|
username = replaceSeparators(username, cfg)
|
|
|
|
return username
|
|
|
|
}
|