OAuth2 Client
The Rocket.Chat App OAuth2 workflow is a feature that lets developers handle OAuth2 authentication on their apps directly within Rocket.Chat

OAuth2 Setup

  • In the main class of your app, we start by importing the methods needed from the IOAuth2 definition.
1
import { IAuthData } from '@rocket.chat/apps-engine/definition/oauth2/IOAuth2';
2
import { createOAuth2Client } from '@rocket.chat/apps-engine/definition/oauth2/OAuth2';
Copied!
  • In setting up the configurations of the app using the extendConfiguration method, we create an instance of the createOAuth2Client we imported above.
1
protected async extendConfiguration(configuration: IConfigurationExtend): Promise<void> {
2
try {
3
await createOAuth2Client(this, this.config)
4
.setup(configuration);
5
await configuration.slashCommands.provideSlashCommand(new AuthCommandCommand(this));
6
} catch (error) {
7
this.getLogger().error('[extendConfiguration] error', error);
8
}
9
}
Copied!
  • The createOAuth2Client method takes in two parameters, createOAuth2Client(this, this.config) being the app itself and an object with props as configuration. Below is a sample of the config parameter as seen in the definition documentation
1
private config = {
2
alias: 'test',
3
accessTokenUri: 'https://oauth2.googleapis.com/token',
4
authUri: 'https://accounts.google.com/o/oauth2/v2/auth',
5
refreshTokenUri: 'https://oauth2.googleapis.com/token',
6
revokeTokenUri: 'https://oauth2.googleapis.com/revoke',
7
callback: this.autorizationCallback.bind(this),
8
};
Copied!
  • Now calling the setup(configuration) method on the createOAuth2Client creates all the setup APIs you need to use.

Using OAuth2

After setup, however, you want to go about implementing OAuth2 on your app is dependent on you.
The OAuth2Client gives you access to multiple methods like getAccessTokenForUser, getAccessTokenForUser, revokeUserAccessToken etc.
Export as PDF
Copy link
Edit on GitHub