Skip to content

Commit f503d60

Browse files
lunnyStelios Malathouras
authored and
Stelios Malathouras
committed
Refactor auth package (go-gitea#17962)
1 parent d31e309 commit f503d60

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2880
-2770
lines changed

cmd/admin.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515

1616
"code.gitea.io/gitea/models"
1717
asymkey_model "code.gitea.io/gitea/models/asymkey"
18+
"code.gitea.io/gitea/models/auth"
1819
"code.gitea.io/gitea/models/db"
19-
"code.gitea.io/gitea/models/login"
2020
user_model "code.gitea.io/gitea/models/user"
2121
"code.gitea.io/gitea/modules/git"
2222
"code.gitea.io/gitea/modules/graceful"
@@ -700,8 +700,8 @@ func runAddOauth(c *cli.Context) error {
700700
return err
701701
}
702702

703-
return login.CreateSource(&login.Source{
704-
Type: login.OAuth2,
703+
return auth.CreateSource(&auth.Source{
704+
Type: auth.OAuth2,
705705
Name: c.String("name"),
706706
IsActive: true,
707707
Cfg: parseOAuth2Config(c),
@@ -720,7 +720,7 @@ func runUpdateOauth(c *cli.Context) error {
720720
return err
721721
}
722722

723-
source, err := login.GetSourceByID(c.Int64("id"))
723+
source, err := auth.GetSourceByID(c.Int64("id"))
724724
if err != nil {
725725
return err
726726
}
@@ -801,7 +801,7 @@ func runUpdateOauth(c *cli.Context) error {
801801
oAuth2Config.CustomURLMapping = customURLMapping
802802
source.Cfg = oAuth2Config
803803

804-
return login.UpdateSource(source)
804+
return auth.UpdateSource(source)
805805
}
806806

807807
func runListAuth(c *cli.Context) error {
@@ -812,7 +812,7 @@ func runListAuth(c *cli.Context) error {
812812
return err
813813
}
814814

815-
loginSources, err := login.Sources()
815+
authSources, err := auth.Sources()
816816

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

854-
source, err := login.GetSourceByID(c.Int64("id"))
854+
source, err := auth.GetSourceByID(c.Int64("id"))
855855
if err != nil {
856856
return err
857857
}
858858

859-
return auth_service.DeleteLoginSource(source)
859+
return auth_service.DeleteSource(source)
860860
}

cmd/admin_auth_ldap.go

+38-38
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import (
99
"fmt"
1010
"strings"
1111

12-
"code.gitea.io/gitea/models/login"
12+
"code.gitea.io/gitea/models/auth"
1313
"code.gitea.io/gitea/services/auth/source/ldap"
1414

1515
"github.com/urfave/cli"
1616
)
1717

1818
type (
1919
authService struct {
20-
initDB func(ctx context.Context) error
21-
createLoginSource func(loginSource *login.Source) error
22-
updateLoginSource func(loginSource *login.Source) error
23-
getLoginSourceByID func(id int64) (*login.Source, error)
20+
initDB func(ctx context.Context) error
21+
createAuthSource func(*auth.Source) error
22+
updateAuthSource func(*auth.Source) error
23+
getAuthSourceByID func(id int64) (*auth.Source, error)
2424
}
2525
)
2626

@@ -168,23 +168,23 @@ var (
168168
// newAuthService creates a service with default functions.
169169
func newAuthService() *authService {
170170
return &authService{
171-
initDB: initDB,
172-
createLoginSource: login.CreateSource,
173-
updateLoginSource: login.UpdateSource,
174-
getLoginSourceByID: login.GetSourceByID,
171+
initDB: initDB,
172+
createAuthSource: auth.CreateSource,
173+
updateAuthSource: auth.UpdateSource,
174+
getAuthSourceByID: auth.GetSourceByID,
175175
}
176176
}
177177

178-
// parseLoginSource assigns values on loginSource according to command line flags.
179-
func parseLoginSource(c *cli.Context, loginSource *login.Source) {
178+
// parseAuthSource assigns values on authSource according to command line flags.
179+
func parseAuthSource(c *cli.Context, authSource *auth.Source) {
180180
if c.IsSet("name") {
181-
loginSource.Name = c.String("name")
181+
authSource.Name = c.String("name")
182182
}
183183
if c.IsSet("not-active") {
184-
loginSource.IsActive = !c.Bool("not-active")
184+
authSource.IsActive = !c.Bool("not-active")
185185
}
186186
if c.IsSet("synchronize-users") {
187-
loginSource.IsSyncEnabled = c.Bool("synchronize-users")
187+
authSource.IsSyncEnabled = c.Bool("synchronize-users")
188188
}
189189
}
190190

@@ -275,23 +275,23 @@ func findLdapSecurityProtocolByName(name string) (ldap.SecurityProtocol, bool) {
275275
return 0, false
276276
}
277277

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

285-
loginSource, err := a.getLoginSourceByID(c.Int64("id"))
285+
authSource, err := a.getAuthSourceByID(c.Int64("id"))
286286
if err != nil {
287287
return nil, err
288288
}
289289

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

294-
return loginSource, nil
294+
return authSource, nil
295295
}
296296

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

310-
loginSource := &login.Source{
311-
Type: login.LDAP,
310+
authSource := &auth.Source{
311+
Type: auth.LDAP,
312312
IsActive: true, // active by default
313313
Cfg: &ldap.Source{
314314
Enabled: true, // always true
315315
},
316316
}
317317

318-
parseLoginSource(c, loginSource)
319-
if err := parseLdapConfig(c, loginSource.Cfg.(*ldap.Source)); err != nil {
318+
parseAuthSource(c, authSource)
319+
if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
320320
return err
321321
}
322322

323-
return a.createLoginSource(loginSource)
323+
return a.createAuthSource(authSource)
324324
}
325325

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

335-
loginSource, err := a.getLoginSource(c, login.LDAP)
335+
authSource, err := a.getAuthSource(c, auth.LDAP)
336336
if err != nil {
337337
return err
338338
}
339339

340-
parseLoginSource(c, loginSource)
341-
if err := parseLdapConfig(c, loginSource.Cfg.(*ldap.Source)); err != nil {
340+
parseAuthSource(c, authSource)
341+
if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
342342
return err
343343
}
344344

345-
return a.updateLoginSource(loginSource)
345+
return a.updateAuthSource(authSource)
346346
}
347347

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

361-
loginSource := &login.Source{
362-
Type: login.DLDAP,
361+
authSource := &auth.Source{
362+
Type: auth.DLDAP,
363363
IsActive: true, // active by default
364364
Cfg: &ldap.Source{
365365
Enabled: true, // always true
366366
},
367367
}
368368

369-
parseLoginSource(c, loginSource)
370-
if err := parseLdapConfig(c, loginSource.Cfg.(*ldap.Source)); err != nil {
369+
parseAuthSource(c, authSource)
370+
if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
371371
return err
372372
}
373373

374-
return a.createLoginSource(loginSource)
374+
return a.createAuthSource(authSource)
375375
}
376376

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

386-
loginSource, err := a.getLoginSource(c, login.DLDAP)
386+
authSource, err := a.getAuthSource(c, auth.DLDAP)
387387
if err != nil {
388388
return err
389389
}
390390

391-
parseLoginSource(c, loginSource)
392-
if err := parseLdapConfig(c, loginSource.Cfg.(*ldap.Source)); err != nil {
391+
parseAuthSource(c, authSource)
392+
if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
393393
return err
394394
}
395395

396-
return a.updateLoginSource(loginSource)
396+
return a.updateAuthSource(authSource)
397397
}

0 commit comments

Comments
 (0)