mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-07-08 19:00:02 +02:00
Refactor: Move login out of models (#16199)
`models` does far too much. In particular it handles all `UserSignin`. It shouldn't be responsible for calling LDAP, SMTP or PAM for signing in. Therefore we should move this code out of `models`. This code has to depend on `models` - therefore it belongs in `services`. There is a package in `services` called `auth` and clearly this functionality belongs in there. Plan: - [x] Change `auth.Auth` to `auth.Method` - as they represent methods of authentication. - [x] Move `models.UserSignIn` into `auth` - [x] Move `models.ExternalUserLogin` - [x] Move most of the `LoginVia*` methods to `auth` or subpackages - [x] Move Resynchronize functionality to `auth` - Involved some restructuring of `models/ssh_key.go` to reduce the size of this massive file and simplify its files. - [x] Move the rest of the LDAP functionality in to the ldap subpackage - [x] Re-factor the login sources to express an interfaces `auth.Source`? - I've done this through some smaller interfaces Authenticator and Synchronizable - which would allow us to extend things in future - [x] Now LDAP is out of models - need to think about modules/auth/ldap and I think all of that functionality might just be moveable - [x] Similarly a lot Oauth2 functionality need not be in models too and should be moved to services/auth/source/oauth2 - [x] modules/auth/oauth2/oauth2.go uses xorm... This is naughty - probably need to move this into models. - [x] models/oauth2.go - mostly should be in modules/auth/oauth2 or services/auth/source/oauth2 - [x] More simplifications of login_source.go may need to be done - Allow wiring in of notify registration - *this can now easily be done - but I think we should do it in another PR* - see #16178 - More refactors...? - OpenID should probably become an auth Method but I think that can be left for another PR - Methods should also probably be cleaned up - again another PR I think. - SSPI still needs more refactors.* Rename auth.Auth auth.Method * Restructure ssh_key.go - move functions from models/user.go that relate to ssh_key to ssh_key - split ssh_key.go to try create clearer function domains for allow for future refactors here. Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
parent
f135a818f5
commit
5d2e11eedb
77 changed files with 3803 additions and 2951 deletions
|
@ -15,6 +15,7 @@ import (
|
|||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/templates"
|
||||
"code.gitea.io/gitea/modules/web/middleware"
|
||||
"code.gitea.io/gitea/services/auth/source/sspi"
|
||||
|
||||
gouuid "github.com/google/uuid"
|
||||
"github.com/quasoft/websspi"
|
||||
|
@ -32,7 +33,10 @@ var (
|
|||
sspiAuth *websspi.Authenticator
|
||||
|
||||
// Ensure the struct implements the interface.
|
||||
_ Auth = &SSPI{}
|
||||
_ Method = &SSPI{}
|
||||
_ Named = &SSPI{}
|
||||
_ Initializable = &SSPI{}
|
||||
_ Freeable = &SSPI{}
|
||||
)
|
||||
|
||||
// SSPI implements the SingleSignOn interface and authenticates requests
|
||||
|
@ -146,7 +150,7 @@ func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore,
|
|||
}
|
||||
|
||||
// getConfig retrieves the SSPI configuration from login sources
|
||||
func (s *SSPI) getConfig() (*models.SSPIConfig, error) {
|
||||
func (s *SSPI) getConfig() (*sspi.Source, error) {
|
||||
sources, err := models.ActiveLoginSources(models.LoginSSPI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -157,7 +161,7 @@ func (s *SSPI) getConfig() (*models.SSPIConfig, error) {
|
|||
if len(sources) > 1 {
|
||||
return nil, errors.New("more than one active login source of type SSPI found")
|
||||
}
|
||||
return sources[0].SSPI(), nil
|
||||
return sources[0].Cfg.(*sspi.Source), nil
|
||||
}
|
||||
|
||||
func (s *SSPI) shouldAuthenticate(req *http.Request) (shouldAuth bool) {
|
||||
|
@ -177,7 +181,7 @@ func (s *SSPI) shouldAuthenticate(req *http.Request) (shouldAuth bool) {
|
|||
|
||||
// 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.
|
||||
func (s *SSPI) newUser(username string, cfg *models.SSPIConfig) (*models.User, error) {
|
||||
func (s *SSPI) newUser(username string, cfg *sspi.Source) (*models.User, error) {
|
||||
email := gouuid.New().String() + "@localhost.localdomain"
|
||||
user := &models.User{
|
||||
Name: username,
|
||||
|
@ -214,7 +218,7 @@ func stripDomainNames(username string) string {
|
|||
return username
|
||||
}
|
||||
|
||||
func replaceSeparators(username string, cfg *models.SSPIConfig) string {
|
||||
func replaceSeparators(username string, cfg *sspi.Source) string {
|
||||
newSep := cfg.SeparatorReplacement
|
||||
username = strings.ReplaceAll(username, "\\", newSep)
|
||||
username = strings.ReplaceAll(username, "/", newSep)
|
||||
|
@ -222,7 +226,7 @@ func replaceSeparators(username string, cfg *models.SSPIConfig) string {
|
|||
return username
|
||||
}
|
||||
|
||||
func sanitizeUsername(username string, cfg *models.SSPIConfig) string {
|
||||
func sanitizeUsername(username string, cfg *sspi.Source) string {
|
||||
if len(username) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue