Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin should avoid using revoked tokens #261

Closed
mickmister opened this issue Sep 27, 2021 · 8 comments · Fixed by #308
Closed

Plugin should avoid using revoked tokens #261

mickmister opened this issue Sep 27, 2021 · 8 comments · Fixed by #308
Assignees
Labels
Difficulty/1:Easy Easy ticket Hacktoberfest Help Wanted Community help wanted Tech/Go Type/Enhancement New feature or improvement of existing feature
Milestone

Comments

@mickmister
Copy link
Member

If we perform an API request using a user's token, and we receive a response noting that the token is revoked, we should:

  • delete the token from the KV store, declaring the user as disconnected
  • and notify the user via DM that their token has been revoked, and that they will need to reconnect their account in order to use the plugin

Issue created from a Mattermost message by @thiefmaster.

@mickmister mickmister added Difficulty/1:Easy Easy ticket Help Wanted Community help wanted Tech/Go Type/Enhancement New feature or improvement of existing feature Up For Grabs Ready for help from the community. Removed when someone volunteers labels Sep 27, 2021
@mickmister mickmister added this to the Future Consideration milestone Jan 12, 2022
@MatthewDorner
Copy link
Contributor

I would like to work on this.

@mickmister
Copy link
Member Author

Great! Thank you @MatthewDorner!

@mickmister mickmister removed the Up For Grabs Ready for help from the community. Removed when someone volunteers label Jun 6, 2022
@MatthewDorner
Copy link
Contributor

MatthewDorner commented Jun 21, 2022

I'm adding checks for the "401 {error: invalid_token}" error in the places where code from main calls into the gitlab client package. I believe I need to handle it in main in order to Disconnect user and send the bot message.

But I'm afraid it is complicating the error handling code in main a bit and may be prone to bugs when further changes are made in main. It's unfortunate since all calls into gitlab would need the exact same check but I can't find a way to avoid duplicating it. I'd need to add like 10 instances of checking such as:

hooks, err := p.GitlabClient.GetGroupHooks(ctx, user, namespace)
	if err != nil {
		if strings.Contains(err.Error(), "401 {error: invalid_token}") {
			p.handleRevokedToken(user)
		}
		return false, errors.New("unable to connect to GitLab")
	}

where the second if statement is what's added to the existing check at: https://github.com/mattermost/mattermost-plugin-gitlab/blob/master/server/plugin.go#L570

@MatthewDorner
Copy link
Contributor

Just waiting to see the resolution of #298, but may have another issue.

In the case where the token is revoked AND expired, the attempt to renew the token will fail and there, I would like to Disconnect the account and DM the user. However, the call to p.disconnectGitlabAccount will call into p.getGitlabUserInfoByMattermostID again which will then call (in the PR above, at least) p.checkAndRefreshToken and attempt to refresh the token, causing an infinite loop.

I don't think this should block #298 but wondering if this will require some more extensive refactoring

@MatthewDorner
Copy link
Contributor

Issue created from a Mattermost message by @thiefmaster.

I was trying to see what specific need prompted this Issue, but the link doesn't work for me.

@ThiefMaster
Copy link

Ping :)

I was trying to see what specific need prompted this Issue, but the link doesn't work for me.

I'd say the "specific need" is simply being a "polite API user". Repeating requests with a token you know is no longer valid is not nice. Some APIs might even (rightfully!) block/throttle you if a client does this too often (luckily GitLab doesn't).

It also spams my log file with errors about the invalid tokens, which makes it hard to see if there's anything useful in the log file.

@mickmister
Copy link
Member Author

@ThiefMaster

  1. Are you running at least v1.5.0 of the plugin?
  2. If so, can you ask your users to reconnect their GitLab accounts, if their token is not working?

We are in the process of a fix that involves automatically disconnecting users that try to use an expired token, and pinging them to reconnect their account.

@ThiefMaster
Copy link

Besides there not being an option to get a list of users who use the plugin / have invalid tokens, I don't think that scales well (I'd have to @all ping a major channel where hopefully most people are). So I'll rather wait a bit until it's fixed properly instead of manually pinging my users..

@hanzei hanzei modified the milestones: Future Consideration, v1.7.0 Dec 6, 2022
@trilopin trilopin modified the milestones: v1.7.0, v1.8.0 Apr 19, 2023
hanzei pushed a commit that referenced this issue May 9, 2023
Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty/1:Easy Easy ticket Hacktoberfest Help Wanted Community help wanted Tech/Go Type/Enhancement New feature or improvement of existing feature
Projects
None yet
6 participants