-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce app configs with shared auth clients
- Loading branch information
Showing
47 changed files
with
1,499 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/backend/src/db/migrations/20230812132005_create_app_configs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema.createTable('app_configs', (table) => { | ||
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()')); | ||
table.string('key').unique().notNullable(); | ||
table.boolean('allow_custom_connection').notNullable().defaultTo(false); | ||
table.boolean('shared').notNullable().defaultTo(false); | ||
table.boolean('disabled').notNullable().defaultTo(false); | ||
|
||
table.timestamps(true, true); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('app_configs'); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/backend/src/db/migrations/20230813172729_create_app_auth_clients.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema.createTable('app_auth_clients', (table) => { | ||
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()')); | ||
table.string('name').unique().notNullable(); | ||
table.uuid('app_config_id').notNullable().references('id').inTable('app_configs'); | ||
table.text('auth_defaults').notNullable(); | ||
table.boolean('active').notNullable().defaultTo(false); | ||
|
||
table.timestamps(true, true); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('app_auth_clients'); | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/backend/src/db/migrations/20230815161102_add_app_auth_client_id_in_connections.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
await knex.schema.table('connections', async (table) => { | ||
table.uuid('app_auth_client_id').references('id').inTable('app_auth_clients'); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return await knex.schema.table('connections', (table) => { | ||
table.dropColumn('app_auth_client_id'); | ||
}); | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/backend/src/db/migrations/20230816121044_seed_update_app_permissions_to_admin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Knex } from 'knex'; | ||
|
||
const getPermissionForRole = ( | ||
roleId: string, | ||
subject: string, | ||
actions: string[] | ||
) => | ||
actions.map((action) => ({ | ||
role_id: roleId, | ||
subject, | ||
action, | ||
conditions: [], | ||
})); | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
const role = (await knex('roles') | ||
.first(['id', 'key']) | ||
.where({ key: 'admin' }) | ||
.limit(1)) as { id: string; key: string }; | ||
|
||
await knex('permissions').insert( | ||
getPermissionForRole(role.id, 'App', [ | ||
'create', | ||
'read', | ||
'delete', | ||
'update', | ||
]) | ||
); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex('permissions').where({ subject: 'App' }).delete(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/backend/src/graphql/mutations/create-app-auth-client.ee.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { IJSONObject } from '@automatisch/types'; | ||
import AppConfig from '../../models/app-config'; | ||
import Context from '../../types/express/context'; | ||
|
||
type Params = { | ||
input: { | ||
appConfigId: string; | ||
name: string; | ||
formattedAuthDefaults?: IJSONObject; | ||
active?: boolean; | ||
}; | ||
}; | ||
|
||
const createAppAuthClient = async ( | ||
_parent: unknown, | ||
params: Params, | ||
context: Context | ||
) => { | ||
context.currentUser.can('update', 'App'); | ||
|
||
const appConfig = await AppConfig | ||
.query() | ||
.findById(params.input.appConfigId) | ||
.throwIfNotFound(); | ||
|
||
const appAuthClient = await appConfig | ||
.$relatedQuery('appAuthClients') | ||
.insert( | ||
params.input | ||
); | ||
|
||
return appAuthClient; | ||
}; | ||
|
||
export default createAppAuthClient; |
36 changes: 36 additions & 0 deletions
36
packages/backend/src/graphql/mutations/create-app-config.ee.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import App from '../../models/app'; | ||
import AppConfig from '../../models/app-config'; | ||
import Context from '../../types/express/context'; | ||
|
||
type Params = { | ||
input: { | ||
key: string; | ||
allowCustomConnection?: boolean; | ||
shared?: boolean; | ||
disabled?: boolean; | ||
}; | ||
}; | ||
|
||
const createAppConfig = async ( | ||
_parent: unknown, | ||
params: Params, | ||
context: Context | ||
) => { | ||
context.currentUser.can('update', 'App'); | ||
|
||
const key = params.input.key; | ||
|
||
const app = await App.findOneByKey(key); | ||
|
||
if (!app) throw new Error('The app cannot be found!'); | ||
|
||
const appConfig = await AppConfig | ||
.query() | ||
.insert( | ||
params.input | ||
); | ||
|
||
return appConfig; | ||
}; | ||
|
||
export default createAppConfig; |
46 changes: 39 additions & 7 deletions
46
packages/backend/src/graphql/mutations/create-connection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,59 @@ | ||
import { IJSONObject } from '@automatisch/types'; | ||
import App from '../../models/app'; | ||
import AppConfig from '../../models/app-config'; | ||
import Context from '../../types/express/context'; | ||
import { IJSONObject } from '@automatisch/types'; | ||
|
||
type Params = { | ||
input: { | ||
key: string; | ||
appAuthClientId: string; | ||
formattedData: IJSONObject; | ||
}; | ||
}; | ||
|
||
const createConnection = async ( | ||
_parent: unknown, | ||
params: Params, | ||
context: Context | ||
) => { | ||
context.currentUser.can('create', 'Connection'); | ||
|
||
await App.findOneByKey(params.input.key); | ||
const { key, appAuthClientId } = params.input; | ||
|
||
const app = await App.findOneByKey(key); | ||
|
||
const appConfig = await AppConfig.query().findOne({ key }); | ||
|
||
let formattedData = params.input.formattedData; | ||
if (appConfig) { | ||
if (appConfig.disabled) throw new Error('This application has been disabled for new connections!'); | ||
|
||
if (!appConfig.allowCustomConnection && formattedData) throw new Error(`Custom connections cannot be created for ${app.name}!`); | ||
|
||
if (appConfig.shared && !formattedData) { | ||
const authClient = await appConfig | ||
.$relatedQuery('appAuthClients') | ||
.findById(appAuthClientId) | ||
.where({ | ||
active: true | ||
}) | ||
.throwIfNotFound(); | ||
|
||
formattedData = authClient.formattedAuthDefaults; | ||
} | ||
} | ||
|
||
const createdConnection = await context | ||
.currentUser | ||
.$relatedQuery('connections') | ||
.insert({ | ||
key, | ||
appAuthClientId, | ||
formattedData, | ||
verified: false, | ||
}); | ||
|
||
return await context.currentUser.$relatedQuery('connections').insert({ | ||
key: params.input.key, | ||
formattedData: params.input.formattedData, | ||
verified: false, | ||
}); | ||
return createdConnection; | ||
}; | ||
|
||
export default createConnection; |
28 changes: 28 additions & 0 deletions
28
packages/backend/src/graphql/mutations/delete-app-auth-client.ee.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Context from '../../types/express/context'; | ||
import AppAuthClient from '../../models/app-auth-client'; | ||
|
||
type Params = { | ||
input: { | ||
id: string; | ||
}; | ||
}; | ||
|
||
const deleteAppAuthClient = async ( | ||
_parent: unknown, | ||
params: Params, | ||
context: Context | ||
) => { | ||
context.currentUser.can('delete', 'App'); | ||
|
||
await AppAuthClient | ||
.query() | ||
.delete() | ||
.findOne({ | ||
id: params.input.id, | ||
}) | ||
.throwIfNotFound(); | ||
|
||
return; | ||
}; | ||
|
||
export default deleteAppAuthClient; |
38 changes: 38 additions & 0 deletions
38
packages/backend/src/graphql/mutations/update-app-auth-client.ee.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { IJSONObject } from '@automatisch/types'; | ||
import AppAuthClient from '../../models/app-auth-client'; | ||
import Context from '../../types/express/context'; | ||
|
||
type Params = { | ||
input: { | ||
id: string; | ||
name: string; | ||
formattedAuthDefaults?: IJSONObject; | ||
active?: boolean; | ||
}; | ||
}; | ||
|
||
const updateAppAuthClient = async ( | ||
_parent: unknown, | ||
params: Params, | ||
context: Context | ||
) => { | ||
context.currentUser.can('update', 'App'); | ||
|
||
const { | ||
id, | ||
...appAuthClientData | ||
} = params.input; | ||
|
||
const appAuthClient = await AppAuthClient | ||
.query() | ||
.findById(id) | ||
.throwIfNotFound(); | ||
|
||
await appAuthClient | ||
.$query() | ||
.patch(appAuthClientData); | ||
|
||
return appAuthClient; | ||
}; | ||
|
||
export default updateAppAuthClient; |
39 changes: 39 additions & 0 deletions
39
packages/backend/src/graphql/mutations/update-app-config.ee.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import AppConfig from '../../models/app-config'; | ||
import Context from '../../types/express/context'; | ||
|
||
type Params = { | ||
input: { | ||
id: string; | ||
allowCustomConnection?: boolean; | ||
shared?: boolean; | ||
disabled?: boolean; | ||
}; | ||
}; | ||
|
||
const updateAppConfig = async ( | ||
_parent: unknown, | ||
params: Params, | ||
context: Context | ||
) => { | ||
context.currentUser.can('update', 'App'); | ||
|
||
const { | ||
id, | ||
...appConfigToUpdate | ||
} = params.input; | ||
|
||
const appConfig = await AppConfig | ||
.query() | ||
.findById(id) | ||
.throwIfNotFound(); | ||
|
||
await appConfig | ||
.$query() | ||
.patch( | ||
appConfigToUpdate | ||
); | ||
|
||
return appConfig; | ||
}; | ||
|
||
export default updateAppConfig; |
Oops, something went wrong.