Skip to content

Commit

Permalink
Refactor auth package (go-gitea#17962)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Jan 2, 2022
1 parent e61b390 commit de8e394
Show file tree
Hide file tree
Showing 87 changed files with 2,880 additions and 2,770 deletions.
18 changes: 9 additions & 9 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
Expand Down Expand Up @@ -700,8 +700,8 @@ func runAddOauth(c *cli.Context) error {
return err
}

return login.CreateSource(&login.Source{
Type: login.OAuth2,
return auth.CreateSource(&auth.Source{
Type: auth.OAuth2,
Name: c.String("name"),
IsActive: true,
Cfg: parseOAuth2Config(c),
Expand All @@ -720,7 +720,7 @@ func runUpdateOauth(c *cli.Context) error {
return err
}

source, err := login.GetSourceByID(c.Int64("id"))
source, err := auth.GetSourceByID(c.Int64("id"))
if err != nil {
return err
}
Expand Down Expand Up @@ -801,7 +801,7 @@ func runUpdateOauth(c *cli.Context) error {
oAuth2Config.CustomURLMapping = customURLMapping
source.Cfg = oAuth2Config

return login.UpdateSource(source)
return auth.UpdateSource(source)
}

func runListAuth(c *cli.Context) error {
Expand All @@ -812,7 +812,7 @@ func runListAuth(c *cli.Context) error {
return err
}

loginSources, err := login.Sources()
authSources, err := auth.Sources()

if err != nil {
return err
Expand All @@ -831,7 +831,7 @@ func runListAuth(c *cli.Context) error {
// loop through each source and print
w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
for _, source := range loginSources {
for _, source := range authSources {
fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, source.Type.String(), source.IsActive)
}
w.Flush()
Expand All @@ -851,10 +851,10 @@ func runDeleteAuth(c *cli.Context) error {
return err
}

source, err := login.GetSourceByID(c.Int64("id"))
source, err := auth.GetSourceByID(c.Int64("id"))
if err != nil {
return err
}

return auth_service.DeleteLoginSource(source)
return auth_service.DeleteSource(source)
}
76 changes: 38 additions & 38 deletions cmd/admin_auth_ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
"fmt"
"strings"

"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/services/auth/source/ldap"

"github.com/urfave/cli"
)

type (
authService struct {
initDB func(ctx context.Context) error
createLoginSource func(loginSource *login.Source) error
updateLoginSource func(loginSource *login.Source) error
getLoginSourceByID func(id int64) (*login.Source, error)
initDB func(ctx context.Context) error
createAuthSource func(*auth.Source) error
updateAuthSource func(*auth.Source) error
getAuthSourceByID func(id int64) (*auth.Source, error)
}
)

Expand Down Expand Up @@ -168,23 +168,23 @@ var (
// newAuthService creates a service with default functions.
func newAuthService() *authService {
return &authService{
initDB: initDB,
createLoginSource: login.CreateSource,
updateLoginSource: login.UpdateSource,
getLoginSourceByID: login.GetSourceByID,
initDB: initDB,
createAuthSource: auth.CreateSource,
updateAuthSource: auth.UpdateSource,
getAuthSourceByID: auth.GetSourceByID,
}
}

// parseLoginSource assigns values on loginSource according to command line flags.
func parseLoginSource(c *cli.Context, loginSource *login.Source) {
// parseAuthSource assigns values on authSource according to command line flags.
func parseAuthSource(c *cli.Context, authSource *auth.Source) {
if c.IsSet("name") {
loginSource.Name = c.String("name")
authSource.Name = c.String("name")
}
if c.IsSet("not-active") {
loginSource.IsActive = !c.Bool("not-active")
authSource.IsActive = !c.Bool("not-active")
}
if c.IsSet("synchronize-users") {
loginSource.IsSyncEnabled = c.Bool("synchronize-users")
authSource.IsSyncEnabled = c.Bool("synchronize-users")
}
}

Expand Down Expand Up @@ -275,23 +275,23 @@ func findLdapSecurityProtocolByName(name string) (ldap.SecurityProtocol, bool) {
return 0, false
}

// getLoginSource gets the login source by its id defined in the command line flags.
// getAuthSource gets the login source by its id defined in the command line flags.
// It returns an error if the id is not set, does not match any source or if the source is not of expected type.
func (a *authService) getLoginSource(c *cli.Context, loginType login.Type) (*login.Source, error) {
func (a *authService) getAuthSource(c *cli.Context, authType auth.Type) (*auth.Source, error) {
if err := argsSet(c, "id"); err != nil {
return nil, err
}

loginSource, err := a.getLoginSourceByID(c.Int64("id"))
authSource, err := a.getAuthSourceByID(c.Int64("id"))
if err != nil {
return nil, err
}

if loginSource.Type != loginType {
return nil, fmt.Errorf("Invalid authentication type. expected: %s, actual: %s", loginType.String(), loginSource.Type.String())
if authSource.Type != authType {
return nil, fmt.Errorf("Invalid authentication type. expected: %s, actual: %s", authType.String(), authSource.Type.String())
}

return loginSource, nil
return authSource, nil
}

// addLdapBindDn adds a new LDAP via Bind DN authentication source.
Expand All @@ -307,20 +307,20 @@ func (a *authService) addLdapBindDn(c *cli.Context) error {
return err
}

loginSource := &login.Source{
Type: login.LDAP,
authSource := &auth.Source{
Type: auth.LDAP,
IsActive: true, // active by default
Cfg: &ldap.Source{
Enabled: true, // always true
},
}

parseLoginSource(c, loginSource)
if err := parseLdapConfig(c, loginSource.Cfg.(*ldap.Source)); err != nil {
parseAuthSource(c, authSource)
if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
return err
}

return a.createLoginSource(loginSource)
return a.createAuthSource(authSource)
}

// updateLdapBindDn updates a new LDAP via Bind DN authentication source.
Expand All @@ -332,17 +332,17 @@ func (a *authService) updateLdapBindDn(c *cli.Context) error {
return err
}

loginSource, err := a.getLoginSource(c, login.LDAP)
authSource, err := a.getAuthSource(c, auth.LDAP)
if err != nil {
return err
}

parseLoginSource(c, loginSource)
if err := parseLdapConfig(c, loginSource.Cfg.(*ldap.Source)); err != nil {
parseAuthSource(c, authSource)
if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
return err
}

return a.updateLoginSource(loginSource)
return a.updateAuthSource(authSource)
}

// addLdapSimpleAuth adds a new LDAP (simple auth) authentication source.
Expand All @@ -358,20 +358,20 @@ func (a *authService) addLdapSimpleAuth(c *cli.Context) error {
return err
}

loginSource := &login.Source{
Type: login.DLDAP,
authSource := &auth.Source{
Type: auth.DLDAP,
IsActive: true, // active by default
Cfg: &ldap.Source{
Enabled: true, // always true
},
}

parseLoginSource(c, loginSource)
if err := parseLdapConfig(c, loginSource.Cfg.(*ldap.Source)); err != nil {
parseAuthSource(c, authSource)
if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
return err
}

return a.createLoginSource(loginSource)
return a.createAuthSource(authSource)
}

// updateLdapBindDn updates a new LDAP (simple auth) authentication source.
Expand All @@ -383,15 +383,15 @@ func (a *authService) updateLdapSimpleAuth(c *cli.Context) error {
return err
}

loginSource, err := a.getLoginSource(c, login.DLDAP)
authSource, err := a.getAuthSource(c, auth.DLDAP)
if err != nil {
return err
}

parseLoginSource(c, loginSource)
if err := parseLdapConfig(c, loginSource.Cfg.(*ldap.Source)); err != nil {
parseAuthSource(c, authSource)
if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
return err
}

return a.updateLoginSource(loginSource)
return a.updateAuthSource(authSource)
}
Loading

0 comments on commit de8e394

Please sign in to comment.