Skip to content

Commit

Permalink
[mattermostGH-235] Add Oauth User Level Refresh Token (mattermost#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
jupriano committed Dec 14, 2021
1 parent 502b78b commit 34a68c0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 50 deletions.
6 changes: 0 additions & 6 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ func (p *Plugin) completeUserOAuthToZoom(w http.ResponseWriter, r *http.Request)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
err = p.setUserToken(userID, token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

client := zoom.NewOAuthClient(token, conf, p.siteURL, p.getZoomAPIURL(), p.configuration.AccountLevelApp, p)
Expand Down
25 changes: 15 additions & 10 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,7 @@ func (p *Plugin) getActiveClient(user *model.User) (Client, string, error) {
if err != nil {
return nil, message, errors.New("could not decrypt OAuth access token")
}
oldToken, err := p.getUserToken(user.Id)
if err == nil {
p.API.LogInfo(fmt.Sprintf("oldTokenIs===========================%s", *oldToken))
}

info.OAuthToken.AccessToken = plainToken
conf := p.getOAuthConfig()
return zoom.NewOAuthClient(info.OAuthToken, conf, p.siteURL, p.getZoomAPIURL(), false, p), "", nil
Expand Down Expand Up @@ -251,21 +248,29 @@ func (p *Plugin) SetZoomSuperUserToken(token *oauth2.Token) error {
}

func (p *Plugin) GetZoomUserToken(userID string) (*oauth2.Token, error) {
token, err := p.getUserToken(userID)
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, nil
return token.OAuthToken, nil
}

func (p *Plugin) SetZoomUserToken(userID string, token *oauth2.Token) error {
err := p.setUserToken(userID, token)
func (p *Plugin) UpdateZoomUserToken(userID string, token *oauth2.Token) error {
zoomUser, err := p.fetchOAuthUserInfo(zoomUserByZoomID, userID)
if err != nil {
p.API.LogError("could not set token", err)
return errors.Wrap(err, "could not set token")
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
}
32 changes: 0 additions & 32 deletions server/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,35 +194,3 @@ func (p *Plugin) removeSuperUserToken() error {

return nil
}

func (p *Plugin) setUserToken(userID string, token *oauth2.Token) error {
rawToken, err := json.Marshal(token)
if err != nil {
return err
}

appErr := p.API.KVSet(fmt.Sprintf("%s%s_", zoomSuperUserTokenKey, userID), rawToken)
if appErr != nil {
return appErr
}

return nil
}

func (p *Plugin) getUserToken(userID string) (*oauth2.Token, error) {
var token oauth2.Token
rawToken, appErr := p.API.KVGet(fmt.Sprintf("%s%s_", zoomSuperUserTokenKey, userID))
if appErr != nil {
return nil, appErr
}
if len(rawToken) == 0 {
return nil, nil
}

err := json.Unmarshal(rawToken, &token)
if err != nil {
return nil, err
}

return &token, nil
}
2 changes: 1 addition & 1 deletion server/zoom/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ type PluginAPI interface {
GetZoomSuperUserToken() (*oauth2.Token, error)
SetZoomSuperUserToken(*oauth2.Token) error
GetZoomUserToken(userID string) (*oauth2.Token, error)
SetZoomUserToken(userID string, token *oauth2.Token) error
UpdateZoomUserToken(userID string, token *oauth2.Token) error
}
2 changes: 1 addition & 1 deletion server/zoom/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *OAuthClient) getUserViaOAuth(user *model.User) (*User, error) {
}

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

0 comments on commit 34a68c0

Please sign in to comment.