Skip to content

Commit

Permalink
Convert files to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Sep 30, 2022
1 parent 63903c0 commit 9191a8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import { Users } from '../../../models/server';
import { userScopes } from '../oauthScopes';
import { SystemLogger } from '../../../../server/lib/logger/system';

export function finishOAuthAuthorization(code, state) {
export function finishOAuthAuthorization(code: string, state: string) {
if (settings.get('Cloud_Workspace_Registration_State') !== state) {
throw new Meteor.Error('error-invalid-state', 'Invalid state provided', {
method: 'cloud:finishOAuthAuthorization',
});
}

const cloudUrl = settings.get('Cloud_Url');
const clientId = settings.get('Cloud_Workspace_Client_Id');
const clientSecret = settings.get('Cloud_Workspace_Client_Secret');
const clientId = settings.get<string>('Cloud_Workspace_Client_Id');
const clientSecret = settings.get<string>('Cloud_Workspace_Client_Secret');

const scope = userScopes.join(' ');

Expand All @@ -33,7 +33,7 @@ export function finishOAuthAuthorization(code, state) {
redirect_uri: getRedirectUri(),
},
});
} catch (err) {
} catch (err: any) {
SystemLogger.error({
msg: 'Failed to finish OAuth authorization with Rocket.Chat Cloud',
url: '/api/oauth/token',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function getWorkspaceAccessTokenWithScope(scope = '') {
return tokenResponse;
}

const client_id = settings.get('Cloud_Workspace_Client_Id');
if (!client_id) {
const clientId = settings.get<string>('Cloud_Workspace_Client_Id');
if (!clientId) {
return tokenResponse;
}

Expand All @@ -26,22 +26,22 @@ export function getWorkspaceAccessTokenWithScope(scope = '') {
}

const cloudUrl = settings.get('Cloud_Url');
const client_secret = settings.get('Cloud_Workspace_Client_Secret');
const clientSecret = settings.get<string>('Cloud_Workspace_Client_Secret');
const redirectUri = getRedirectUri();

let authTokenResult;
try {
authTokenResult = HTTP.post(`${cloudUrl}/api/oauth/token`, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
params: {
client_id,
client_secret,
client_id: clientId,
client_secret: clientSecret,
scope,
grant_type: 'client_credentials',
redirect_uri: redirectUri,
},
});
} catch (err) {
} catch (err: any) {
SystemLogger.error({
msg: 'Failed to get Workspace AccessToken from Rocket.Chat Cloud',
url: '/api/oauth/token',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Users } from '../../../models/server';
import { settings } from '../../../settings/server';
import { SystemLogger } from '../../../../server/lib/logger/system';

export function userLogout(userId) {
export function userLogout(userId?: string) {
const { connectToCloud, workspaceRegistered } = retrieveRegistrationStatus();

if (!connectToCloud || !workspaceRegistered) {
Expand All @@ -19,33 +19,34 @@ export function userLogout(userId) {

const user = Users.findOneById(userId);

if (user && user.services && user.services.cloud && user.services.cloud.refreshToken) {
if (user?.services?.cloud?.refreshToken) {
try {
const client_id = settings.get('Cloud_Workspace_Client_Id');
if (!client_id) {
const clientId = settings.get<string>('Cloud_Workspace_Client_Id');
if (!clientId) {
return '';
}

const cloudUrl = settings.get('Cloud_Url');
const client_secret = settings.get('Cloud_Workspace_Client_Secret');
const clientSecret = settings.get<string>('Cloud_Workspace_Client_Secret');

const { refreshToken } = user.services.cloud;

HTTP.post(`${cloudUrl}/api/oauth/revoke`, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
params: {
client_id,
client_secret,
client_id: clientId,
client_secret: clientSecret,
token: refreshToken,
token_type_hint: 'refresh_token',
},
});
} catch (e) {
if (e.response && e.response.data && e.response.data.error) {
SystemLogger.error(`Failed to get Revoke refresh token to logout of Rocket.Chat Cloud. Error: ${e.response.data.error}`);
} else {
SystemLogger.error(e);
}
} catch (err: any) {
SystemLogger.error({
msg: 'Failed to get Revoke refresh token to logout of Rocket.Chat Cloud',
url: '/api/oauth/revoke',
...(err.response?.data?.error && { errorMessage: err.response.data.error }),
err,
});
}
}

Expand Down

0 comments on commit 9191a8b

Please sign in to comment.