mirror of
https://github.com/warmbly/warmbly.git
synced 2026-11-28 08:05:45 +00:00
161 lines
5.5 KiB
Go
161 lines
5.5 KiB
Go
package email
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/warmbly/warmbly/internal/errx"
|
|
"github.com/warmbly/warmbly/internal/infrastructure/pubsub"
|
|
"github.com/warmbly/warmbly/internal/models"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// Brokered OAuth: consent on this deployment's OAuth app for a linked instance; the grant stays here.
|
|
|
|
func (s *emailService) OAuthAuthorizeURL(provider models.InboxProvider, state, verifier string) (string, *errx.Error) {
|
|
cfg, xerr := s.oauthConfigFor(provider)
|
|
if xerr != nil {
|
|
return "", xerr
|
|
}
|
|
if verifier == "" {
|
|
return "", errx.InternalError()
|
|
}
|
|
opts := append(authCodeOptions(provider, ""), oauth2.S256ChallengeOption(verifier))
|
|
return cfg.AuthCodeURL(state, opts...), nil
|
|
}
|
|
|
|
// OAuthCallbackOrigin is the scheme and host the provider redirects back to.
|
|
func (s *emailService) OAuthCallbackOrigin(provider models.InboxProvider) (string, *errx.Error) {
|
|
cfg, xerr := s.oauthConfigFor(provider)
|
|
if xerr != nil {
|
|
return "", xerr
|
|
}
|
|
u, err := url.Parse(cfg.RedirectURL)
|
|
if err != nil || u.Scheme == "" || u.Host == "" {
|
|
return "", errx.InternalError()
|
|
}
|
|
return u.Scheme + "://" + u.Host, nil
|
|
}
|
|
|
|
func (s *emailService) OAuthConnectWithCode(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier string) (*models.Email, *errx.Error) {
|
|
return s.OAuthConnectWithCodeForAccount(ctx, userID, orgID, provider, code, verifier, uuid.Nil)
|
|
}
|
|
|
|
func (s *emailService) OAuthConnectWithCodeForAccount(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier string, accountID uuid.UUID) (*models.Email, *errx.Error) {
|
|
ctx, cancel := detach(ctx, connectBudget)
|
|
defer cancel()
|
|
if code = strings.TrimSpace(code); code == "" {
|
|
return nil, errx.ErrEmailOnboardCode
|
|
}
|
|
if verifier == "" {
|
|
return nil, errx.ErrEmailOnboardState
|
|
}
|
|
allowance, xerr := s.guardInboxLimit(ctx, orgID)
|
|
if xerr != nil {
|
|
return nil, xerr
|
|
}
|
|
cfg, xerr := s.oauthConfigFor(provider)
|
|
if xerr != nil {
|
|
return nil, xerr
|
|
}
|
|
tok, err := cfg.Exchange(ctx, code, oauth2.VerifierOption(verifier))
|
|
if err != nil {
|
|
return nil, errx.ErrEmailOnboardExchange
|
|
}
|
|
|
|
// The same two guards the first-party connect applies. Without them a
|
|
// brokered mailbox could be stored after a consent the person half granted,
|
|
// or with no refresh token at all, and it would read as connected until its
|
|
// first send failed days later.
|
|
if xerr := checkGrantedScopes(ctx, provider, cfg.Scopes, tok); xerr != nil {
|
|
return nil, xerr
|
|
}
|
|
if strings.TrimSpace(tok.RefreshToken) == "" {
|
|
return nil, errx.New(errx.BadRequest,
|
|
"The provider did not return a long-lived token for this mailbox, so it would stop working within the hour. "+
|
|
"Remove Warmbly's access in your account settings and connect it again.")
|
|
}
|
|
|
|
owner, xerr := fetchInboxOwner(ctx, provider, tok.AccessToken)
|
|
if xerr != nil {
|
|
return nil, xerr
|
|
}
|
|
if existing, xerr := s.findExisting(ctx, userID, orgID, owner.Email); xerr != nil {
|
|
return nil, xerr
|
|
} else if existing != nil {
|
|
return nil, errx.ErrEmailOnboardAlreadyExists
|
|
}
|
|
if provider == models.InboxProviderGoogle && personalGmail(owner.Email) {
|
|
return nil, errx.ErrEmailOnboardGooglePersonal
|
|
}
|
|
name := strings.TrimSpace(owner.Name)
|
|
if name == "" {
|
|
name = deriveNameFromEmail(owner.Email)
|
|
}
|
|
acc, xerr := s.emailRepository.NewOauthAccount(ctx, userID, models.NewOauthAccount{
|
|
ID: accountID,
|
|
OrganizationID: orgID,
|
|
Allowance: allowance,
|
|
Provider: provider,
|
|
Name: name,
|
|
Email: owner.Email,
|
|
MailHost: oauthMailHost(provider, owner.Email),
|
|
AccessToken: tok.AccessToken,
|
|
RefreshToken: tok.RefreshToken,
|
|
ExpiresAt: tok.Expiry,
|
|
GrantedScopes: recordedScopes(tok),
|
|
})
|
|
if xerr != nil {
|
|
return nil, xerr
|
|
}
|
|
s.captureSendIdentity(ctx, acc, tok)
|
|
if accountID != uuid.Nil {
|
|
return acc, nil
|
|
}
|
|
s.syncWarmupPoolMembership(ctx, acc)
|
|
s.publishAccountEvent(ctx, pubsub.EventAccountConnected, acc)
|
|
s.dispatchAccountConnected(ctx, orgID, acc)
|
|
s.loadAccountBestEffort(ctx, acc.ID)
|
|
return acc, nil
|
|
}
|
|
|
|
// OAuthAccessToken refreshes and re-seals the grant when within two minutes of expiry.
|
|
func (s *emailService) OAuthAccessToken(ctx context.Context, accountID uuid.UUID) (*oauth2.Token, *errx.Error) {
|
|
acc, xerr := s.emailRepository.GetByID(ctx, accountID)
|
|
if xerr != nil {
|
|
return nil, xerr
|
|
}
|
|
if acc == nil || acc.AuthMethod == models.MailAuthDelegated {
|
|
return nil, errx.ErrEmailCredentials
|
|
}
|
|
creds, xerr := s.emailRepository.GetOAuthCredentials(ctx, accountID)
|
|
if xerr != nil {
|
|
return nil, xerr
|
|
}
|
|
current := &oauth2.Token{AccessToken: creds.AccessToken, RefreshToken: creds.RefreshToken, Expiry: creds.ExpiresAt}
|
|
if current.AccessToken != "" && time.Until(current.Expiry) > 2*time.Minute {
|
|
return current, nil
|
|
}
|
|
// Only the client that issued the grant can refresh it.
|
|
cfg, xerr := s.grantConfig(ctx, creds.OAuthAppID, models.InboxProvider(acc.Provider))
|
|
if xerr != nil {
|
|
return nil, xerr
|
|
}
|
|
fresh, err := cfg.TokenSource(ctx, current).Token()
|
|
if err != nil {
|
|
log.Warn().Err(err).Str("account_id", accountID.String()).Msg("brokered token refresh failed")
|
|
return nil, errx.ErrEmailCredentials
|
|
}
|
|
if fresh.RefreshToken == "" {
|
|
fresh.RefreshToken = current.RefreshToken
|
|
}
|
|
if err := s.emailRepository.RefreshBoxTokenForApp(ctx, accountID, fresh.AccessToken, fresh.RefreshToken, fresh.Expiry, creds.OAuthAppID); err != nil {
|
|
log.Warn().Err(err).Str("account_id", accountID.String()).Msg("brokered token persist failed")
|
|
}
|
|
return fresh, nil
|
|
}
|