-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnative.go
131 lines (112 loc) · 4.27 KB
/
native.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package nylas
import (
"context"
"encoding/json"
"net/http"
"strings"
)
// AuthorizeSettings provides settings for a native authentication connect
// request and should JSON marshal into the desired object.
// See: https://docs.nylas.com/reference#section-provider-specific-settings
type AuthorizeSettings interface {
// Provider returns the provider value to be used in a connect request.
Provider() string
}
// AuthorizeRequest used to start the process of connecting an account to Nylas.
// See: https://docs.nylas.com/reference#connectauthorize
type AuthorizeRequest struct {
clientID string
Name string
EmailAddress string
Settings AuthorizeSettings
Scopes []string
}
// MarshalJSON implements the json.Marshaler interface.
func (r AuthorizeRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"client_id": r.clientID,
"name": r.Name,
"email_address": r.EmailAddress,
"provider": r.Settings.Provider(),
"settings": r.Settings,
"scopes": strings.Join(r.Scopes, ","),
})
}
// ConnectAccount to Nylas with Native Authentication.
// See: https://docs.nylas.com/docs/native-authentication
func (c *Client) ConnectAccount(ctx context.Context, authReq AuthorizeRequest) (Account, error) {
code, err := c.connectAuthorize(ctx, authReq)
if err != nil {
return Account{}, err
}
return c.connectExchangeCode(ctx, code)
}
func (c *Client) connectAuthorize(ctx context.Context, authReq AuthorizeRequest) (string, error) {
authReq.clientID = c.clientID
req, err := c.newRequest(ctx, http.MethodPost, "/connect/authorize", &authReq)
if err != nil {
return "", err
}
connectResp := struct {
Code string `json:"code"`
}{}
return connectResp.Code, c.do(req, &connectResp)
}
func (c *Client) connectExchangeCode(ctx context.Context, authorizeCode string) (Account, error) {
req, err := c.newRequest(ctx, http.MethodPost, "/connect/token", &map[string]interface{}{
"client_id": c.clientID,
"client_secret": c.clientSecret,
"code": authorizeCode,
})
if err != nil {
return Account{}, err
}
var resp Account
return resp, c.do(req, &resp)
}
// GmailAuthorizeSettings implements AuthorizeSettings.
type GmailAuthorizeSettings struct {
GoogleClientID string `json:"google_client_id"`
GoogleClientSecret string `json:"google_client_secret"`
GoogleRefreshToken string `json:"google_refresh_token"`
}
// Provider returns the provider value to be used in a connect request.
func (GmailAuthorizeSettings) Provider() string { return "gmail" }
// IMAPAuthorizeSettings implements AuthorizeSettings.
type IMAPAuthorizeSettings struct {
IMAPHost string `json:"imap_host"`
IMAPPort int `json:"imap_port"`
IMAPUsername string `json:"imap_username"`
IMAPPassword string `json:"imap_password"`
SMTPHost string `json:"smtp_host"`
SMTPPort int `json:"smtp_port"`
SMTPUsername string `json:"smtp_username"`
SMTPPassword string `json:"smtp_password"`
SSLRequired bool `json:"ssl_required"`
}
// Provider returns the provider value to be used in a connect request.
func (IMAPAuthorizeSettings) Provider() string { return "imap" }
// ExchangeAuthorizeSettings implements AuthorizeSettings.
type ExchangeAuthorizeSettings struct {
Username string `json:"username"`
Password string `json:"password"`
EASServerHost string `json:"eas_server_host"`
}
// Provider returns the provider value to be used in a connect request.
func (ExchangeAuthorizeSettings) Provider() string { return "exchange" }
// Office365AuthorizeSettings implements AuthorizeSettings.
type Office365AuthorizeSettings struct {
MicrosoftClientSecret string `json:"microsoft_client_secret"`
MicrosoftRefreshToken string `json:"microsoft_refresh_token"`
RedirectURI string `json:"redirect_uri"`
}
// Provider returns the provider value to be used in a connect request.
func (Office365AuthorizeSettings) Provider() string { return "office365" }
// OutlookAuthorizeSettings implements AuthorizeSettings.
type OutlookAuthorizeSettings struct {
Username string `json:"username"`
Password string `json:"password"`
EASServerHost string `json:"eas_server_host"`
}
// Provider returns the provider value to be used in a connect request.
func (OutlookAuthorizeSettings) Provider() string { return "outlook" }