Skip to content

Commit

Permalink
imap: add Authenticatable interface
Browse files Browse the repository at this point in the history
Used by Authenticator, mainly to allow mocking for testing.
  • Loading branch information
vs49688 committed Jul 30, 2022
1 parent 5083bfd commit 816a920
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 3 additions & 4 deletions imap/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package imap

import (
"github.com/emersion/go-imap/client"
"github.com/emersion/go-sasl"
"golang.org/x/oauth2"
)
Expand All @@ -33,7 +32,7 @@ func NewNormalAuthenticator(username string, password string) Authenticator {
return &plainAuthenticator{username: username, password: password}
}

func (a *plainAuthenticator) Authenticate(c *client.Client) error {
func (a *plainAuthenticator) Authenticate(c Authenticatable) error {
return c.Login(a.username, a.password)
}

Expand All @@ -45,7 +44,7 @@ func NewSASLAuthenticator(client sasl.Client) Authenticator {
return &saslAuthenticator{client: client}
}

func (a *saslAuthenticator) Authenticate(c *client.Client) error {
func (a *saslAuthenticator) Authenticate(c Authenticatable) error {
return c.Authenticate(a.client)
}

Expand All @@ -61,7 +60,7 @@ func NewOAuthBearerAuthenticator(username string, source oauth2.TokenSource) Aut
}
}

func (a *oauthBearerAuthenticator) Authenticate(c *client.Client) error {
func (a *oauthBearerAuthenticator) Authenticate(c Authenticatable) error {
tok, err := a.source.Token()
if err != nil {
return err
Expand Down
11 changes: 10 additions & 1 deletion imap/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"crypto/tls"
"time"

"github.com/emersion/go-sasl"

"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
)
Expand All @@ -48,10 +50,17 @@ type Client interface {
FlagQuit()
}

// Authenticatable is the minimal set of functions that are
// used by an Authenticator. This is mainly to assist with mocking.
type Authenticatable interface {
Login(username, password string) error
Authenticate(auth sasl.Client) error
}

// Authenticator provides a common interface to authenticate via
// username/password and SASL.
type Authenticator interface {
Authenticate(c *client.Client) error
Authenticate(a Authenticatable) error
}

// ConnectionConfig contains the base configuration required
Expand Down

0 comments on commit 816a920

Please sign in to comment.