Debian Codegraph @

pkg/services/email/smtp/smtp_oauth2.go

raw
1 package smtp
2
3 import (
4 "net/smtp"
5 )
6
7 // oauth2Auth implements the SASL XOAUTH2 authentication mechanism for SMTP.
8 //
9 // Credentials are sent as a single initial response from Start. See [newOAuth2Auth].
10 type oauth2Auth struct {
11 // username is the SMTP account username.
12 username string
13 // accessToken is the OAuth2 Bearer token sent in Start.
14 accessToken string
15 // host is the expected SMTP server hostname checked in Start.
16 host string
17 }
18
19 // newOAuth2Auth returns an [smtp.Auth] that implements SASL XOAUTH2 authentication.
20 //
21 // The password is treated as a static OAuth2 access token. Token refresh is not
22 // supported. A 334 challenge is answered with an empty continuation so the
23 // server can send 535 on failure. See
24 // https://developers.google.com/gmail/imap/xoauth2-protocol.
25 //
26 // Credentials are sent only when the connection uses TLS or the server name is
27 // localhost. Otherwise authentication fails without transmitting the token,
28 // matching [smtp.PlainAuth] and [newLoginAuth].
29 //
30 // Parameters:
31 // - username: The SMTP account username.
32 // - accessToken: A valid OAuth2 access token used as the Bearer credential.
33 // - host: The expected SMTP server hostname, which must match [smtp.ServerInfo.Name] at [oauth2Auth.Start].
34 //
35 // Returns:
36 // - An [smtp.Auth] that performs XOAUTH2 authentication for the given credentials and host.
37 func newOAuth2Auth(username, accessToken, host string) smtp.Auth {
38 return &oauth2Auth{
39 username: username,
40 accessToken: accessToken,
41 host: host,
42 }
43 }
44
45 // Next continues the XOAUTH2 exchange.
46 //
47 // Credentials were sent in [oauth2Auth.Start]. When more is true the server
48 // issued a 334 (typically a JSON error status); an empty continuation is
49 // returned so net/smtp does not treat AUTH as successful and the server can
50 // follow with 535.
51 //
52 // Parameters:
53 // - fromServer: The decoded server challenge payload (unused).
54 // - more: Whether the server expects another client response (SMTP 334).
55 //
56 // Returns:
57 // - An empty slice when the server expects a continuation, or nil when AUTH is finished.
58 // - A nil error; failure is reported by the subsequent SMTP status line.
59 func (a *oauth2Auth) Next(_ []byte, more bool) ([]byte, error) {
60 if more {
61 return []byte{}, nil
62 }
63
64 return nil, nil
65 }
66
67 // Start begins XOAUTH2 authentication with an initial SASL payload.
68 //
69 // Parameters:
70 // - server: Connection metadata from the SMTP client, including Name and TLS ([smtp.ServerInfo]).
71 //
72 // Returns:
73 // - The authentication protocol name ("XOAUTH2").
74 // - The initial client response containing the user and Bearer token.
75 // - A non-nil error if the connection is unencrypted to a non-local host or the host name does not match.
76 func (a *oauth2Auth) Start(server *smtp.ServerInfo) (string, []byte, error) {
77 if !server.TLS && !isLocalhost(server.Name) {
78 return "", nil, errUnencryptedConnection
79 }
80
81 if server.Name != a.host {
82 return "", nil, errWrongHostName
83 }
84
85 resp := []byte("user=" + a.username + "\x01auth=Bearer " + a.accessToken + "\x01\x01")
86
87 return "XOAUTH2", resp, nil
88 }