Links

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

  1. 1.
    Import the methods needed from the IOAuth2 and OAuth2 definitions in the main class of your app.
import { IAuthData } from '@rocket.chat/apps-engine/definition/oauth2/IOAuth2';
import { createOAuth2Client } from '@rocket.chat/apps-engine/definition/oauth2/OAuth2';
  1. 2.
    When setting up the app configurations using the extendConfiguration method, we create an instance of the createOAuth2Client imported above.
1
protected async extendConfiguration(configuration: IConfigurationExtend): Promise<void> {
2
const oauthConfig = {
3
alias: 'test',
4
accessTokenUri: 'https://oauth2.googleapis.com/token',
5
authUri: 'https://accounts.google.com/o/oauth2/v2/auth',
6
refreshTokenUri: 'https://oauth2.googleapis.com/token',
7
revokeTokenUri: 'https://oauth2.googleapis.com/revoke',
8
};
9
10
try {
11
await createOAuth2Client(this, oauthConfig)
12
.setup(configuration);
13
} catch (error) {
14
this.getLogger().error('[extendConfiguration] error', error);
15
}
16
}
  • The createOAuth2Client method takes in two parameters:
  • Now calling the setup(configuration) method on the createOAuth2Client creates all the setup APIs you need to use.

Use OAuth2

After setup, use the OAuth2Client to access multiple methods like getAccessTokenForUser, revokeUserAccessToken etc.

getAccessTokenForUser

Gets the token information for a specific user, if available. This receives the user instance as a parameter and returns data about the authenticated user.
await createOAuth2Client(this, this.config).getAccessTokenForUser(user);

getUserAuthorizationUrl

Returns the authorization URL to which the user must be redirected in order to authorize access to the application.
const url = await createOAuth2Client(this, this.config).getUserAuthorizationUrl(user);

refreshUserAccessToken

Refreshes the user's access token. This is useful when the user access token has expired.
await createOAuth2Client(this, this.config).refreshUserAccessToken(user, persis);

revokeUserAccessToken

Revokes user's access token in the service provider. When successfully executed, users will need to be authenticated again before using the service.
await createOAuth2Client(this, this.config).revokeUserAccessToken(user, persis);
Last modified 3mo ago
Rocket.Chat versions receive support for six months after release.