Skip to content

Commit

Permalink
[GH-235] Refresh token on user level oauth (#235) (#242)
Browse files Browse the repository at this point in the history
* [GH-235] Refresh token on user level oauth (#235)

* [GH-235] Add Oauth User Level Refresh Token (#235)

* [GH-235] Remove Unwanted Change

* [GH-235] Add Error Log

* [GH-235] Add Oauth User Level Refresh Token (#235)

Co-authored-by: root <[email protected]>
Co-authored-by: jupriano <[email protected]>
Co-authored-by: Ben Schumacher <[email protected]>
  • Loading branch information
4 people authored and mickmister committed Feb 24, 2022
1 parent 2acac55 commit bb48fe2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
28 changes: 28 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,31 @@ func (p *Plugin) SetZoomSuperUserToken(token *oauth2.Token) error {
}
return nil
}

func (p *Plugin) GetZoomUserToken(userID string) (*oauth2.Token, error) {
token, err := p.fetchOAuthUserInfo(zoomUserByZoomID, userID)
if err != nil {
return nil, errors.Wrap(err, "could not get token")
}
if token == nil {
return nil, errors.New("zoom app not connected")
}
return token.OAuthToken, nil
}

func (p *Plugin) UpdateZoomUserToken(userID string, token *oauth2.Token) error {
zoomUser, err := p.fetchOAuthUserInfo(zoomUserByZoomID, userID)
if err != nil {
p.API.LogError("could not update zoom user token", err)
return errors.Wrap(err, "could not update zoom user token")
}

zoomUser.OAuthToken = token
if err = p.storeOAuthUserInfo(zoomUser); err != nil {
msg := "unable to update user token"
p.API.LogWarn(msg, "error", err.Error())
return errors.Wrap(err, msg)
}

return nil
}
2 changes: 2 additions & 0 deletions server/zoom/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ type Client interface {
type PluginAPI interface {
GetZoomSuperUserToken() (*oauth2.Token, error)
SetZoomSuperUserToken(*oauth2.Token) error
GetZoomUserToken(userID string) (*oauth2.Token, error)
UpdateZoomUserToken(userID string, token *oauth2.Token) error
}
20 changes: 20 additions & 0 deletions server/zoom/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ func (c *OAuthClient) getUserViaOAuth(user *model.User) (*User, error) {
}
}

c.token = updatedToken
} else {
currentToken, err := c.api.GetZoomUserToken(user.Id)
if err != nil {
return nil, errors.Wrap(err, "error getting zoom user token")
}

tokenSource := c.config.TokenSource(context.Background(), currentToken)
updatedToken, err := tokenSource.Token()
if err != nil {
return nil, errors.Wrap(err, "error getting token from token source")
}

if updatedToken.AccessToken != currentToken.AccessToken {
kvErr := c.api.UpdateZoomUserToken(user.Id, updatedToken)
if kvErr != nil {
return nil, errors.Wrap(kvErr, "error setting new token")
}
}

c.token = updatedToken
}

Expand Down

0 comments on commit bb48fe2

Please sign in to comment.