-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from mxaly/zoom-provider
Add Zoom provider
- Loading branch information
Showing
6 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ $ go get github.com/markbates/goth | |
* Yahoo | ||
* Yammer | ||
* Yandex | ||
* Zoom | ||
|
||
## Examples | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package zoom | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"strings" | ||
"time" | ||
|
||
"github.com/markbates/goth" | ||
) | ||
|
||
// Session stores data during the auth process with Zoom. | ||
type Session struct { | ||
AuthURL string | ||
AccessToken string | ||
RefreshToken string | ||
ExpiresAt time.Time | ||
} | ||
|
||
var _ goth.Session = &Session{} | ||
|
||
// GetAuthURL will return the URL set by calling the `BeginAuth` function on the Zoom provider. | ||
func (s Session) GetAuthURL() (string, error) { | ||
if s.AuthURL == "" { | ||
return "", errors.New(goth.NoAuthUrlErrorMessage) | ||
} | ||
return s.AuthURL, nil | ||
} | ||
|
||
// Authorize the session with Zoom and return the access token to be stored for future use. | ||
func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { | ||
p := provider.(*Provider) | ||
token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if !token.Valid() { | ||
return "", errors.New("Invalid token received from provider") | ||
} | ||
|
||
s.AccessToken = token.AccessToken | ||
s.RefreshToken = token.RefreshToken | ||
return token.AccessToken, err | ||
} | ||
|
||
// Marshal the session into a string | ||
func (s Session) Marshal() string { | ||
b, _ := json.Marshal(s) | ||
return string(b) | ||
} | ||
|
||
func (s Session) String() string { | ||
return s.Marshal() | ||
} | ||
|
||
// UnmarshalSession wil unmarshal a JSON string into a session. | ||
func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { | ||
s := &Session{} | ||
err := json.NewDecoder(strings.NewReader(data)).Decode(s) | ||
return s, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package zoom_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/markbates/goth" | ||
"github.com/markbates/goth/providers/zoom" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Implements_Session(t *testing.T) { | ||
t.Parallel() | ||
a := assert.New(t) | ||
s := &zoom.Session{} | ||
|
||
a.Implements((*goth.Session)(nil), s) | ||
} | ||
|
||
func Test_GetAuthURL(t *testing.T) { | ||
t.Parallel() | ||
a := assert.New(t) | ||
s := &zoom.Session{} | ||
|
||
_, err := s.GetAuthURL() | ||
a.Error(err) | ||
|
||
s.AuthURL = "/foo" | ||
|
||
url, _ := s.GetAuthURL() | ||
a.Equal(url, "/foo") | ||
} | ||
|
||
func Test_ToJSON(t *testing.T) { | ||
t.Parallel() | ||
a := assert.New(t) | ||
s := &zoom.Session{} | ||
|
||
data := s.Marshal() | ||
a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) | ||
} | ||
|
||
func Test_String(t *testing.T) { | ||
t.Parallel() | ||
a := assert.New(t) | ||
s := &zoom.Session{} | ||
|
||
a.Equal(s.String(), s.Marshal()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Package zoom implements the OAuth2 protocol for authenticating users through zoo. | ||
// This package can be used as a reference implementation of an OAuth2 provider for Goth. | ||
package zoom | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"golang.org/x/oauth2" | ||
|
||
"github.com/markbates/goth" | ||
) | ||
|
||
var ( | ||
authorizeURL string = "https://zoom.us/oauth/authorize" | ||
tokenURL string = "https://zoom.us/oauth/token" | ||
profileURL string = "https://zoom.us/v2/users/me" | ||
) | ||
|
||
// Provider is the implementation of `goth.Provider` for accessing Zoom. | ||
type Provider struct { | ||
ClientKey string | ||
Secret string | ||
CallbackURL string | ||
HTTPClient *http.Client | ||
config *oauth2.Config | ||
providerName string | ||
} | ||
|
||
type profileResp struct { | ||
FirstName string `json:"first_name"` | ||
LastName string `json:"last_name"` | ||
Email string `json:"email"` | ||
AvatarURL string `json:"pic_url"` | ||
ID string `json:"id"` | ||
} | ||
|
||
// New creates a new Zoom provider and sets up connection details. | ||
func New(clientKey, secret, callbackURL string, scopes ...string) *Provider { | ||
p := &Provider{ | ||
ClientKey: clientKey, | ||
Secret: secret, | ||
CallbackURL: callbackURL, | ||
providerName: "zoom", | ||
} | ||
p.config = newConfig(p, scopes) | ||
return p | ||
} | ||
|
||
// Name is the name used to retrieve the provider. | ||
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 zoom package. | ||
func (p *Provider) Debug(debug bool) {} | ||
|
||
// BeginAuth returns zoom authentication endpoint. | ||
func (p *Provider) BeginAuth(state string) (goth.Session, error) { | ||
return &Session{ | ||
AuthURL: p.config.AuthCodeURL(state), | ||
}, nil | ||
} | ||
|
||
// FetchUser makes a request to profileURL and returns zoom user data. | ||
func (p *Provider) FetchUser(session goth.Session) (goth.User, error) { | ||
s := session.(*Session) | ||
user := goth.User{ | ||
AccessToken: s.AccessToken, | ||
Provider: p.Name(), | ||
RefreshToken: s.RefreshToken, | ||
} | ||
|
||
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", profileURL, nil) | ||
if err != nil { | ||
return user, err | ||
} | ||
|
||
req.Header.Set("Authorization", "Bearer "+s.AccessToken) | ||
resp, err := p.Client().Do(req) | ||
if err != nil { | ||
return user, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return user, fmt.Errorf("%s responded with a %d trying to fetch user information", p.providerName, resp.StatusCode) | ||
} | ||
|
||
err = userFromReader(resp.Body, &user) | ||
return user, err | ||
} | ||
|
||
//RefreshTokenAvailable refresh token is provided by auth provider or not | ||
func (p *Provider) RefreshTokenAvailable() bool { | ||
return true | ||
} | ||
|
||
//RefreshToken get new access token based on the refresh token | ||
func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) { | ||
token := &oauth2.Token{RefreshToken: refreshToken} | ||
ts := p.config.TokenSource(goth.ContextForClient(p.Client()), token) | ||
newToken, err := ts.Token() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return newToken, err | ||
} | ||
|
||
func newConfig(provider *Provider, scopes []string) *oauth2.Config { | ||
c := &oauth2.Config{ | ||
ClientID: provider.ClientKey, | ||
ClientSecret: provider.Secret, | ||
RedirectURL: provider.CallbackURL, | ||
Endpoint: oauth2.Endpoint{ | ||
AuthURL: authorizeURL, | ||
TokenURL: tokenURL, | ||
}, | ||
Scopes: []string{}, | ||
} | ||
|
||
if len(scopes) > 0 { | ||
for _, scope := range scopes { | ||
c.Scopes = append(c.Scopes, scope) | ||
} | ||
} | ||
|
||
return c | ||
} | ||
|
||
func userFromReader(r io.Reader, user *goth.User) error { | ||
var rawData map[string]interface{} | ||
|
||
buf := new(bytes.Buffer) | ||
_, err := buf.ReadFrom(r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(buf.Bytes(), &rawData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
u := &profileResp{} | ||
err = json.Unmarshal(buf.Bytes(), &u) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
user.Email = u.Email | ||
user.FirstName = u.FirstName | ||
user.LastName = u.LastName | ||
user.Name = fmt.Sprintf("%s %s", u.FirstName, u.LastName) | ||
user.UserID = u.ID | ||
user.AvatarURL = u.AvatarURL | ||
user.RawData = rawData | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package zoom_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/markbates/goth" | ||
"github.com/markbates/goth/providers/zoom" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func zoomProvider() *zoom.Provider { | ||
return zoom.New(os.Getenv("ZOOM_KEY"), os.Getenv("ZOOM_SECRET"), "/foo", "basic") | ||
} | ||
|
||
func Test_New(t *testing.T) { | ||
t.Parallel() | ||
a := assert.New(t) | ||
|
||
provider := zoomProvider() | ||
a.Equal(provider.ClientKey, os.Getenv("ZOOM_KEY")) | ||
a.Equal(provider.Secret, os.Getenv("ZOOM_SECRET")) | ||
a.Equal(provider.CallbackURL, "/foo") | ||
} | ||
|
||
func Test_Implements_Provider(t *testing.T) { | ||
t.Parallel() | ||
a := assert.New(t) | ||
a.Implements((*goth.Provider)(nil), zoomProvider()) | ||
} | ||
func Test_BeginAuth(t *testing.T) { | ||
t.Parallel() | ||
a := assert.New(t) | ||
provider := zoomProvider() | ||
session, err := provider.BeginAuth("test_state") | ||
s := session.(*zoom.Session) | ||
a.NoError(err) | ||
a.Contains(s.AuthURL, "https://zoom.us/oauth/authorize") | ||
a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("ZOOM_KEY"))) | ||
a.Contains(s.AuthURL, "state=test_state") | ||
} | ||
|
||
func Test_SessionFromJSON(t *testing.T) { | ||
t.Parallel() | ||
a := assert.New(t) | ||
|
||
provider := zoomProvider() | ||
|
||
s, err := provider.UnmarshalSession(`{"AuthURL":"https://app.zoom.io/oauth","AccessToken":"1234567890"}`) | ||
a.NoError(err) | ||
session := s.(*zoom.Session) | ||
a.Equal(session.AuthURL, "https://app.zoom.io/oauth") | ||
a.Equal(session.AccessToken, "1234567890") | ||
} |