-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add React Native versions of encryption
- Loading branch information
1 parent
a47abd2
commit d3c78a5
Showing
11 changed files
with
191 additions
and
29 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
examples/react-native/v12/TestApp/src/components/encryption/EncryptMetadata.test.tsx
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,16 @@ | ||
import 'react-native'; | ||
import React from 'react'; | ||
import {render, screen} from '@testing-library/react-native'; | ||
|
||
import {EncryptMetadata} from './EncryptMetadata'; | ||
|
||
// Create encryption key for encryption examples. | ||
const encryptionKey = new ArrayBuffer(64); | ||
|
||
test('linking an anonymous user with an email/password account', async () => { | ||
render(<EncryptMetadata encryptionKey={encryptionKey} />); | ||
|
||
const encryptionResultTextNode = await screen.findByTestId('is-realm-app'); | ||
expect(encryptionResultTextNode).toBeInTheDocument; | ||
expect(encryptionResultTextNode.children[1]).toBe('true'); | ||
}); |
59 changes: 59 additions & 0 deletions
59
examples/react-native/v12/TestApp/src/components/encryption/EncryptMetadata.tsx
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,59 @@ | ||
// :snippet-start: imports | ||
import React from 'react'; | ||
import {Text, View} from 'react-native'; | ||
import {MetadataMode} from 'realm'; | ||
import {AppProvider} from '@realm/react'; | ||
// :snippet-end: | ||
import {StyleSheet} from 'react-native'; | ||
import {useApp} from '@realm/react'; | ||
import {APP_ID} from '../../../appServicesConfig'; | ||
|
||
// :snippet-start: encrypt-metadata | ||
// :replace-start: { | ||
// "terms": { | ||
// "export ": "" | ||
// } | ||
// } | ||
export const EncryptMetadata = ({ | ||
encryptionKey, | ||
}: { | ||
encryptionKey: ArrayBuffer; | ||
}) => { | ||
// :emphasize-start: | ||
const metadataConfig = { | ||
mode: MetadataMode.Encryption, | ||
encryptionKey: encryptionKey, | ||
}; | ||
// :emphasize-end: | ||
|
||
return ( | ||
<AppProvider | ||
id={APP_ID} | ||
metadata={metadataConfig}> | ||
<RestOfApp /> | ||
</AppProvider> | ||
); | ||
}; | ||
// :replace-end: | ||
// :snippet-end: | ||
|
||
const RestOfApp = () => { | ||
const app = useApp(); | ||
|
||
return ( | ||
<View style={styles.section}> | ||
<Text testID="is-realm-app"> | ||
Is an instance of `Realm.App`?: {app ? 'true' : 'false'} | ||
</Text> | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
section: { | ||
flex: 1, | ||
marginTop: 8, | ||
paddingVertical: 12, | ||
alignItems: 'center', | ||
}, | ||
}); |
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
21 changes: 21 additions & 0 deletions
21
...les/generated/react-native/v12/EncryptMetadata.snippet.encrypt-metadata.tsx.rst
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,21 @@ | ||
.. code-block:: typescript | ||
:emphasize-lines: 6-9 | ||
const EncryptMetadata = ({ | ||
encryptionKey, | ||
}: { | ||
encryptionKey: ArrayBuffer; | ||
}) => { | ||
const metadataConfig = { | ||
mode: MetadataMode.Encryption, | ||
encryptionKey: encryptionKey, | ||
}; | ||
return ( | ||
<AppProvider | ||
id={APP_ID} | ||
metadata={metadataConfig}> | ||
<RestOfApp /> | ||
</AppProvider> | ||
); | ||
}; |
6 changes: 6 additions & 0 deletions
6
source/examples/generated/react-native/v12/EncryptMetadata.snippet.imports.tsx.rst
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,6 @@ | ||
.. code-block:: typescript | ||
import React from 'react'; | ||
import {Text, View} from 'react-native'; | ||
import {MetadataMode} from 'realm'; | ||
import {AppProvider} from '@realm/react'; |
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
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 |
---|---|---|
|
@@ -22,13 +22,16 @@ integrity using a :wikipedia:`hash-based message authentication code | |
|
||
.. include:: /includes/encrypt-use-strong-cryptographic-hash.rst | ||
|
||
Considerations | ||
-------------- | ||
The following code demonstrates how to generate an encryption key and | ||
open an encrypted realm: | ||
|
||
.. literalinclude:: /examples/generated/react-native/ts/encrypted-realm.test.snippet.encrypted-realm.tsx | ||
:language: typescript | ||
|
||
The following are key impacts to consider when encrypting a realm. | ||
|
||
Storing & Reusing Keys | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
---------------------- | ||
|
||
You **must** pass the same encryption key every time you open the encrypted realm. | ||
If you don't provide a key or specify the wrong key for an encrypted | ||
|
@@ -38,12 +41,12 @@ Apps should store the encryption key securely, typically in the target | |
platform's secure key/value storage, so that other apps cannot read the key. | ||
|
||
Performance Impact | ||
~~~~~~~~~~~~~~~~~~ | ||
------------------ | ||
|
||
Reads and writes on encrypted realms can be up to 10% slower than unencrypted realms. | ||
|
||
Encryption and Atlas Device Sync | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
-------------------------------- | ||
|
||
You can encrypt a :ref:`synced realm <react-native-open-a-synced-realm>`. | ||
|
||
|
@@ -54,24 +57,25 @@ use one of the :ref:`Realm authentication providers <users-and-authentication>` | |
and an :ref:`authentication trigger<authentication-triggers>` | ||
to create a 64-bit key and store that key in a :ref:`user object <user-objects>`. | ||
|
||
Accessing an Encrypted Realm from Multiple Processes | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
Access an Encrypted Realm from Multiple Processes | ||
------------------------------------------------- | ||
|
||
.. versionchanged:: ``[email protected]`` | ||
|
||
Starting with Realm React Native SDK version 11.8.0, Realm supports opening | ||
Starting with Realm React Native SDK version v11.8.0, Realm supports opening | ||
the same encrypted realm in multiple processes. | ||
|
||
If your app uses Realm React Native SDK version 11.7.0 or earlier, attempting to | ||
If your app uses Realm React Native SDK version v11.7.0 or earlier, attempting to | ||
open an encrypted realm from multiple processes throws this error: | ||
``Encrypted interprocess sharing is currently unsupported.`` | ||
|
||
Example | ||
------- | ||
Encrypt App Services App Metadata | ||
--------------------------------- | ||
|
||
The following code demonstrates how to generate an encryption key and | ||
open an encrypted realm: | ||
If you use Atlas Device Sync with your realm, your App Services App uses an | ||
on-device metadata file to determine changes that should sync. | ||
|
||
.. literalinclude:: /examples/generated/react-native/ts/encrypted-realm.test.snippet.encrypted-realm.tsx | ||
:language: typescript | ||
You can encrypt this metadata file in a similar manner as encrypting your | ||
realm. | ||
|
||
To learn more, refer to :ref:`Ecrypt App Metadata <react-native-encrypt-app-metadata>`. |