fix: Do not create oauth clients for non-supported apps
This commit is contained in:
@@ -6,9 +6,9 @@ export default async (request, response) => {
|
|||||||
.findOne({ key: request.params.appKey })
|
.findOne({ key: request.params.appKey })
|
||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
const oauthClient = await appConfig
|
const oauthClient = await appConfig.createOAuthClient(
|
||||||
.$relatedQuery('oauthClients')
|
oauthClientParams(request)
|
||||||
.insert(oauthClientParams(request));
|
);
|
||||||
|
|
||||||
renderObject(response, oauthClient, { status: 201 });
|
renderObject(response, oauthClient, { status: 201 });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -48,6 +48,34 @@ describe('POST /api/v1/admin/apps/:appKey/oauth-clients', () => {
|
|||||||
expect(response.body).toMatchObject(expectedPayload);
|
expect(response.body).toMatchObject(expectedPayload);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should throw validation error for app that does not support oauth connections', async () => {
|
||||||
|
await createAppConfig({
|
||||||
|
key: 'deepl',
|
||||||
|
});
|
||||||
|
|
||||||
|
const oauthClient = {
|
||||||
|
active: true,
|
||||||
|
appKey: 'deepl',
|
||||||
|
name: 'First auth client',
|
||||||
|
formattedAuthDefaults: {
|
||||||
|
clientid: 'sample client ID',
|
||||||
|
clientSecret: 'sample client secret',
|
||||||
|
instanceUrl: 'https://deepl.com',
|
||||||
|
oAuthRedirectUrl: 'http://localhost:3001/app/deepl/connection/add',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.post('/api/v1/admin/apps/deepl/oauth-clients')
|
||||||
|
.set('Authorization', token)
|
||||||
|
.send(oauthClient)
|
||||||
|
.expect(422);
|
||||||
|
|
||||||
|
expect(response.body.errors).toMatchObject({
|
||||||
|
app: ['This app does not support OAuth clients!'],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should return not found response for not existing app config', async () => {
|
it('should return not found response for not existing app config', async () => {
|
||||||
const oauthClient = {
|
const oauthClient = {
|
||||||
active: true,
|
active: true,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import App from './app.js';
|
import App from './app.js';
|
||||||
import OAuthClient from './oauth-client.js';
|
import OAuthClient from './oauth-client.js';
|
||||||
import Base from './base.js';
|
import Base from './base.js';
|
||||||
|
import { ValidationError } from 'objection';
|
||||||
|
|
||||||
class AppConfig extends Base {
|
class AppConfig extends Base {
|
||||||
static tableName = 'app_configs';
|
static tableName = 'app_configs';
|
||||||
@@ -39,6 +40,27 @@ class AppConfig extends Base {
|
|||||||
|
|
||||||
return await App.findOneByKey(this.key);
|
return await App.findOneByKey(this.key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async createOAuthClient(params) {
|
||||||
|
const supportsOauthClients = (await this.getApp())?.auth?.generateAuthUrl
|
||||||
|
? true
|
||||||
|
: false;
|
||||||
|
|
||||||
|
if (!supportsOauthClients) {
|
||||||
|
throw new ValidationError({
|
||||||
|
data: {
|
||||||
|
app: [
|
||||||
|
{
|
||||||
|
message: 'This app does not support OAuth clients!',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
type: 'ModelValidation',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.$relatedQuery('oauthClients').insert(params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default AppConfig;
|
export default AppConfig;
|
||||||
|
|||||||
Reference in New Issue
Block a user