diff --git a/libs/auth-api-lib/migrations/20241018150418-add-default-scopes-to-client-allowed-scope-table.js b/libs/auth-api-lib/migrations/20241018150418-add-default-scopes-to-client-allowed-scope-table.js new file mode 100644 index 000000000000..d9e364fd4ebd --- /dev/null +++ b/libs/auth-api-lib/migrations/20241018150418-add-default-scopes-to-client-allowed-scope-table.js @@ -0,0 +1,53 @@ +'use strict' + +module.exports = { + async up(queryInterface, Sequelize) { + const clients = await queryInterface.sequelize.query( + `SELECT "client_id" FROM "client" WHERE "client_type" IN ('web', 'native', 'spa');`, + { type: queryInterface.sequelize.QueryTypes.SELECT }, + ) + const clientIds = clients.map((client) => client.client_id) + + if (!clientIds.length) { + return + } + + const existingScopes = await queryInterface.sequelize.query( + `SELECT "client_id", "scope_name" FROM "client_allowed_scope" WHERE "client_id" IN (:clientIds);`, + { + type: queryInterface.sequelize.QueryTypes.SELECT, + replacements: { clientIds }, + }, + ) + + const existingScopesLookup = new Set( + existingScopes.map((scope) => `${scope.client_id}-${scope.scope_name}`), + ) + + const rows = [] + const defaultScopes = ['email', 'address', 'phone'] + + for (const clientId of clientIds) { + rows.push( + ...defaultScopes + .filter((scope) => { + return !existingScopesLookup.has(`${clientId}-${scope}`) + }) + .map((scope) => ({ client_id: clientId, scope_name: scope })), + ) + } + + if (!rows.length) { + return + } + + await queryInterface.bulkInsert('client_allowed_scope', rows) + }, + + async down(queryInterface, Sequelize) { + /** + * There is no need to go back + * + */ + }, +} diff --git a/libs/auth-api-lib/src/lib/clients/admin/admin-clients.service.ts b/libs/auth-api-lib/src/lib/clients/admin/admin-clients.service.ts index 443a1071149a..f919f1611294 100644 --- a/libs/auth-api-lib/src/lib/clients/admin/admin-clients.service.ts +++ b/libs/auth-api-lib/src/lib/clients/admin/admin-clients.service.ts @@ -631,10 +631,26 @@ export class AdminClientsService { switch (client.clientType) { case ClientType.web: case ClientType.native: - scopes.push({ - clientId: client.clientId, - scopeName: 'profile', - }) + scopes.push( + ...[ + { + clientId: client.clientId, + scopeName: 'profile', + }, + { + clientId: client.clientId, + scopeName: 'email', + }, + { + clientId: client.clientId, + scopeName: 'phone', + }, + { + clientId: client.clientId, + scopeName: 'address', + }, + ], + ) } return scopes