Initial commit

This commit is contained in:
Neil Alexander
2021-07-07 18:15:07 +01:00
commit ceffe7612d
26 changed files with 2130 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
package imapserver
import (
"fmt"
"log"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/backend"
"github.com/neilalexander/yggmail/internal/config"
"github.com/neilalexander/yggmail/internal/storage"
)
type Backend struct {
Config *config.Config
Log *log.Logger
Storage storage.Storage
}
func (b *Backend) Login(_ *imap.ConnInfo, username, password string) (backend.User, error) {
if authed, err := b.Storage.TryAuthenticate(username, password); err != nil {
b.Log.Printf("Failed to authenticate IMAP user %q due to error: %s", username, err)
return nil, fmt.Errorf("failed to authenticate: %w", err)
} else if !authed {
b.Log.Printf("Failed to authenticate IMAP user %q\n", username)
return nil, backend.ErrInvalidCredentials
}
defer b.Log.Printf("Authenticated IMAP user %q\n", username)
user := &User{
backend: b,
username: username,
}
return user, nil
}