Skip to content

Commit 632116c

Browse files
committed
Docs
1 parent e9f2f9c commit 632116c

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

resources/views/docs/mobile/1/concepts/security.md

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Security
3-
order: 400
3+
order: 100
44
---
55

66
## Security
@@ -25,6 +25,80 @@ level of entropy, as this makes them hard to guess and hard to abuse.
2525
**Always use HTTPS.**
2626

2727
If your application allows users to connect _their own_ API keys for a service, you should treat these keys with great
28-
care. If you choose to store them anywhere (either in a [File](files) or
28+
care. If you choose to store them anywhere (either in a file or
2929
[Database](databases)), make sure you store them
30-
[encrypted](../the-basics/system#encryption-decryption) and decrypt them only when needed.
30+
[encrypted](../the-basics/system#encryption-decryption) and decrypt them only when needed.
31+
32+
## Secure Storage
33+
34+
NativePHP provides access to your users' device's native Keystore/Keychain through the
35+
[`SecureStorage`](/docs/mobile/1/apis/secure-storage) facade, which
36+
allow you to store small amounts of data in a secure way.
37+
38+
The device's secure storage encrypts and decrypts data on the fly and that means you can safely rely on it to store
39+
critical things like API tokens, keeping your users and your systems safe.
40+
41+
This data is only accessible by your app and is persisted beyond the lifetime of your app, so it will still be available
42+
the next time your app is open.
43+
44+
### Why not use the Laravel `Crypt` facade?
45+
46+
By default, the `Crypt` facade - and by extension the `encrypt` and `decrypt` helper functions - all rely on the
47+
`APP_KEY` value set in your `.env` file.
48+
49+
We _will_ use Laravel's underlying `Encryption` class, but you should avoid using these helpers directly.
50+
51+
In the context of distributed apps, the `APP_KEY` is shipped _with_ your app and therefore isn't secure. Anyone who
52+
knows where to look for it will be able to find it. Then any data encrypted with it is no better off than if it was
53+
stored in plain text.
54+
55+
Also, it will be the same key for every user, and this presents a considerable risk.
56+
57+
What you really want is a **unique key for each user**, and for that you really need to generate your encryption key
58+
once your app is installed on your user's device.
59+
60+
You could do this and update the `.env` file, but it would still be stored in a way that an attacker may be able to
61+
exploit.
62+
63+
A better approach is to generate a secure key the first time your app opens, place that key in Secure Storage, and
64+
then use that key to encrypt your other data before storage:
65+
66+
```php
67+
use Illuminate\Encryption\Encrypter;
68+
use Illuminate\Support\Facades\Storage;
69+
use Native\Mobile\Facades\SecureStorage;
70+
71+
function generateRandomKey()
72+
{
73+
return base64_encode(
74+
Encrypter::generateKey(config('app.cipher'))
75+
);
76+
}
77+
78+
$encryptionKey = SecureStorage::get('encryption_key');
79+
80+
if (! $encryptionKey) {
81+
SecureStorage::set('encryption_key', $encryptionKey = generateRandomKey());
82+
}
83+
84+
$mobileEncrypter = new Encrypter($encryptionKey);
85+
86+
$encryptedContents = $mobileEncrypter->encrypt(
87+
$request->file('super_private_file')
88+
);
89+
90+
Storage::put('my_secure_file.pdf', $encryptedContents);
91+
```
92+
93+
And then decrypt it later:
94+
95+
```php
96+
$decryptedContents = $mobileEncrypter->decrypt(
97+
Storage::get('my_secure_file.pdf')
98+
);
99+
```
100+
101+
### Secure Storage vs Database/Files
102+
103+
Secure Storage is only meant for small amounts of text data, usually no more than a few KBs. If you need to store
104+
larger amounts of data or files, you should store this in a database or as a file.

resources/views/docs/mobile/1/getting-started/development.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,8 @@ This is useful during development for quickly testing changes without rebuilding
6666

6767
### Caveats
6868

69-
- This feature is currently best suited for **Blade** and **Livewire** applications.
70-
- It does **not** currently detect or sync **compiled frontend assets**, such as those built with Vite or used by
71-
**Inertia.js**.
72-
- If you're working with a JavaScript-heavy stack (Vue, React, Inertia), you should continue
73-
[building your frontend](/docs/mobile/1/the-basics/assets) before launching the app with `native:run`.
69+
This feature is currently best suited for **Blade** and **Livewire** applications. It does not work so well if you're
70+
also trying to hot reload compiled frontends using something like Vite's hot reloading.
7471

7572
## Releasing
7673

resources/views/docs/mobile/1/getting-started/installation.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,34 @@ email address you used when purchasing your license. Your password is your licen
6868
This package contains all the libraries, classes, commands, and interfaces that your application will need to work with
6969
iOS and Android.
7070

71+
## Run the NativePHP installer
72+
7173
**Before** running the `install` command, it is important to set the following variables in your `.env`:
7274

73-
```shell
75+
```dotenv
7476
NATIVEPHP_APP_ID=com.yourcompany.yourapp
7577
NATIVEPHP_APP_VERSION="DEBUG"
7678
NATIVEPHP_APP_VERSION_CODE="1"
7779
```
7880

79-
## Run the NativePHP installer
81+
Find out more about these options in
82+
[Configuration](/docs/mobile/1/getting-started/configuration#codenativephp-app-idcode).
83+
84+
<aside class="relative z-0 mt-5 overflow-hidden rounded-2xl bg-pink-50 px-5 ring-1 ring-black/5 dark:bg-pink-600/10">
85+
86+
#### Setting your Apple Developer Team ID
87+
88+
It may be useful to set your development team. You can do this via your `.env` file. Your development team ID can be
89+
found in your [Apple Developer account](https://developer.apple.com/account), under 'Membership details'.
90+
91+
![](/img/docs/team-id.png)
92+
93+
```dotenv
94+
NATIVEPHP_DEVELOPMENT_TEAM={your team ID}
95+
```
96+
97+
</aside>
98+
8099

81100
```shell
82101
php artisan native:install

0 commit comments

Comments
 (0)