~sircmpwn/aerc

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2 2

[PATCH] Add `oauthbearer` support for SMTP

Details
Message ID
<20200525181059.52373-1-necaris@gmail.com>
DKIM signature
pass
Download raw message
Patch: +33 -2
From: R Chowdhury <necaris@gmail.com>

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...
---
 commands/compose/send.go | 31 +++++++++++++++++++++++++++++++
 lib/oauthbearer.go       |  4 ++--
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/commands/compose/send.go b/commands/compose/send.go
index 36b9fd8..8c3f1fe 100644
--- a/commands/compose/send.go
+++ b/commands/compose/send.go
@@ -17,9 +17,11 @@ import (
	"github.com/miolini/datacounter"
	"github.com/pkg/errors"

	"git.sr.ht/~sircmpwn/aerc/lib"
	"git.sr.ht/~sircmpwn/aerc/models"
	"git.sr.ht/~sircmpwn/aerc/widgets"
	"git.sr.ht/~sircmpwn/aerc/worker/types"
	"golang.org/x/oauth2"
)

type Send struct{}
@@ -98,6 +100,35 @@ func (Send) Execute(aerc *widgets.Aerc, args []string) error {
	case "plain":
		password, _ := uri.User.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 token URL for OAuth Bearer - no worky")
		}
		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:
		return fmt.Errorf("Unsupported auth mechanism %s", auth)
	}
diff --git a/lib/oauthbearer.go b/lib/oauthbearer.go
index 5bcba60..1030696 100644
--- a/lib/oauthbearer.go
+++ b/lib/oauthbearer.go
@@ -13,7 +13,7 @@ type OAuthBearer struct {
	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.RefreshToken = refreshToken
	token.TokenType = "Bearer"
@@ -26,7 +26,7 @@ func (c *OAuthBearer) Authenticate(username string, password string, client *cli
	}

	if c.OAuth2.Endpoint.TokenURL != "" {
		token, err := c.exchangeRefreshToken(password)
		token, err := c.ExchangeRefreshToken(password)
		if err != nil {
			return err
		}
-- 
2.26.2
Details
Message ID
<20200526054433.sijuno6bgp4y4g2s@feather.localdomain>
In-Reply-To
<20200525181059.52373-1-necaris@gmail.com> (view parent)
DKIM signature
pass
Download raw message
Hi,

On Mon, May 25, 2020 at 02:11:00PM -0400, Rami Chowdhury wrote:
> From: R Chowdhury <necaris@gmail.com>
> +		if bearer.OAuth2.Endpoint.TokenURL == "" {
> +			return fmt.Errorf("No token URL for OAuth Bearer - no worky")
> +		}

Get rid of those kinds of childish remarks please.
Treat the user like an adult and keep it somewhat professional.

Cheers,
Reto
Details
Message ID
<UWRXAQ.G0I1PDLZ98K23@gmail.com>
In-Reply-To
<20200526054433.sijuno6bgp4y4g2s@feather.localdomain> (view parent)
DKIM signature
pass
Download raw message

On Tue, May 26, 2020 at 07:44, Reto <reto@labrat.space> wrote:
> Hi,
> 
> On Mon, May 25, 2020 at 02:11:00PM -0400, Rami Chowdhury wrote:
>>  From: R Chowdhury <necaris@gmail.com>
>>  +		if bearer.OAuth2.Endpoint.TokenURL == "" {
>>  +			return fmt.Errorf("No token URL for OAuth Bearer - no worky")
>>  +		}
> 
> Get rid of those kinds of childish remarks please.
> Treat the user like an adult and keep it somewhat professional.

Forgot that was still in there -- thank you for the catch! Updated 
patch will be there in a moment.
Review patch Export thread (mbox)