Skip to content

Commit

Permalink
Update auth.CertificateAuthenticator interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cybergarage committed Dec 22, 2024
1 parent 4f2650e commit 0226432
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
49 changes: 13 additions & 36 deletions redis/auth/authenticator_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,22 @@

package auth

// CertificateAuthenticator represents an authenticator for TLS certificates.
type CertificateAuthenticator struct {
commonName string
}

// CertificateAuthenticatorOption represents an authenticator option.
type CertificateAuthenticatorOption = func(*CertificateAuthenticator)
import (
"github.com/cybergarage/go-authenticator/auth"
)

// NewCertificateAuthenticator returns a new certificate authenticator.
func NewCertificateAuthenticatorWith(opts ...CertificateAuthenticatorOption) *CertificateAuthenticator {
authenticator := &CertificateAuthenticator{
commonName: "",
}
for _, opt := range opts {
opt(authenticator)
}
// CertificateAuthenticator is the interface for authenticating a client using TLS certificates.
type CertificateAuthenticator = auth.CertificateAuthenticator

return authenticator
}
// CertificateAuthenticatorOption represents an authenticator option.
type CertificateAuthenticatorOption = auth.CertificateAuthenticatorOption

// WithCommonName returns an authenticator option to set the common name.
func WithCommonName(name string) func(*CertificateAuthenticator) {
return func(conn *CertificateAuthenticator) {
conn.commonName = name
}
// WithCertificateAuthenticatorCommonName sets the common name.
func WithCertificateAuthenticatorCommonNameRegexp(regexps ...string) CertificateAuthenticatorOption {
return auth.WithCertificateAuthenticatorCommonNameRegexp(regexps...)
}

// Authenticate authenticates the specified connection.
func (authenticator *CertificateAuthenticator) Authenticate(conn Conn) (bool, error) {
conState, ok := conn.TLSConnectionState()
if !ok {
return false, nil
}
for _, cert := range conState.PeerCertificates {
if 0 < len(authenticator.commonName) {
if cert.Subject.CommonName == authenticator.commonName {
return true, nil
}
}
}
return false, nil
// NewCertificateAuthenticator returns a new certificate authenticator.
func NewCertificateAuthenticator(opts ...CertificateAuthenticatorOption) (CertificateAuthenticator, error) {
return auth.NewCertificateAuthenticator(opts...)
}
6 changes: 6 additions & 0 deletions redis/auth/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@

package auth

import (
"github.com/cybergarage/go-authenticator/auth"
)

// AuthManager represent an authenticator manager.
type AuthManager struct {
auth.Manager
authenticators []Authenticator
}

// NewAuthManager returns a new authenticator manager.
func NewAuthManager() *AuthManager {
manager := &AuthManager{
Manager: auth.NewManager(),
authenticators: make([]Authenticator, 0),
}
return manager
Expand Down
7 changes: 6 additions & 1 deletion redistest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ func TestTLSServer(t *testing.T) {
server.SetTLSCertFile(serverCert)
server.SetTLSCaCertFile(rootCert)

server.AddAuthenticator(auth.NewCertificateAuthenticatorWith(auth.WithCommonName("localhost")))
ca, err := auth.NewCertificateAuthenticator(auth.WithCertificateAuthenticatorCommonNameRegexp("localhost"))
if err != nil {
t.Error(err)
return
}
server.SetCertificateAuthenticator(ca)

err = server.Start()
if err != nil {
Expand Down

0 comments on commit 0226432

Please sign in to comment.