kittemail/backend.go

58 lines
996 B
Go
Raw Normal View History

2024-12-10 19:54:22 +00:00
package main
import (
"crypto/ed25519"
2025-01-17 18:13:01 +00:00
"github.com/emersion/go-imap/v2/imapserver"
2024-12-10 19:54:22 +00:00
)
2025-01-17 18:13:01 +00:00
// OAuthConfig is the configuration for OAuth.
2024-12-10 19:54:22 +00:00
type OAuthConfig struct {
HostName string
PublicKey ed25519.PublicKey
}
2025-01-17 18:13:01 +00:00
// Server is a server instance.
//
// A server contains a list of users.
type Server struct {
config OAuthConfig
2024-12-10 19:54:22 +00:00
}
2025-01-17 18:13:01 +00:00
// New creates a new server.
func New(config OAuthConfig) *Server {
return &Server{
config: config,
2024-12-10 19:54:22 +00:00
}
2025-01-17 18:13:01 +00:00
}
2024-12-10 19:54:22 +00:00
2025-01-17 18:13:01 +00:00
// NewSession creates a new IMAP session.
func (s *Server) NewSession() imapserver.Session {
return &serverSession{
server: s,
UserSession: &UserSession{
user: &User{
server: s,
},
mailbox: nil,
},
2024-12-10 19:54:22 +00:00
}
2025-01-17 18:13:01 +00:00
}
2024-12-10 19:54:22 +00:00
2025-01-17 18:13:01 +00:00
type serverSession struct {
*UserSession // may be nil
server *Server // immutable
}
var _ imapserver.Session = (*serverSession)(nil)
2024-12-10 19:54:22 +00:00
2025-01-17 18:13:01 +00:00
func (sess *serverSession) SLogin(username, token string) error {
sess.user = &User{server: sess.server}
err := sess.user.Login(username, token)
2024-12-10 19:54:22 +00:00
if err != nil {
2025-01-17 18:13:01 +00:00
return err
2024-12-10 19:54:22 +00:00
}
2025-01-17 18:13:01 +00:00
return nil
2024-12-10 19:54:22 +00:00
}