mattermost/mattermost-plugin-mscalendar

Handle case where a user's refresh token expires

Open

#198 opened on Nov 10, 2020

 (27 comments) (1 reaction) (1 assignee)Go (35 forks)auto 404
Difficulty/1:EasyHacktoberfestHelp WantedType/BugUp For Grabs

Repository metrics

Stars
 (22 stars)
PR merge metrics
 (PR metrics pending)

Description

The task here is to do the following on any request to the msgraph API:

  • If an API request fails, examine the error coming back from the msgraph API to check if the acting user's refresh token is expired. If it is expired, mark the user as inactive in the kv store. Then send a DM to the user to reconnect their account.
  • If the user is marked as inactive, abort any future API requests done for this user, and log via the WARN log level.

https://learn.microsoft.com/en-us/answers/questions/340205/for-how-long-i-can-keep-using-the-refresh-token


Per the discussion in https://github.com/mattermost/mattermost-plugin-mscalendar/issues/15#issuecomment-585033566, it was determined that we should allow the OAuth2 library to handle access tokens and refresh tokens with the library's default behavior.

The refresh token is used to retrieve an access token whenever we need to perform an operation in Microsoft's system. A customer's log reports an error that says The refresh token has expired due to inactivity. This error is occurring during the Renew Subscription job. The customer said this error is printed to the logs every night at midnight.

(log ERROR) Error renewing subscription. err=msgraph RenewSubscription: Patch https://graph.microsoft.com/v1.0/subscriptions/REDACTED: oauth2: cannot fetch token: 400 Bad Request Response: {"error":"invalid_grant","error_description":"AADSTS700082: The refresh token has expired due to inactivity. The token was issued on 2020-07-23T12:22:48.1096407Z and was inactive for 90.00:00:00.\r\nTrace ID: b40f4014-73bb-40f3-b90d-cd42d78c9600\r\nCorrelation ID: 1bb86522-a8e8-45a2-9966-2d11a892c925\r\nTimestamp: 2020-11-05 00:00:00Z","error_codes":[700082],"timestamp":"2020-11-05 00:00:00Z","trace_id":"b40f4014-73bb-40f3-b90d-cd42d78c9600","correlation_id":"1bb86522-a8e8-45a2-9966-2d11a892c925","error_uri":https://login.microsoftonline.com/error?code=700082}

https://learn.microsoft.com/en-us/answers/questions/340205/for-how-long-i-can-keep-using-the-refresh-token

More context here https://community-daily.mattermost.com/core/pl/u7jyh7dokpdtfcq49keu6peofe

Issue created from a Mattermost message by @mickmister.

Contributor guide