about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--commands/compose/send.go31
-rw-r--r--lib/oauthbearer.go4
2 files changed, 33 insertions, 2 deletions
diff --git a/commands/compose/send.go b/commands/compose/send.go
index 40ac4ca..dd7ece9 100644
--- a/commands/compose/send.go
+++ b/commands/compose/send.go
@@ -16,9 +16,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{}
@@ -97,6 +99,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 '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:
 		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
 		}
title='author James Booth <boothj5@gmail.com> 2015-03-23 23:38:06 +0000 committer James Booth <boothj5@gmail.com> 2015-03-23 23:38:06 +0000 Added pgpkeyid account setting, send signed presence' href='/danisanti/profani-tty/commit/src/config/account.h?id=475dfebd97b8adace3ee56683b968da752a3ae02'>475dfebd ^
904a5a81 ^
53fc89f7 ^
6640a089 ^
21ab1821 ^

19e71f05 ^



21ab1821 ^
19e71f05 ^


53fc89f7 ^
e043029a ^
bc9e6b79 ^
21ab1821 ^
3fd6f70b ^


21ab1821 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84