Add `oauthbearer` support for SMTP
This piggybacks on the existing IMAP support, and uses the same configuration format (my local testing example has the IMAP and SMTP lines almost copy-pasted from one another). It's a little clumsy in that a new token is negotiated for every `Send()` command, but it's a start...
This commit is contained in:
parent
f1a0fd20d6
commit
f4dc7e1f74
|
@ -16,9 +16,11 @@ import (
|
||||||
"github.com/miolini/datacounter"
|
"github.com/miolini/datacounter"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"git.sr.ht/~sircmpwn/aerc/lib"
|
||||||
"git.sr.ht/~sircmpwn/aerc/models"
|
"git.sr.ht/~sircmpwn/aerc/models"
|
||||||
"git.sr.ht/~sircmpwn/aerc/widgets"
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
||||||
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
||||||
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Send struct{}
|
type Send struct{}
|
||||||
|
@ -97,6 +99,35 @@ func (Send) Execute(aerc *widgets.Aerc, args []string) error {
|
||||||
case "plain":
|
case "plain":
|
||||||
password, _ := uri.User.Password()
|
password, _ := uri.User.Password()
|
||||||
saslClient = sasl.NewPlainClient("", uri.User.Username(), password)
|
saslClient = sasl.NewPlainClient("", uri.User.Username(), password)
|
||||||
|
case "oauthbearer":
|
||||||
|
q := uri.Query()
|
||||||
|
|
||||||
|
oauth2 := &oauth2.Config{}
|
||||||
|
if q.Get("token_endpoint") != "" {
|
||||||
|
oauth2.ClientID = q.Get("client_id")
|
||||||
|
oauth2.ClientSecret = q.Get("client_secret")
|
||||||
|
oauth2.Scopes = []string{q.Get("scope")}
|
||||||
|
oauth2.Endpoint.TokenURL = q.Get("token_endpoint")
|
||||||
|
}
|
||||||
|
|
||||||
|
password, _ := uri.User.Password()
|
||||||
|
bearer := lib.OAuthBearer{
|
||||||
|
OAuth2: oauth2,
|
||||||
|
Enabled: true,
|
||||||
|
}
|
||||||
|
if bearer.OAuth2.Endpoint.TokenURL == "" {
|
||||||
|
return fmt.Errorf("No 'TokenURL' configured for this account")
|
||||||
|
}
|
||||||
|
token, err := bearer.ExchangeRefreshToken(password)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
password = token.AccessToken
|
||||||
|
|
||||||
|
saslClient = sasl.NewOAuthBearerClient(&sasl.OAuthBearerOptions{
|
||||||
|
Username: uri.User.Username(),
|
||||||
|
Token: password,
|
||||||
|
})
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Unsupported auth mechanism %s", auth)
|
return fmt.Errorf("Unsupported auth mechanism %s", auth)
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ type OAuthBearer struct {
|
||||||
Enabled bool
|
Enabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *OAuthBearer) exchangeRefreshToken(refreshToken string) (*oauth2.Token, error) {
|
func (c *OAuthBearer) ExchangeRefreshToken(refreshToken string) (*oauth2.Token, error) {
|
||||||
token := new(oauth2.Token)
|
token := new(oauth2.Token)
|
||||||
token.RefreshToken = refreshToken
|
token.RefreshToken = refreshToken
|
||||||
token.TokenType = "Bearer"
|
token.TokenType = "Bearer"
|
||||||
|
@ -26,7 +26,7 @@ func (c *OAuthBearer) Authenticate(username string, password string, client *cli
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.OAuth2.Endpoint.TokenURL != "" {
|
if c.OAuth2.Endpoint.TokenURL != "" {
|
||||||
token, err := c.exchangeRefreshToken(password)
|
token, err := c.ExchangeRefreshToken(password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue