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

fix: Unable to log in with third-party apps without "Manage OAuth Apps" permission #32782

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion apps/meteor/app/api/server/v1/oauthapps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { OAuthApps } from '@rocket.chat/models';
import { isUpdateOAuthAppParams, isOauthAppsGetParams, isOauthAppsAddParams, isDeleteOAuthAppParams } from '@rocket.chat/rest-typings';
import {
isDeleteOAuthAppParams,
isOauthAppsAddParams,
isOauthAppsGetParams,
isOauthInfoInfoParams,
isUpdateOAuthAppParams,
} from '@rocket.chat/rest-typings';

import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { apiDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
Expand Down Expand Up @@ -48,6 +54,24 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
'oauth-apps.info',
{ authRequired: true, validateParams: isOauthInfoInfoParams },
{
async get() {
const oauthApp = await OAuthApps.findOneAuthAppByIdOrClientId(this.queryParams);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a projection since you're returning just clientId/name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this will imply updating the model typings for allowing the projection to go as the current method doesn't allow it)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added projection to the findOneAuthAppByIdOrClientId method of the IOAuthAppsModel and OAuthAppsRaw models. I hope I changed the code everywhere it was needed and didn't forget anything.


if (!oauthApp) {
return API.v1.failure('OAuth app not found.');
}

return API.v1.success({
oauthApp: { clientId: oauthApp.clientId, name: oauthApp.name },
});
},
},
);

API.v1.addRoute(
'oauth-apps.update',
{
Expand Down
12 changes: 4 additions & 8 deletions apps/meteor/client/views/oauth/hooks/useOAuthAppQuery.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { IOAuthApps } from '@rocket.chat/core-typings';
import type { IOAuthAppsInfo } from '@rocket.chat/core-typings';
import { useEndpoint } from '@rocket.chat/ui-contexts';
import type { UseQueryOptions } from '@tanstack/react-query';
import { useQuery } from '@tanstack/react-query';

type UseOAuthAppQueryOptions = Omit<
UseQueryOptions<IOAuthApps, unknown, IOAuthApps, readonly ['oauth-app', { readonly clientId: string | undefined }]>,
UseQueryOptions<IOAuthAppsInfo, unknown, IOAuthAppsInfo, readonly ['oauth-app', { readonly clientId: string | undefined }]>,
'queryKey' | 'queryFn'
>;

export const useOAuthAppQuery = (clientId: string | undefined, options?: UseOAuthAppQueryOptions) => {
const getOAuthApp = useEndpoint('GET', '/v1/oauth-apps.get');
const getOAuthApp = useEndpoint('GET', '/v1/oauth-apps.info');

return useQuery(
['oauth-app', { clientId }] as const,
Expand All @@ -19,11 +19,7 @@ export const useOAuthAppQuery = (clientId: string | undefined, options?: UseOAut
}

const { oauthApp } = await getOAuthApp({ clientId });
return {
...oauthApp,
_createdAt: new Date(oauthApp._createdAt),
_updatedAt: new Date(oauthApp._updatedAt),
};
return oauthApp;
},
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
options,
);
Expand Down
18 changes: 18 additions & 0 deletions apps/meteor/tests/end-to-end/api/oauthapps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ describe('[OAuthApps]', () => {
});
});

describe('[/oauth-apps.info]', () => {
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
verdel marked this conversation as resolved.
Show resolved Hide resolved
it('should return a single oauthApp with only client id and name attributes', (done) => {
void request
.get(api('oauth-apps.info'))
.query({ clientId: 'zapier' })
.set(credentials)
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('oauthApp');
expect(res.body.oauthApp.clientId).to.be.equal('zapier');
expect(res.body.oauthApp).to.have.keys(['clientId', 'name']);
expect(Object.keys(res.body.oauthApp)).to.have.length(2);
})
.end(done);
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('[/oauth-apps.create]', () => {
it('should return an error when the user does not have the necessary permission', async () => {
await updatePermission('manage-oauth-apps', []);
Expand Down
9 changes: 6 additions & 3 deletions packages/core-typings/src/IOAuthApps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export interface IOAuthApps {
_id: string;
export interface IOAuthAppsInfo {
clientId: string;
name: string;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal pref: do we need this to be an interface? Can't we just export type OauthAppsInfo = Pick<IOAuthApps, 'clientId' | 'name'> ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, that will be better.

export interface IOAuthApps extends IOAuthAppsInfo {
_id: string;
active: boolean;
clientId: string;
clientSecret: string;
redirectUri: string;
_createdAt: Date;
Expand Down
1 change: 1 addition & 0 deletions packages/rest-typings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export * from './v1/omnichannel';
export * from './v1/oauthapps';
export * from './v1/oauthapps/UpdateOAuthAppParamsPOST';
export * from './v1/oauthapps/OAuthAppsGetParamsGET';
export * from './v1/oauthapps/OAuthAppsInfoParamsGET';
export * from './v1/oauthapps/OAuthAppsAddParamsPOST';
export * from './v1/oauthapps/DeleteOAuthAppParamsDELETE';
export * from './helpers/PaginatedRequest';
Expand Down
9 changes: 8 additions & 1 deletion packages/rest-typings/src/v1/oauthapps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { IOAuthApps, IUser } from '@rocket.chat/core-typings';
import type { IOAuthApps, IOAuthAppsInfo, IUser } from '@rocket.chat/core-typings';

import type { DeleteOAuthAppParams } from './oauthapps/DeleteOAuthAppParamsDELETE';
import type { OauthAppsAddParams } from './oauthapps/OAuthAppsAddParamsPOST';
import type { OauthAppsGetParams } from './oauthapps/OAuthAppsGetParamsGET';
import type { OauthAppsInfoParams } from './oauthapps/OAuthAppsInfoParamsGET';
import type { UpdateOAuthAppParams } from './oauthapps/UpdateOAuthAppParamsPOST';

export type OAuthAppsEndpoint = {
Expand All @@ -18,6 +19,12 @@ export type OAuthAppsEndpoint = {
};
};

'/v1/oauth-apps.info': {
GET: (params: OauthAppsInfoParams) => {
oauthApp: IOAuthAppsInfo;
};
};

'/v1/oauth-apps.create': {
POST: (params: OauthAppsAddParams) => { application: IOAuthApps };
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ajv } from '../Ajv';

export type OauthAppsInfoParams = { clientId: string };

const oauthAppsInfoParamsSchema = {
type: 'object',
properties: {
clientId: {
type: 'string',
},
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
},
required: ['clientId'],
additionalProperties: false,
};

export const isOauthAppsInfoParams = ajv.compile<OauthAppsInfoParams>(oauthAppsInfoParamsSchema);
Loading