2019-11-23 05:03:31 +05:30
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2021-06-09 23:23:16 +05:30
|
|
|
package auth
|
2019-11-23 05:03:31 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-01-05 18:35:40 +05:30
|
|
|
"net/http"
|
2019-11-23 05:03:31 +05:30
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-06 07:08:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2021-01-30 14:25:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2021-07-24 15:46:34 +05:30
|
|
|
"code.gitea.io/gitea/services/auth/source/sspi"
|
2019-11-23 05:03:31 +05:30
|
|
|
|
2020-06-18 14:48:44 +05:30
|
|
|
gouuid "github.com/google/uuid"
|
2019-11-23 05:03:31 +05:30
|
|
|
"github.com/quasoft/websspi"
|
2021-01-06 07:08:00 +05:30
|
|
|
"github.com/unrolled/render"
|
2019-11-23 05:03:31 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplSignIn base.TplName = "user/auth/signin"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// sspiAuth is a global instance of the websspi authentication package,
|
|
|
|
// which is used to avoid acquiring the server credential handle on
|
|
|
|
// every request
|
|
|
|
sspiAuth *websspi.Authenticator
|
|
|
|
|
|
|
|
// Ensure the struct implements the interface.
|
2021-07-24 15:46:34 +05:30
|
|
|
_ Method = &SSPI{}
|
|
|
|
_ Named = &SSPI{}
|
|
|
|
_ Initializable = &SSPI{}
|
|
|
|
_ Freeable = &SSPI{}
|
2019-11-23 05:03:31 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// SSPI implements the SingleSignOn interface and authenticates requests
|
|
|
|
// via the built-in SSPI module in Windows for SPNEGO authentication.
|
|
|
|
// On successful authentication returns a valid user object.
|
|
|
|
// Returns nil if authentication fails.
|
|
|
|
type SSPI struct {
|
2021-01-06 07:08:00 +05:30
|
|
|
rnd *render.Render
|
2019-11-23 05:03:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Init creates a new global websspi.Authenticator object
|
|
|
|
func (s *SSPI) Init() error {
|
|
|
|
config := websspi.NewConfig()
|
|
|
|
var err error
|
|
|
|
sspiAuth, err = websspi.New(config)
|
2021-01-06 07:08:00 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.rnd = render.New(render.Options{
|
|
|
|
Extensions: []string{".tmpl"},
|
|
|
|
Directory: "templates",
|
|
|
|
Funcs: templates.NewFuncMap(),
|
|
|
|
Asset: templates.GetAsset,
|
|
|
|
AssetNames: templates.GetAssetNames,
|
2021-01-15 02:47:03 +05:30
|
|
|
IsDevelopment: !setting.IsProd(),
|
2021-01-06 07:08:00 +05:30
|
|
|
})
|
|
|
|
return nil
|
2019-11-23 05:03:31 +05:30
|
|
|
}
|
|
|
|
|
2021-06-09 23:23:16 +05:30
|
|
|
// Name represents the name of auth method
|
|
|
|
func (s *SSPI) Name() string {
|
|
|
|
return "sspi"
|
|
|
|
}
|
|
|
|
|
2019-11-23 05:03:31 +05:30
|
|
|
// Free releases resources used by the global websspi.Authenticator object
|
|
|
|
func (s *SSPI) Free() error {
|
|
|
|
return sspiAuth.Free()
|
|
|
|
}
|
|
|
|
|
2021-06-09 23:23:16 +05:30
|
|
|
// Verify uses SSPI (Windows implementation of SPNEGO) to authenticate the request.
|
2021-07-08 17:08:13 +05:30
|
|
|
// If authentication is successful, returns the corresponding user object.
|
2019-11-23 05:03:31 +05:30
|
|
|
// If negotiation should continue or authentication fails, immediately returns a 401 HTTP
|
|
|
|
// response code, as required by the SPNEGO protocol.
|
2021-06-09 23:23:16 +05:30
|
|
|
func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
|
2021-01-06 07:08:00 +05:30
|
|
|
if !s.shouldAuthenticate(req) {
|
2019-11-23 05:03:31 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := s.getConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("could not get SSPI config: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:34:53 +05:30
|
|
|
log.Trace("SSPI Authorization: Attempting to authenticate")
|
2021-01-06 07:08:00 +05:30
|
|
|
userInfo, outToken, err := sspiAuth.Authenticate(req, w)
|
2019-11-23 05:03:31 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Warn("Authentication failed with error: %v\n", err)
|
2021-01-06 07:08:00 +05:30
|
|
|
sspiAuth.AppendAuthenticateHeader(w, outToken)
|
2019-11-23 05:03:31 +05:30
|
|
|
|
|
|
|
// 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-06 07:08:00 +05:30
|
|
|
store.GetData()["Flash"] = map[string]string{
|
2021-01-14 21:05:10 +05:30
|
|
|
"ErrorMsg": err.Error(),
|
2021-01-06 07:08:00 +05:30
|
|
|
}
|
|
|
|
store.GetData()["EnableOpenIDSignIn"] = setting.Service.EnableOpenIDSignIn
|
|
|
|
store.GetData()["EnableSSPI"] = true
|
|
|
|
|
|
|
|
err := s.rnd.HTML(w, 401, string(tplSignIn), templates.BaseVars().Merge(store.GetData()))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-23 05:03:31 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if outToken != "" {
|
2021-01-06 07:08:00 +05:30
|
|
|
sspiAuth.AppendAuthenticateHeader(w, outToken)
|
2019-11-23 05:03:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
username := sanitizeUsername(userInfo.Username, cfg)
|
|
|
|
if len(username) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Info("Authenticated as %s\n", username)
|
|
|
|
|
|
|
|
user, err := models.GetUserByName(username)
|
|
|
|
if err != nil {
|
|
|
|
if !models.IsErrUserNotExist(err) {
|
|
|
|
log.Error("GetUserByName: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !cfg.AutoCreateUsers {
|
|
|
|
log.Error("User '%s' not found", username)
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-06 07:08:00 +05:30
|
|
|
user, err = s.newUser(username, cfg)
|
2019-11-23 05:03:31 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("CreateUser: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure requests to API paths and PWA resources do not create a new session
|
2021-01-30 14:25:53 +05:30
|
|
|
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) {
|
2021-01-06 07:08:00 +05:30
|
|
|
handleSignIn(w, req, sess, user)
|
2019-11-23 05:03:31 +05:30
|
|
|
}
|
|
|
|
|
2021-05-09 21:34:53 +05:30
|
|
|
log.Trace("SSPI Authorization: Logged in user %-v", user)
|
2019-11-23 05:03:31 +05:30
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConfig retrieves the SSPI configuration from login sources
|
2021-07-24 15:46:34 +05:30
|
|
|
func (s *SSPI) getConfig() (*sspi.Source, error) {
|
2019-11-23 05:03:31 +05:30
|
|
|
sources, err := models.ActiveLoginSources(models.LoginSSPI)
|
|
|
|
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 15:46:34 +05:30
|
|
|
return sources[0].Cfg.(*sspi.Source), nil
|
2019-11-23 05:03:31 +05:30
|
|
|
}
|
|
|
|
|
2021-01-05 18:35:40 +05:30
|
|
|
func (s *SSPI) shouldAuthenticate(req *http.Request) (shouldAuth bool) {
|
2019-11-23 05:03:31 +05:30
|
|
|
shouldAuth = false
|
2021-01-05 18:35:40 +05:30
|
|
|
path := strings.TrimSuffix(req.URL.Path, "/")
|
2019-11-23 05:03:31 +05:30
|
|
|
if path == "/user/login" {
|
2021-01-05 18:35:40 +05:30
|
|
|
if req.FormValue("user_name") != "" && req.FormValue("password") != "" {
|
2019-11-23 05:03:31 +05:30
|
|
|
shouldAuth = false
|
2021-01-06 07:08:00 +05:30
|
|
|
} else if req.FormValue("auth_with_sspi") == "1" {
|
2019-11-23 05:03:31 +05:30
|
|
|
shouldAuth = true
|
|
|
|
}
|
2021-01-30 14:25:53 +05:30
|
|
|
} else if middleware.IsAPIPath(req) || isAttachmentDownload(req) {
|
2019-11-23 05:03:31 +05:30
|
|
|
shouldAuth = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-07-24 15:46:34 +05:30
|
|
|
func (s *SSPI) newUser(username string, cfg *sspi.Source) (*models.User, error) {
|
2020-06-18 14:48:44 +05:30
|
|
|
email := gouuid.New().String() + "@localhost.localdomain"
|
2019-11-23 05:03:31 +05:30
|
|
|
user := &models.User{
|
|
|
|
Name: username,
|
|
|
|
Email: email,
|
|
|
|
KeepEmailPrivate: true,
|
2020-06-18 14:48:44 +05:30
|
|
|
Passwd: gouuid.New().String(),
|
2019-11-23 05:03:31 +05:30
|
|
|
IsActive: cfg.AutoActivateUsers,
|
|
|
|
Language: cfg.DefaultLanguage,
|
|
|
|
UseCustomAvatar: true,
|
2020-12-04 00:16:11 +05:30
|
|
|
Avatar: models.DefaultAvatarLink(),
|
2019-11-23 05:03:31 +05:30
|
|
|
EmailNotificationsPreference: models.EmailNotificationsDisabled,
|
|
|
|
}
|
|
|
|
if err := models.CreateUser(user); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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 15:46:34 +05:30
|
|
|
func replaceSeparators(username string, cfg *sspi.Source) string {
|
2019-11-23 05:03:31 +05:30
|
|
|
newSep := cfg.SeparatorReplacement
|
|
|
|
username = strings.ReplaceAll(username, "\\", newSep)
|
|
|
|
username = strings.ReplaceAll(username, "/", newSep)
|
|
|
|
username = strings.ReplaceAll(username, "@", newSep)
|
|
|
|
return username
|
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:34 +05:30
|
|
|
func sanitizeUsername(username string, cfg *sspi.Source) string {
|
2019-11-23 05:03:31 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-09 23:23:16 +05:30
|
|
|
// specialInit registers the SSPI auth method as the last method in the list.
|
2019-11-23 05:03:31 +05:30
|
|
|
// 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.
|
2021-06-09 23:23:16 +05:30
|
|
|
func specialInit() {
|
|
|
|
if models.IsSSPIEnabled() {
|
|
|
|
Register(&SSPI{})
|
|
|
|
}
|
2019-11-23 05:03:31 +05:30
|
|
|
}
|