-
Notifications
You must be signed in to change notification settings - Fork 2
MS-448 Ad-hoc tokenisation improvements #1520
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a772d8f
MS-448 Cleanup ad-hoc tokenization function
luhmirin-s 27ef2ca
MS-448 Move the ad-hoc tokenization from every project refresh call t…
luhmirin-s 90597c4
MS-448 Schedule configuration sync immediately after app update
luhmirin-s d2cd3e7
MS-448 Double check that values in action request are tokenised after…
luhmirin-s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
...main/java/com/simprints/feature/logincheck/usecases/EnsureActionFieldsTokenizedUseCase.kt
This file contains hidden or 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,43 @@ | ||
| package com.simprints.feature.logincheck.usecases | ||
|
|
||
| import com.simprints.infra.config.store.models.TokenKeyType | ||
| import com.simprints.infra.config.store.tokenization.TokenizationProcessor | ||
| import com.simprints.infra.config.sync.ConfigManager | ||
| import com.simprints.infra.orchestration.data.ActionRequest | ||
| import javax.inject.Inject | ||
|
|
||
| class EnsureActionFieldsTokenizedUseCase @Inject constructor( | ||
| private val configManager: ConfigManager, | ||
| private val tokenizationProcessor: TokenizationProcessor, | ||
| ) { | ||
| suspend operator fun invoke(action: ActionRequest): ActionRequest { | ||
| val project = configManager.getProject() ?: return action | ||
|
|
||
| // There is no automatic `.copy()` on the the interfaces, so we have to enumerate all sub-classes separately | ||
| return when (action) { | ||
| is ActionRequest.EnrolActionRequest -> action.copy( | ||
| userId = tokenizationProcessor.tokenizeIfNecessary(action.userId, TokenKeyType.AttendantId, project), | ||
| moduleId = tokenizationProcessor.tokenizeIfNecessary(action.moduleId, TokenKeyType.ModuleId, project), | ||
| ) | ||
|
|
||
| is ActionRequest.IdentifyActionRequest -> action.copy( | ||
| userId = tokenizationProcessor.tokenizeIfNecessary(action.userId, TokenKeyType.AttendantId, project), | ||
| moduleId = tokenizationProcessor.tokenizeIfNecessary(action.moduleId, TokenKeyType.ModuleId, project), | ||
| ) | ||
|
|
||
| is ActionRequest.VerifyActionRequest -> action.copy( | ||
| userId = tokenizationProcessor.tokenizeIfNecessary(action.userId, TokenKeyType.AttendantId, project), | ||
| moduleId = tokenizationProcessor.tokenizeIfNecessary(action.moduleId, TokenKeyType.ModuleId, project), | ||
| ) | ||
|
|
||
| is ActionRequest.EnrolLastBiometricActionRequest -> action.copy( | ||
| userId = tokenizationProcessor.tokenizeIfNecessary(action.userId, TokenKeyType.AttendantId, project), | ||
| moduleId = tokenizationProcessor.tokenizeIfNecessary(action.moduleId, TokenKeyType.ModuleId, project), | ||
| ) | ||
|
|
||
| is ActionRequest.ConfirmIdentityActionRequest -> action.copy( | ||
| userId = tokenizationProcessor.tokenizeIfNecessary(action.userId, TokenKeyType.AttendantId, project), | ||
| ) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
134 changes: 134 additions & 0 deletions
134
.../java/com/simprints/feature/logincheck/usecases/EnsureActionFieldsTokenisedUseCaseTest.kt
This file contains hidden or 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,134 @@ | ||
| package com.simprints.feature.logincheck.usecases | ||
|
|
||
| import com.google.common.truth.Truth.* | ||
| import com.simprints.core.domain.tokenization.asTokenizableEncrypted | ||
| import com.simprints.core.domain.tokenization.asTokenizableRaw | ||
| import com.simprints.infra.config.store.models.Project | ||
| import com.simprints.infra.config.store.models.TokenKeyType | ||
| import com.simprints.infra.config.store.tokenization.TokenizationProcessor | ||
| import com.simprints.infra.config.sync.ConfigManager | ||
| import com.simprints.infra.orchestration.data.ActionRequest | ||
| import io.mockk.* | ||
| import io.mockk.impl.annotations.MockK | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
|
|
||
| class EnsureActionFieldsTokenisedUseCaseTest { | ||
| @MockK | ||
| private lateinit var configManager: ConfigManager | ||
|
|
||
| @MockK | ||
| private lateinit var tokenizationProcessor: TokenizationProcessor | ||
|
|
||
| @MockK | ||
| private lateinit var project: Project | ||
|
|
||
| private lateinit var useCase: EnsureActionFieldsTokenizedUseCase | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| MockKAnnotations.init(this, relaxed = true) | ||
| useCase = EnsureActionFieldsTokenizedUseCase(configManager, tokenizationProcessor) | ||
|
|
||
| every { tokenizationProcessor.tokenizeIfNecessary(any(), any(), any()) } returns "value".asTokenizableEncrypted() | ||
| } | ||
|
|
||
| @Test | ||
| fun `when project is null, return original action`() = runTest { | ||
| coEvery { configManager.getProject() } returns null | ||
|
|
||
| val expected = mockk<ActionRequest>() | ||
| val result = useCase(expected) | ||
|
|
||
| assertThat(result).isSameInstanceAs(expected) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when invoking with EnrolActionRequest, attempt tokenizing fields`() = runTest { | ||
| coEvery { configManager.getProject() } returns project | ||
|
|
||
| val action = mockk<ActionRequest.EnrolActionRequest> { | ||
| every { userId } returns "user".asTokenizableRaw() | ||
| every { moduleId } returns "module".asTokenizableRaw() | ||
| } | ||
| every { action.copy(userId = any(), moduleId = any()) } returns action | ||
| val result = useCase(action) | ||
|
|
||
| assertThat(result).isInstanceOf(ActionRequest.EnrolActionRequest::class.java) | ||
| verify { | ||
| tokenizationProcessor.tokenizeIfNecessary(any(), TokenKeyType.ModuleId, any()) | ||
| tokenizationProcessor.tokenizeIfNecessary(any(), TokenKeyType.AttendantId, any()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `when invoking with VerifyActionRequest, attempt tokenizing fields`() = runTest { | ||
| coEvery { configManager.getProject() } returns project | ||
|
|
||
| val action = mockk<ActionRequest.VerifyActionRequest> { | ||
| every { userId } returns "user".asTokenizableRaw() | ||
| every { moduleId } returns "module".asTokenizableRaw() | ||
| } | ||
| every { action.copy(userId = any(), moduleId = any()) } returns action | ||
| val result = useCase(action) | ||
|
|
||
| assertThat(result).isInstanceOf(ActionRequest.VerifyActionRequest::class.java) | ||
| verify { | ||
| tokenizationProcessor.tokenizeIfNecessary(any(), TokenKeyType.ModuleId, any()) | ||
| tokenizationProcessor.tokenizeIfNecessary(any(), TokenKeyType.AttendantId, any()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `when invoking with IdentifyActionRequest, attempt tokenizing fields`() = runTest { | ||
| coEvery { configManager.getProject() } returns project | ||
|
|
||
| val action = mockk<ActionRequest.IdentifyActionRequest> { | ||
| every { userId } returns "user".asTokenizableRaw() | ||
| every { moduleId } returns "module".asTokenizableRaw() | ||
| } | ||
| every { action.copy(userId = any(), moduleId = any()) } returns action | ||
| val result = useCase(action) | ||
|
|
||
| assertThat(result).isInstanceOf(ActionRequest.IdentifyActionRequest::class.java) | ||
| verify { | ||
| tokenizationProcessor.tokenizeIfNecessary(any(), TokenKeyType.ModuleId, any()) | ||
| tokenizationProcessor.tokenizeIfNecessary(any(), TokenKeyType.AttendantId, any()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `when invoking with ConfirmIdentityActionRequest, attempt tokenizing fields`() = runTest { | ||
| coEvery { configManager.getProject() } returns project | ||
|
|
||
| val action = mockk<ActionRequest.ConfirmIdentityActionRequest> { | ||
| every { userId } returns "user".asTokenizableRaw() | ||
| } | ||
| every { action.copy(userId = any()) } returns action | ||
| val result = useCase(action) | ||
|
|
||
| assertThat(result).isInstanceOf(ActionRequest.ConfirmIdentityActionRequest::class.java) | ||
| verify { | ||
| tokenizationProcessor.tokenizeIfNecessary(any(), TokenKeyType.AttendantId, any()) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `when invoking with EnrolLastBiometricActionRequest, attempt tokenizing fields`() = runTest { | ||
| coEvery { configManager.getProject() } returns project | ||
|
|
||
| val action = mockk<ActionRequest.EnrolLastBiometricActionRequest> { | ||
| every { userId } returns "user".asTokenizableRaw() | ||
| every { moduleId } returns "module".asTokenizableRaw() | ||
| } | ||
| every { action.copy(userId = any(), moduleId = any()) } returns action | ||
| val result = useCase(action) | ||
|
|
||
| assertThat(result).isInstanceOf(ActionRequest.EnrolLastBiometricActionRequest::class.java) | ||
| verify { | ||
| tokenizationProcessor.tokenizeIfNecessary(any(), TokenKeyType.ModuleId, any()) | ||
| tokenizationProcessor.tokenizeIfNecessary(any(), TokenKeyType.AttendantId, any()) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ package com.simprints.infra.enrolment.records.repository | |
|
|
||
| import androidx.core.content.edit | ||
| import com.simprints.core.DispatcherIO | ||
| import com.simprints.core.domain.tokenization.TokenizableString | ||
| import com.simprints.infra.config.store.models.Project | ||
| import com.simprints.infra.config.store.models.TokenKeyType | ||
| import com.simprints.infra.config.store.tokenization.TokenizationProcessor | ||
|
|
@@ -63,23 +62,23 @@ internal class EnrolmentRecordRepositoryImpl @Inject constructor( | |
| override suspend fun tokenizeExistingRecords(project: Project) { | ||
| try { | ||
| val query = EnrolmentRecordQuery(projectId = project.id, hasUntokenizedFields = true) | ||
| val tokenizedSubjectsCreateAction = selectEnrolmentRecordLocalDataSource() | ||
| val tokenizedRecordsCreateAction = selectEnrolmentRecordLocalDataSource() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be untokenizedRecordsCreateAction?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the end of the call chain the final value is indeed tokenized. |
||
| .load(query) | ||
| .mapNotNull { subject -> | ||
| if (subject.projectId != project.id) return@mapNotNull null | ||
| val moduleId = tokenizeIfNecessary( | ||
| value = subject.moduleId, | ||
| .mapNotNull { record -> | ||
| if (record.projectId != project.id) return@mapNotNull null | ||
| val moduleId = tokenizationProcessor.tokenizeIfNecessary( | ||
| tokenizableString = record.moduleId, | ||
| tokenKeyType = TokenKeyType.ModuleId, | ||
| project = project, | ||
| ) | ||
| val attendantId = tokenizeIfNecessary( | ||
| value = subject.attendantId, | ||
| val attendantId = tokenizationProcessor.tokenizeIfNecessary( | ||
| tokenizableString = record.attendantId, | ||
| tokenKeyType = TokenKeyType.AttendantId, | ||
| project = project, | ||
| ) | ||
| return@mapNotNull subject.copy(moduleId = moduleId, attendantId = attendantId) | ||
| record.copy(moduleId = moduleId, attendantId = attendantId) | ||
| }.map(EnrolmentRecordAction::Creation) | ||
| selectEnrolmentRecordLocalDataSource().performActions(tokenizedSubjectsCreateAction, project) | ||
| selectEnrolmentRecordLocalDataSource().performActions(tokenizedRecordsCreateAction, project) | ||
| } catch (e: Exception) { | ||
| when (e) { | ||
| is RealmUninitialisedException -> Unit | ||
|
|
@@ -90,20 +89,6 @@ internal class EnrolmentRecordRepositoryImpl @Inject constructor( | |
| } | ||
| } | ||
|
|
||
| private fun tokenizeIfNecessary( | ||
| value: TokenizableString, | ||
| tokenKeyType: TokenKeyType, | ||
| project: Project, | ||
| ) = when (value) { | ||
| is TokenizableString.Tokenized -> value | ||
|
|
||
| is TokenizableString.Raw -> tokenizationProcessor.encrypt( | ||
| decrypted = value, | ||
| tokenKeyType = tokenKeyType, | ||
| project = project, | ||
| ) | ||
| } | ||
|
|
||
| override suspend fun count( | ||
| query: EnrolmentRecordQuery, | ||
| dataSource: BiometricDataSource, | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.