|
1 | | -## Credential Storage |
| 1 | +# Credential Storage |
2 | 2 |
|
| 3 | +There are currently five credential storage options available: |
| 4 | + |
| 5 | +1. ** File Store ** (default) - Stores the credentials in a JSON file on the default disk. |
| 6 | +2. ** Cache Store ** - Stores the credentials in the cache. |
| 7 | +3. ** Model Store ** - Stores the credentials against a predefined model. |
| 8 | +4. ** Authenticated User Store ** - Stores the credentials against the authenticated user. |
| 9 | +5. ** Array Store ** - Stores the credentials in an array. |
| 10 | + |
| 11 | +## Changing the default storage |
| 12 | + |
| 13 | +If your using a default storage you can change the `xero.credential_store` config key to point to the new implementation. |
| 14 | + |
| 15 | +## File Store |
3 | 16 | Credentials are stored in a JSON file using the default disk on the Laravel Filesystem, with visibility set to private. This allows credential sharing across multiple servers using a shared disk such as S3, regardless of which server conducted the OAuth flow. |
4 | 17 |
|
5 | 18 | To use a different disk, change the `xero.credential_disk` config item to another disk defined in `config/filesystem.php`. |
6 | 19 |
|
7 | | -You can switch out the credential store (e.g. for your own `UserStore` if you wanted to store |
8 | | -the credentials against your user) in one of two ways: |
| 20 | +## Cache Store |
| 21 | + |
| 22 | +Credentials are stored in the cache, this is useful for applications that do not have a shared disk. |
| 23 | + |
| 24 | +## Model Store |
| 25 | + |
| 26 | +Credentials are stored against a predefined model, this is useful for applications that have a settings model. |
| 27 | + |
| 28 | +For the package to know which model you want to use, you will need to call the following method: |
| 29 | + |
| 30 | +```php |
| 31 | +use Webfox\Xero\Xero; |
| 32 | + |
| 33 | +Xero::useModelStore(Settings::class); |
| 34 | +``` |
| 35 | + |
| 36 | +By default, the package will use the `xero_credentials` field, Should you need to rename this field, you can do so by calling: |
| 37 | + |
| 38 | +```php |
| 39 | +use Webfox\Xero\Xero; |
| 40 | + |
| 41 | +Xero::useAttributeOnModelStore('my_custom_field'); |
| 42 | +``` |
| 43 | + |
| 44 | +## Authenticated User Store |
| 45 | + |
| 46 | +Credentials are stored against the authenticated user. By default, the package will use the `xero_credentials` field on the Users table. |
9 | 47 |
|
10 | | -1. If it's a simple store and Laravel can automatically resolve your bindings, simply change the `xero.credential_store` config |
11 | | - key to point to your new implementation. |
12 | | -2. If it requires more advanced logic (e.g. using the current user to retrieve the credentials) then you can rebind this |
13 | | - in your `AppServiceProvider` or a Middleware |
14 | | - e.g. |
| 48 | +Should you need to rename this field, you can do so by calling: |
15 | 49 |
|
16 | 50 | ```php |
17 | | -$this->app->bind(OauthCredentialManager::class, function(Application $app) { |
18 | | - return new UserStorageProvider( |
19 | | - \Auth::user(), // Storage Mechanism |
20 | | - $app->make('session.store'), // Used for storing/retrieving oauth 2 "state" for redirects |
21 | | - $app->make(\Webfox\Xero\Oauth2Provider::class) // Used for getting redirect url and refreshing token |
22 | | - ); |
23 | | -}); |
| 51 | +use Webfox\Xero\Xero; |
| 52 | + |
| 53 | +Xero::useAttributeOnModelStore('my_custom_field'); |
24 | 54 | ``` |
25 | 55 |
|
26 | | -An example UserStorageProvider [can be found here](https://github.com/webfox/laravel-xero-oauth2/issues/45#issuecomment-757552563) |
| 56 | +Should you need to change the authentication guard, you can do so by calling: |
| 57 | +```php |
| 58 | +use Webfox\Xero\Xero; |
| 59 | + |
| 60 | +Xero::setDefaultAuthGuard('admin'); |
| 61 | +``` |
| 62 | + |
| 63 | +## Array Store |
| 64 | + |
| 65 | +Credentials are stored in an array, this is useful for testing purposes only. Once the application is restarted, the credentials will be lost. |
| 66 | + |
| 67 | +## Creating your own Storage Manager |
| 68 | + |
| 69 | +You can create your own storage manager by implementing the `OauthCredentialManager` interface, it is suggested that you extend the `BaseCredentialManager` class. |
| 70 | + |
| 71 | +You will need to implement the following methods: |
| 72 | + |
| 73 | +1. `exists()` - Returns a boolean indicating if the credentials exist. |
| 74 | +2. `store(AccessTokenInterface $token, array $tenants = null)` - Creates/Updates the credentials. |
| 75 | +3. `data(string $key = null)` - Returns the stored credentials (it is recommended you check the credentials exists, if not throw the `XeroCredentialsNotFound` exception). |
| 76 | + |
| 77 | +Once this has been completed, you should point `xero.credential_store` to your new implementation. |
0 commit comments