add options for components

This commit is contained in:
ginuerzh
2023-10-26 22:20:46 +08:00
parent 6431cd8bb9
commit 9e767d6745
6 changed files with 37 additions and 11 deletions

View File

@ -2,9 +2,13 @@ package auth
import "context"
type Options struct{}
type Option func(opts *Options)
// Authenticator is an interface for user authentication.
type Authenticator interface {
Authenticate(ctx context.Context, user, password string) (id string, ok bool)
Authenticate(ctx context.Context, user, password string, opts ...Option) (id string, ok bool)
}
type authenticatorGroup struct {
@ -17,7 +21,7 @@ func AuthenticatorGroup(authers ...Authenticator) Authenticator {
}
}
func (p *authenticatorGroup) Authenticate(ctx context.Context, user, password string) (string, bool) {
func (p *authenticatorGroup) Authenticate(ctx context.Context, user, password string, opts ...Option) (string, bool) {
if len(p.authers) == 0 {
return "", false
}
@ -26,7 +30,7 @@ func (p *authenticatorGroup) Authenticate(ctx context.Context, user, password st
continue
}
if id, ok := auther.Authenticate(ctx, user, password); ok {
if id, ok := auther.Authenticate(ctx, user, password, opts...); ok {
return id, ok
}
}