OAuth2 is an open standard that allows applications to access user information without exposing passwords. Rocket.Chat OAuth2 Client simplifies this process by handling the OAuth2 flow with third-party services like Google, GitHub, and others directly within Rocket.Chat.
This document demonstrates using OAuth2 to interact with Google APIs in a Rocket.Chat app.
Prerequisites
Ensure you have the following:
Create and deploy the app: Start by creating a new Rocket.Chat app. For this example, name it
OAuth
, and then deploy the app to your workspace.Set up Google API credentials: In the Google API Console, create authorization credentials to obtain your Client ID and Client Secret.
Configure authorized URLs: In the project’s Google API console, configure the following:
Authorized JavaScript origins: Set the authorized JavaScript origins to the URL of your Rocket.Chat workspace.
Authorized redirect URIs: Set the authorized redirect URIs to the app's API URL. You can find this URL by navigating to the app within your Rocket.Chat workspace, select Details, and then view the APIs section.
OAuth2 client setup
To set up the OAuth2 client in the Rocket.Chat app, start by importing the necessary modules into the app's main class:
The code above defines the OAuth2 configuration, which includes the authorization, token endpoints, client ID, client secret, and scopes. The code also imports two files which will be created in the project root folder. These files each have two methods:
OAuth2Service
which manages the OAuth2 operations.OAuthCommand
registers the user commands that interact with the OAuth2 service.
OAuth service setup
Next, create the OAuth2Service.ts
file and implement the service that will handle OAuth2 operations:
Here, the createOAuth2Client
method takes in two parameters:
app: The app itself.
options: An object with props as configuration - see the definition documentation for more details.
The setup()
method configures the OAuth2Client
which is used to access multiple methods like getAccessTokenForUser
, revokeUserAccessToken
etc., that will handle user-specific OAuth2 operations.
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.getUserAuthorizationUrl
: Returns the authorization URL to which the user must be redirected to authorize access to the applicationrefreshUserAccessToken
: Refreshes the user's access token. This is useful when the user access token has expired.revokeUserAccessToken
: This function revokes the user's access token in the service provider. When successfully executed, users must be authenticated again before using the service.
OAuth command setup
To enable users to interact with the OAuth2 setup, create a new file named OAuthCommand.ts
and define the following slash command:
The slash command here is oauth
which accepts different arguments (token
, refresh
, revoke
) to perform corresponding actions.
To see this app in action, deploy the app and try using these slash commands in the workspace:
/oauth
to authorize the app./oauth token
to get the authorization token./oauth refesh
to refresh the token./oauth revoke
to revoke the access.
When the application is successfully authorized, you can verify the app through the third-party apps connected to your Google account. Likewise, when you revoke this access, the app should be subsequently removed.