From 80d5c2baf6ba602fa6a968c84e7778cad4948cf2 Mon Sep 17 00:00:00 2001 From: Abhijeet <41686026+abhvsn@users.noreply.github.com> Date: Wed, 24 Jan 2024 15:16:12 +0530 Subject: [PATCH] chore: Add `workspaceId` to the redirect url for auth datasource flow (#30463) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description > Add a TL;DR when description is extra long (helps content team) > > Please include a summary of the changes and which issue has been fixed. Please also include relevant motivation > and context. List any dependencies that are required for this change > > Links to Notion, Figma or any other documents that might be relevant to the PR > > #### PR fixes following issue(s) Fixes # (issue number) > if no issue exists, please create an issue and ask the maintainers about this first > > #### Media > A video or a GIF is preferred. when using Loom, don’t embed because it looks like it’s a GIF. instead, just link to the video > > #### Type of change > Please delete options that are not relevant. - Bug fix (non-breaking change which fixes an issue) - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) - Chore (housekeeping or task changes that don't impact user perception) - This change requires a documentation update > > > ## Testing > #### How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Also list any relevant details for your test configuration. > Delete anything that is not relevant - [ ] Manual - [ ] JUnit - [ ] Jest - [ ] Cypress > > #### Test Plan > Add Testsmith test cases links that relate to this PR > > #### Issues raised during DP testing > Link issues raised during DP testing for better visiblity and tracking (copy link from comments dropped on this PR) > > > ## Checklist: #### Dev activity - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag #### QA activity: - [ ] [Speedbreak features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-) have been covered - [ ] Test plan covers all impacted features and [areas of interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-) - [ ] Test plan has been peer reviewed by project stakeholders and other QA members - [ ] Manually tested functionality on DP - [ ] We had an implementation alignment call with stakeholders post QA Round 2 - [ ] Cypress test cases have been added and approved by SDET/manual QA - [ ] Added `Test Plan Approved` label after Cypress tests were reviewed - [ ] Added `Test Plan Approved` label after JUnit tests were reviewed ## Summary by CodeRabbit - **New Features** - Integrated workspace identification into authentication services for enhanced multi-workspace support. - **Refactor** - Improved internal handling of workspace-specific data during authentication processes. - **Tests** - Updated tests to include workspace identification checks in authentication scenarios. --------- Co-authored-by: “sneha122” <“sneha@appsmith.com”> --- .../com/appsmith/server/dtos/IntegrationDTO.java | 2 ++ .../ce/AuthenticationServiceCEImpl.java | 16 +++++++++++----- .../solutions/AuthenticationServiceTest.java | 4 +++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/IntegrationDTO.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/IntegrationDTO.java index 709744c36f..4a0eb2eee1 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/IntegrationDTO.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/IntegrationDTO.java @@ -18,6 +18,8 @@ public class IntegrationDTO { String datasourceId; + String workspaceId; + String applicationId; String pageId; diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ce/AuthenticationServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ce/AuthenticationServiceCEImpl.java index ff52df46a8..400391f4db 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ce/AuthenticationServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ce/AuthenticationServiceCEImpl.java @@ -116,6 +116,7 @@ public class AuthenticationServiceCEImpl implements AuthenticationServiceCE { .flatMap(datasource -> datasourceService.getTrueEnvironmentId( datasource.getWorkspaceId(), environmentId, datasource.getPluginId(), null)) .cache(); + Mono workspaceIdMono = datasourceMonoCached.map(Datasource::getWorkspaceId); return datasourceMonoCached .zipWith(trueEnvironmentIdCached) @@ -127,16 +128,18 @@ public class AuthenticationServiceCEImpl implements AuthenticationServiceCE { .switchIfEmpty(Mono.error( new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, FieldName.DATASOURCE, datasourceId))) .flatMap(this::validateRequiredFieldsForGenericOAuth2) - .zipWith(trueEnvironmentIdCached) + .zipWith(Mono.zip(workspaceIdMono, trueEnvironmentIdCached)) .flatMap(tuple2 -> { DatasourceStorage datasourceStorage = tuple2.getT1(); - String trueEnvironmentId = tuple2.getT2(); + String workspaceId = tuple2.getT2().getT1(); + String trueEnvironmentId = tuple2.getT2().getT2(); OAuth2 oAuth2 = (OAuth2) datasourceStorage.getDatasourceConfiguration().getAuthentication(); final String redirectUri = redirectHelper.getRedirectDomain(httpRequest.getHeaders()); final String state = StringUtils.hasText(branchName) - ? String.join(",", pageId, datasourceId, trueEnvironmentId, redirectUri, branchName) - : String.join(",", pageId, datasourceId, trueEnvironmentId, redirectUri); + ? String.join( + ",", pageId, datasourceId, trueEnvironmentId, redirectUri, workspaceId, branchName) + : String.join(",", pageId, datasourceId, trueEnvironmentId, redirectUri, workspaceId); // Adding basic uri components UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString( oAuth2.getAuthorizationUrl()) @@ -332,7 +335,8 @@ public class AuthenticationServiceCEImpl implements AuthenticationServiceCE { final String datasourceId = splitState[1]; final String environmentId = splitState[2]; final String redirectOrigin = splitState[3]; - final String branchName = splitState.length == 5 ? splitState[4] : null; + final String workspaceId = splitState[4]; + final String branchName = splitState.length == 6 ? splitState[5] : null; String response = SUCCESS; if (error != null) { response = error; @@ -350,6 +354,7 @@ public class AuthenticationServiceCEImpl implements AuthenticationServiceCE { + "?response_status=" + responseStatus + "&view_mode=true" + + (StringUtils.hasText(workspaceId) ? "&workspaceId=" + workspaceId : "") + (StringUtils.hasText(branchName) ? "&branch=" + branchName : "")) .onErrorResume(e -> Mono.just(redirectOrigin + Entity.SLASH + Entity.APPLICATIONS + "?response_status=" @@ -415,6 +420,7 @@ public class AuthenticationServiceCEImpl implements AuthenticationServiceCE { integrationDTO.setApplicationId(defaultApplicationId); integrationDTO.setBranch(branchName); integrationDTO.setImportForGit(importForGit); + integrationDTO.setWorkspaceId(datasource.getWorkspaceId()); final Plugin plugin = tuple.getT3(); integrationDTO.setPluginName(plugin.getPluginName()); integrationDTO.setPluginVersion(plugin.getVersion()); diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/AuthenticationServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/AuthenticationServiceTest.java index 1a578ff318..d861b6ffc3 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/AuthenticationServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/AuthenticationServiceTest.java @@ -242,7 +242,8 @@ public class AuthenticationServiceTest { pageDto.getId(), datasourceId1, defaultEnvironmentId, - "https://mock.origin.com") + "https://mock.origin.com", + workspace.getId()) + "&scope=Scope\\d%20Scope\\d" + "&key=value")); }) @@ -336,6 +337,7 @@ public class AuthenticationServiceTest { datasourceId, defaultEnvironmentId, "https://mock.origin.com", + workspaceId, "testBranch") + "&scope=Scope\\d%20Scope\\d" + "&key=value"));