| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/smtp" |
| 5 |
) |
| 6 |
|
| 7 |
type oauth2Auth struct { |
| 8 |
username, accessToken string |
| 9 |
} |
| 10 |
|
| 11 |
// OAuth2Auth returns an Auth that implements the SASL XOAUTH2 authentication |
| 12 |
// as per https://developers.google.com/gmail/imap/xoauth2-protocol. |
| 13 |
// It assumes the provided password is a valid OAuth2 access token. |
| 14 |
// Token refresh or complex challenge-response flows are not supported. |
| 15 |
func OAuth2Auth(username, accessToken string) smtp.Auth { |
| 16 |
return &oauth2Auth{username, accessToken} |
| 17 |
} |
| 18 |
|
| 19 |
func (a *oauth2Auth) Next(_ []byte, _ bool) ([]byte, error) { |
| 20 |
return nil, nil |
| 21 |
} |
| 22 |
|
| 23 |
func (a *oauth2Auth) Start(_ *smtp.ServerInfo) (string, []byte, error) { |
| 24 |
resp := []byte("user=" + a.username + "\x01auth=Bearer " + a.accessToken + "\x01\x01") |
| 25 |
|
| 26 |
return "XOAUTH2", resp, nil |
| 27 |
} |