Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
173 lines
4.3 KiB
Go
173 lines
4.3 KiB
Go
package nylas
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"github.com/markbates/goth"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// These vars define the Authentication, Token, and API URLS for Nylas.
|
|
var (
|
|
AuthURL = "https://api.nylas.com/oauth/authorize"
|
|
TokenURL = "https://api.nylas.com/oauth/token"
|
|
ProfileURL = "https://api.Nylas.com/account"
|
|
)
|
|
|
|
// New creates a new Nylas provider, and sets up important connection details.
|
|
// You should always call `Nylas.New` to get a new Provider. Never try to create
|
|
// one manually.
|
|
func New(clientKey, secret, callbackURL string, scopes ...string) *Provider {
|
|
return NewCustomisedURL(clientKey, secret, callbackURL, AuthURL, TokenURL, ProfileURL, scopes...)
|
|
}
|
|
|
|
// NewCustomisedURL is similar to New(...) but can be used to set custom URLs to connect to
|
|
func NewCustomisedURL(clientKey, secret, callbackURL, authURL, tokenURL, profileURL string, scopes ...string) *Provider {
|
|
p := &Provider{
|
|
ClientKey: clientKey,
|
|
Secret: secret,
|
|
CallbackURL: callbackURL,
|
|
providerName: "nylas",
|
|
profileURL: profileURL,
|
|
}
|
|
p.config = newConfig(p, authURL, tokenURL, scopes)
|
|
return p
|
|
}
|
|
|
|
// Provider is the implementation of `goth.Provider` for accessing Nylas.
|
|
type Provider struct {
|
|
ClientKey string
|
|
Secret string
|
|
CallbackURL string
|
|
HTTPClient *http.Client
|
|
config *oauth2.Config
|
|
providerName string
|
|
profileURL string
|
|
}
|
|
|
|
// Name is the name used to retrieve this provider later.
|
|
func (p *Provider) Name() string {
|
|
return p.providerName
|
|
}
|
|
|
|
// SetName is to update the name of the provider (needed in case of multiple providers of 1 type)
|
|
func (p *Provider) SetName(name string) {
|
|
p.providerName = name
|
|
}
|
|
|
|
func (p *Provider) Client() *http.Client {
|
|
return goth.HTTPClientWithFallBack(p.HTTPClient)
|
|
}
|
|
|
|
// Debug is a no-op for the Nylas package.
|
|
func (p *Provider) Debug(debug bool) {}
|
|
|
|
// BeginAuth asks Nylas for an authentication end-point.
|
|
func (p *Provider) BeginAuth(state string) (goth.Session, error) {
|
|
url := p.config.AuthCodeURL(state)
|
|
session := &Session{
|
|
AuthURL: url,
|
|
}
|
|
return session, nil
|
|
}
|
|
|
|
// FetchUser will go to Nylas and access basic information about the user.
|
|
func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
|
|
sess := session.(*Session)
|
|
user := goth.User{
|
|
AccessToken: sess.AccessToken,
|
|
Provider: p.Name(),
|
|
}
|
|
|
|
if user.AccessToken == "" {
|
|
// data is not yet retrieved since accessToken is still empty
|
|
return user, fmt.Errorf("%s cannot get user information without accessToken", p.providerName)
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", p.profileURL, nil)
|
|
if err != nil {
|
|
return user, err
|
|
}
|
|
|
|
req.Header.Add("Authorization", "Bearer "+sess.AccessToken)
|
|
response, err := p.Client().Do(req)
|
|
if err != nil {
|
|
return user, err
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return user, fmt.Errorf("Nylas API responded with a %d trying to fetch user information", response.StatusCode)
|
|
}
|
|
|
|
bits, err := ioutil.ReadAll(response.Body)
|
|
if err != nil {
|
|
return user, err
|
|
}
|
|
|
|
err = json.NewDecoder(bytes.NewReader(bits)).Decode(&user.RawData)
|
|
if err != nil {
|
|
return user, err
|
|
}
|
|
|
|
err = userFromReader(bytes.NewReader(bits), &user)
|
|
if err != nil {
|
|
return user, err
|
|
}
|
|
|
|
return user, err
|
|
}
|
|
|
|
func userFromReader(reader io.Reader, user *goth.User) error {
|
|
u := struct {
|
|
ID string `json:"id"`
|
|
Email string `json:"email_address"`
|
|
Name string `json:"name"`
|
|
}{}
|
|
|
|
err := json.NewDecoder(reader).Decode(&u)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user.Name = u.Name
|
|
user.NickName = u.Name
|
|
user.Email = u.Email
|
|
user.UserID = u.ID
|
|
|
|
return err
|
|
}
|
|
|
|
func newConfig(provider *Provider, authURL, tokenURL string, scopes []string) *oauth2.Config {
|
|
c := &oauth2.Config{
|
|
ClientID: provider.ClientKey,
|
|
ClientSecret: provider.Secret,
|
|
RedirectURL: provider.CallbackURL,
|
|
Endpoint: oauth2.Endpoint{
|
|
AuthURL: authURL,
|
|
TokenURL: tokenURL,
|
|
},
|
|
Scopes: []string{},
|
|
}
|
|
|
|
c.Scopes = append(c.Scopes, scopes...)
|
|
|
|
return c
|
|
}
|
|
|
|
//RefreshToken refresh token is not provided by Nylas
|
|
func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) {
|
|
return nil, errors.New("Refresh token is not provided by Nylas")
|
|
}
|
|
|
|
//RefreshTokenAvailable refresh token is not provided by Nylas
|
|
func (p *Provider) RefreshTokenAvailable() bool {
|
|
return false
|
|
}
|