mirror of
https://github.com/bitinflow/accounts.git
synced 2026-03-14 14:05:52 +00:00
Compare commits
56 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ba3cdffb0 | ||
| 00f30e097d | |||
| 99a9331b6e | |||
| 5caef21b01 | |||
| 8e88d12d88 | |||
| d44400fcfa | |||
| e4378d67c1 | |||
| a205365bf5 | |||
| c7eadf30ee | |||
| 49bbefae64 | |||
| 6290f6a419 | |||
| ac734a04c4 | |||
| 32b7437239 | |||
| 120bd61ae5 | |||
| 78f570f404 | |||
| 7dad10bd47 | |||
| 87fc416b44 | |||
| d03a95b552 | |||
| 1476446765 | |||
| 76243f0883 | |||
| 91bdad593f | |||
| 4fdbe165bc | |||
| f929f32887 | |||
| eb30953d8e | |||
| c6c815523c | |||
| f86a547ea1 | |||
| ac7ffab887 | |||
| db5685d612 | |||
| 3c46197a61 | |||
| a11436fbfe | |||
| c1bd13b449 | |||
| fc54c8b542 | |||
| 6fb7f0b38a | |||
|
e902a3a5b8
|
|||
|
377dc53037
|
|||
|
|
76edd961b7 | ||
|
|
8816ade239 | ||
|
032c771e49
|
|||
| b5b4f9cf5e | |||
| 4f3fa6d571 | |||
|
|
2df4501eae | ||
| 55c4276d9b | |||
|
|
baa45edb8a | ||
| 8f962d1e06 | |||
|
|
3760e312cd | ||
|
|
76b0569b0f | ||
|
|
e60c5a5cd3 | ||
|
|
7af00cb7ab | ||
|
|
3ba4805e3f | ||
|
|
80a6491058 | ||
|
|
14bf9d5480 | ||
|
|
32990da8a0 | ||
|
|
7805933b10 | ||
|
|
e006cd64b5 | ||
|
|
7edff12765 | ||
|
|
94a6a91e84 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
vendor
|
||||
.phpunit.result.cache
|
||||
.idea
|
||||
.idea
|
||||
composer.lock
|
||||
61
AUTH.md
Normal file
61
AUTH.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# Implementing Auth
|
||||
|
||||
This method should typically be called in the `boot` method of your `AuthServiceProvider` class:
|
||||
|
||||
```php
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Providers\BitinflowAccountsSsoUserProvider;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Register any authentication / authorization services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
Auth::provider('sso-users', function ($app, array $config) {
|
||||
return new BitinflowAccountsSsoUserProvider(
|
||||
$app->make(BitinflowAccounts::class),
|
||||
$app->make(Request::class),
|
||||
$config['model'],
|
||||
$config['fields'] ?? [],
|
||||
$config['access_token_field'] ?? null
|
||||
);
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
reference the guard in the `guards` configuration of your `auth.php` configuration file:
|
||||
|
||||
```php
|
||||
'guards' => [
|
||||
'web' => [
|
||||
'driver' => 'session',
|
||||
'provider' => 'users',
|
||||
],
|
||||
|
||||
'api' => [
|
||||
'driver' => 'bitinflow-accounts',
|
||||
'provider' => 'sso-users',
|
||||
],
|
||||
],
|
||||
```
|
||||
|
||||
reference the provider in the `providers` configuration of your `auth.php` configuration file:
|
||||
|
||||
```php
|
||||
'providers' => [
|
||||
'users' => [
|
||||
'driver' => 'eloquent',
|
||||
'model' => App\Models\User::class,
|
||||
],
|
||||
|
||||
'sso-users' => [
|
||||
'driver' => 'sso-users',
|
||||
'model' => App\Models\User::class,
|
||||
'fields' => ['first_name', 'last_name', 'email'],
|
||||
'access_token_field' => 'sso_access_token',
|
||||
],
|
||||
],
|
||||
```
|
||||
39
README.md
39
README.md
@@ -1,8 +1,9 @@
|
||||
# bitinflow Accounts
|
||||
|
||||
[](https://packagist.org/packages/ghostzero/bitinflow-accounts)
|
||||
[](https://packagist.org/packages/ghostzero/bitinflow-accounts)
|
||||
[](https://packagist.org/packages/ghostzero/bitinflow-accounts)
|
||||
<a href="https://packagist.org/packages/ghostzero/bitinflow-accounts"><img src="https://img.shields.io/packagist/dt/ghostzero/bitinflow-accounts" alt="Total Downloads"></a>
|
||||
<a href="https://packagist.org/packages/ghostzero/bitinflow-accounts"><img src="https://img.shields.io/packagist/v/ghostzero/bitinflow-accounts" alt="Latest Stable Version"></a>
|
||||
<a href="https://packagist.org/packages/ghostzero/bitinflow-accounts"><img src="https://img.shields.io/packagist/l/ghostzero/bitinflow-accounts" alt="License"></a>
|
||||
<a href="https://ghostzero.dev/discord"><img src="https://discordapp.com/api/guilds/590942233126240261/embed.png?style=shield" alt="Discord"></a>
|
||||
|
||||
PHP bitinflow Accounts API Client for Laravel 5+
|
||||
|
||||
@@ -26,7 +27,7 @@ composer require ghostzero/bitinflow-accounts
|
||||
Add Service Provider to your `app.php` configuration file:
|
||||
|
||||
```php
|
||||
GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider::class,
|
||||
Bitinflow\Accounts\Providers\BitinflowAccountsServiceProvider::class,
|
||||
```
|
||||
|
||||
## Event Listener
|
||||
@@ -46,7 +47,7 @@ GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider::class,
|
||||
protected $listen = [
|
||||
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
|
||||
// add your listeners (aka providers) here
|
||||
'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',
|
||||
'Bitinflow\\Accounts\\Socialite\\BitinflowExtendSocialite@handle',
|
||||
],
|
||||
];
|
||||
```
|
||||
@@ -56,7 +57,7 @@ protected $listen = [
|
||||
Copy configuration to config folder:
|
||||
|
||||
```
|
||||
$ php artisan vendor:publish --provider="GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider"
|
||||
$ php artisan vendor:publish --provider="Bitinflow\Accounts\Providers\BitinflowAccountsServiceProvider"
|
||||
```
|
||||
|
||||
Add environmental variables to your `.env`
|
||||
@@ -84,7 +85,7 @@ You will need to add an entry to the services configuration file so that after c
|
||||
#### Basic
|
||||
|
||||
```php
|
||||
$bitinflowAccounts = new GhostZero\BitinflowAccounts\BitinflowAccounts();
|
||||
$bitinflowAccounts = new Bitinflow\Accounts\BitinflowAccounts();
|
||||
|
||||
$bitinflowAccounts->setClientId('abc123');
|
||||
|
||||
@@ -105,7 +106,7 @@ echo $sshKey->name;
|
||||
#### Setters
|
||||
|
||||
```php
|
||||
$bitinflowAccounts = new GhostZero\BitinflowAccounts\BitinflowAccounts();
|
||||
$bitinflowAccounts = new Bitinflow\Accounts\BitinflowAccounts();
|
||||
|
||||
$bitinflowAccounts->setClientId('abc123');
|
||||
$bitinflowAccounts->setClientSecret('abc456');
|
||||
@@ -119,7 +120,7 @@ $bitinflowAccounts = $bitinflowAccounts->withToken('abcdef123456');
|
||||
#### OAuth Tokens
|
||||
|
||||
```php
|
||||
$bitinflowAccounts = new GhostZero\BitinflowAccounts\BitinflowAccounts();
|
||||
$bitinflowAccounts = new Bitinflow\Accounts\BitinflowAccounts();
|
||||
|
||||
$bitinflowAccounts->setClientId('abc123');
|
||||
$bitinflowAccounts->setToken('abcdef123456');
|
||||
@@ -142,29 +143,13 @@ $result = $bitinflowAccounts->withToken('uvwxyz456789')->getAuthedUser();
|
||||
#### Facade
|
||||
|
||||
```php
|
||||
use GhostZero\BitinflowAccounts\Facades\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Facades\BitinflowAccounts;
|
||||
|
||||
BitinflowAccounts::withClientId('abc123')->withToken('abcdef123456')->getAuthedUser();
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
### Charges
|
||||
|
||||
```php
|
||||
public function createCharge(array $parameters)
|
||||
public function getCharge(string $id)
|
||||
public function updateCharge(string $id, array $parameters)
|
||||
public function captureCharge(string $id, array $parameters = array ())
|
||||
```
|
||||
|
||||
### Documents
|
||||
|
||||
```php
|
||||
public function createDocument(array $parameters)
|
||||
public function createDocumentDownloadUrl(string $identifier, CarbonInterface $expiresAt = NULL)
|
||||
```
|
||||
|
||||
### Oauth
|
||||
|
||||
```php
|
||||
@@ -217,4 +202,4 @@ composer docs
|
||||
|
||||
Join the bitinflow Discord!
|
||||
|
||||
[](https://discord.gg/2ZrCe2h)
|
||||
[](https://discord.gg/2ZrCe2h)
|
||||
|
||||
16
README.stub
16
README.stub
@@ -26,7 +26,7 @@ composer require ghostzero/bitinflow-accounts
|
||||
Add Service Provider to your `app.php` configuration file:
|
||||
|
||||
```php
|
||||
GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider::class,
|
||||
Bitinflow\Accounts\Providers\BitinflowAccountsServiceProvider::class,
|
||||
```
|
||||
|
||||
## Event Listener
|
||||
@@ -46,7 +46,7 @@ GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider::class,
|
||||
protected $listen = [
|
||||
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
|
||||
// add your listeners (aka providers) here
|
||||
'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',
|
||||
'Bitinflow\\Accounts\\Socialite\\BitinflowExtendSocialite@handle',
|
||||
],
|
||||
];
|
||||
```
|
||||
@@ -56,7 +56,7 @@ protected $listen = [
|
||||
Copy configuration to config folder:
|
||||
|
||||
```
|
||||
$ php artisan vendor:publish --provider="GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider"
|
||||
$ php artisan vendor:publish --provider="Bitinflow\Accounts\Providers\BitinflowAccountsServiceProvider"
|
||||
```
|
||||
|
||||
Add environmental variables to your `.env`
|
||||
@@ -84,7 +84,7 @@ You will need to add an entry to the services configuration file so that after c
|
||||
#### Basic
|
||||
|
||||
```php
|
||||
$bitinflowAccounts = new GhostZero\BitinflowAccounts\BitinflowAccounts();
|
||||
$bitinflowAccounts = new Bitinflow\Accounts\BitinflowAccounts();
|
||||
|
||||
$bitinflowAccounts->setClientId('abc123');
|
||||
|
||||
@@ -105,7 +105,7 @@ echo $sshKey->name;
|
||||
#### Setters
|
||||
|
||||
```php
|
||||
$bitinflowAccounts = new GhostZero\BitinflowAccounts\BitinflowAccounts();
|
||||
$bitinflowAccounts = new Bitinflow\Accounts\BitinflowAccounts();
|
||||
|
||||
$bitinflowAccounts->setClientId('abc123');
|
||||
$bitinflowAccounts->setClientSecret('abc456');
|
||||
@@ -119,7 +119,7 @@ $bitinflowAccounts = $bitinflowAccounts->withToken('abcdef123456');
|
||||
#### OAuth Tokens
|
||||
|
||||
```php
|
||||
$bitinflowAccounts = new GhostZero\BitinflowAccounts\BitinflowAccounts();
|
||||
$bitinflowAccounts = new Bitinflow\Accounts\BitinflowAccounts();
|
||||
|
||||
$bitinflowAccounts->setClientId('abc123');
|
||||
$bitinflowAccounts->setToken('abcdef123456');
|
||||
@@ -142,7 +142,7 @@ $result = $bitinflowAccounts->withToken('uvwxyz456789')->getAuthedUser();
|
||||
#### Facade
|
||||
|
||||
```php
|
||||
use GhostZero\BitinflowAccounts\Facades\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Facades\BitinflowAccounts;
|
||||
|
||||
BitinflowAccounts::withClientId('abc123')->withToken('abcdef123456')->getAuthedUser();
|
||||
```
|
||||
@@ -175,4 +175,4 @@ composer docs
|
||||
|
||||
Join the bitinflow Discord!
|
||||
|
||||
[](https://discord.gg/2ZrCe2h)
|
||||
[](https://discord.gg/2ZrCe2h)
|
||||
|
||||
@@ -1,34 +1,39 @@
|
||||
{
|
||||
"name": "ghostzero/bitinflow-accounts",
|
||||
"name": "bitinflow/accounts",
|
||||
"description": "PHP bitinflow Accounts API Client for Laravel 5+",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "René Preuß",
|
||||
"email": "rene@preuss.io"
|
||||
"email": "rene@bitinflow.com"
|
||||
},
|
||||
{
|
||||
"name": "Maurice Preuß",
|
||||
"email": "maurice@bitinflow.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.2",
|
||||
"php": "^7.4|^8.0",
|
||||
"ext-json": "*",
|
||||
"illuminate/support": "^8.0",
|
||||
"illuminate/console": "^8.0",
|
||||
"guzzlehttp/guzzle": "^6.3",
|
||||
"socialiteproviders/manager": "^3.4"
|
||||
"illuminate/support": "~5.4|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0",
|
||||
"illuminate/console": "~5.4|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0",
|
||||
"guzzlehttp/guzzle": "^6.3|^7.0",
|
||||
"socialiteproviders/manager": "^3.4|^4.0.1",
|
||||
"firebase/php-jwt": "^5.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^8.0",
|
||||
"phpunit/phpunit": "^8.0|^9.0",
|
||||
"orchestra/testbench": "^6.0",
|
||||
"codedungeon/phpunit-result-printer": "^0.26.2"
|
||||
"codedungeon/phpunit-result-printer": "^0.31"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"GhostZero\\BitinflowAccounts\\": "src/GhostZero/BitinflowAccounts"
|
||||
"Bitinflow\\Accounts\\": "src/Accounts"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"GhostZero\\BitinflowAccounts\\Tests\\": "tests/GhostZero/BitinflowAccounts"
|
||||
"Bitinflow\\Accounts\\Tests\\": "tests/Accounts"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
@@ -38,10 +43,10 @@
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"GhostZero\\BitinflowAccounts\\Providers\\BitinflowAccountsServiceProvider"
|
||||
"Bitinflow\\Accounts\\Providers\\BitinflowAccountsServiceProvider"
|
||||
],
|
||||
"aliases": {
|
||||
"BitinflowAccounts": "GhostZero\\BitinflowAccounts\\Facades\\BitinflowAccounts"
|
||||
"BitinflowAccounts": "Bitinflow\\Accounts\\Facades\\BitinflowAccounts"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
6450
composer.lock
generated
6450
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'client_id' => env('BITINFLOW_ACCOUNTS_KEY', ''),
|
||||
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET', ''),
|
||||
'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI', ''),
|
||||
'base_url' => env('BITINFLOW_ACCOUNTS_BASE_URI', ''),
|
||||
];
|
||||
15
config/bitinflow-accounts.php
Normal file
15
config/bitinflow-accounts.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
// Accounts
|
||||
'client_id' => env('BITINFLOW_ACCOUNTS_KEY'),
|
||||
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET'),
|
||||
'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI'),
|
||||
'base_url' => env('BITINFLOW_ACCOUNTS_BASE_URL'),
|
||||
|
||||
// Payments
|
||||
'payments' => [
|
||||
'base_url' => env('BITINFLOW_PAYMENTS_BASE_URL', 'https://api.pay.bitinflow.com/v1/'),
|
||||
'dashboard_url' => env('BITINFLOW_PAYMENTS_DASHBOARD_URL', 'https://pay.bitinflow.com/v1/'),
|
||||
]
|
||||
];
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use GhostZero\BitinflowAccounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
$markdown = collect(class_uses(BitinflowAccounts::class))
|
||||
|
||||
14
oauth-public.key
Normal file
14
oauth-public.key
Normal file
@@ -0,0 +1,14 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA3gKvEGl/CdMihkhttsZX
|
||||
gbnT9Rj+m3d1nI1UG5T2kpzUx5fjnXA70Bfw/wrPyUDucXgTfPro98mZczCvWzY+
|
||||
8EUF3zOM8BUaMOBEPUJV1Kb7mmGX6JI79KgEj5xBnQMNyEg+D6ZiUT7vu/I4JYik
|
||||
TfPHzOz2d6w9g5a02aOKC/PXDjs+G16I6YKxKWNC1vMj4K1/1XNlkN4iu7e0AThQ
|
||||
CcyrAB2qGSgJdEMJTsSB7WmknRZuc69nIpXdWoX1ctdjBiXoEh/C23SmXREX6/oP
|
||||
KeLoT4qy3TkD2Z2sD+ytTDXuhp2HLL01NtUap3QUkriG/GdtT9Xa/AYYrNLYUDQG
|
||||
LpYZU3EG+YLe2AmauouPGKiuHwgHix1yZ5x3iHhAsS8r5ZvumdnaJMB05TgE6sXe
|
||||
Q5dG9TOHsIT9lLduFHpZOg4MjOt0Oj3/IdeP1wXN/JxWbEebkW42vyfXEtD20kbx
|
||||
J3TcKEEB2YSg9qZzk7vvRkdzJCKA2Dmj0sZcRsSK0rYAP0kL7gLWAH3At3sD57cx
|
||||
7i+Hi2lFbK4xRu1hcqpAcu5SPBSBB6lbyaW1XY6u8chc/zVyYdsXzyWBHx9P/Dkh
|
||||
m6kBNv8/HfSz0ZwRPH4Dav57DEnaZPIXaXOE4e4S5BooPL9i7Stz7DkStteZoM09
|
||||
y8bcxEnkdxqOnHc0cYZvPlsCAwEAAQ==
|
||||
-----END PUBLIC KEY-----
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\ApiOperations;
|
||||
namespace Bitinflow\Accounts\ApiOperations;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Helpers\Paginator;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use Bitinflow\Accounts\Helpers\Paginator;
|
||||
use Bitinflow\Accounts\Result;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\ApiOperations;
|
||||
namespace Bitinflow\Accounts\ApiOperations;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Helpers\Paginator;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use Bitinflow\Accounts\Helpers\Paginator;
|
||||
use Bitinflow\Accounts\Result;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\ApiOperations;
|
||||
namespace Bitinflow\Accounts\ApiOperations;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Helpers\Paginator;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use Bitinflow\Accounts\Helpers\Paginator;
|
||||
use Bitinflow\Accounts\Result;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\ApiOperations;
|
||||
namespace Bitinflow\Accounts\ApiOperations;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Helpers\Paginator;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use Bitinflow\Accounts\Helpers\Paginator;
|
||||
use Bitinflow\Accounts\Result;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
82
src/Accounts/ApiTokenCookieFactory.php
Normal file
82
src/Accounts/ApiTokenCookieFactory.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Firebase\JWT\JWT;
|
||||
use Illuminate\Contracts\Config\Repository as Config;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Symfony\Component\HttpFoundation\Cookie;
|
||||
|
||||
class ApiTokenCookieFactory
|
||||
{
|
||||
/**
|
||||
* The configuration repository implementation.
|
||||
*
|
||||
* @var Config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* The encrypter implementation.
|
||||
*
|
||||
* @var Encrypter
|
||||
*/
|
||||
protected $encrypter;
|
||||
|
||||
/**
|
||||
* Create an API token cookie factory instance.
|
||||
*
|
||||
* @param Config $config
|
||||
* @param Encrypter $encrypter
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Config $config, Encrypter $encrypter)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->encrypter = $encrypter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new API token cookie.
|
||||
*
|
||||
* @param mixed $userId
|
||||
* @param string $csrfToken
|
||||
* @return Cookie
|
||||
*/
|
||||
public function make($userId, string $csrfToken): Cookie
|
||||
{
|
||||
$config = $this->config->get('session');
|
||||
|
||||
$expiration = Carbon::now()->addMinutes($config['lifetime']);
|
||||
|
||||
return new Cookie(
|
||||
BitinflowAccounts::cookie(),
|
||||
$this->createToken($userId, $csrfToken, $expiration),
|
||||
$expiration,
|
||||
$config['path'],
|
||||
$config['domain'],
|
||||
$config['secure'],
|
||||
true,
|
||||
false,
|
||||
$config['same_site'] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new JWT token for the given user ID and CSRF token.
|
||||
*
|
||||
* @param mixed $userId
|
||||
* @param string $csrfToken
|
||||
* @param Carbon $expiration
|
||||
* @return string
|
||||
*/
|
||||
protected function createToken($userId, string $csrfToken, Carbon $expiration): string
|
||||
{
|
||||
return JWT::encode([
|
||||
'sub' => $userId,
|
||||
'csrf' => $csrfToken,
|
||||
'expiry' => $expiration->getTimestamp(),
|
||||
], $this->encrypter->getKey());
|
||||
}
|
||||
}
|
||||
229
src/Accounts/Auth/TokenGuard.php
Normal file
229
src/Accounts/Auth/TokenGuard.php
Normal file
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Auth;
|
||||
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Helpers\JwtParser;
|
||||
use Bitinflow\Accounts\Traits\HasBitinflowTokens;
|
||||
use Exception;
|
||||
use Firebase\JWT\JWT;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Auth\GuardHelpers;
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Illuminate\Contracts\Debug\ExceptionHandler;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Illuminate\Cookie\CookieValuePrefix;
|
||||
use Illuminate\Cookie\Middleware\EncryptCookies;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Http\Request;
|
||||
use stdClass;
|
||||
use Throwable;
|
||||
|
||||
class TokenGuard
|
||||
{
|
||||
use GuardHelpers;
|
||||
|
||||
/**
|
||||
* @var Encrypter
|
||||
*/
|
||||
private $encrypter;
|
||||
|
||||
/**
|
||||
* @var JwtParser
|
||||
*/
|
||||
private $jwtParser;
|
||||
|
||||
public function __construct(UserProvider $provider, Encrypter $encrypter, JwtParser $jwtParser)
|
||||
{
|
||||
$this->provider = $provider;
|
||||
$this->encrypter = $encrypter;
|
||||
$this->jwtParser = $jwtParser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user for the incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
* @throws BindingResolutionException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function user(Request $request): ?Authenticatable
|
||||
{
|
||||
if ($request->bearerToken()) {
|
||||
return $this->authenticateViaBearerToken($request);
|
||||
} elseif ($request->cookie(BitinflowAccounts::cookie())) {
|
||||
return $this->authenticateViaCookie($request);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate the incoming request via the Bearer token.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Authenticatable
|
||||
* @throws BindingResolutionException
|
||||
* @throws Throwable
|
||||
*/
|
||||
protected function authenticateViaBearerToken(Request $request): ?Authenticatable
|
||||
{
|
||||
if (!$token = $this->validateRequestViaBearerToken($request)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// If the access token is valid we will retrieve the user according to the user ID
|
||||
// associated with the token. We will use the provider implementation which may
|
||||
// be used to retrieve users from Eloquent. Next, we'll be ready to continue.
|
||||
/** @var Authenticatable|HasBitinflowTokens $user */
|
||||
$user = $this->provider->retrieveById(
|
||||
$request->attributes->get('oauth_user_id') ?: null
|
||||
);
|
||||
|
||||
if (!$user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $token ? $user->withBitinflowAccessToken($token) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate and get the incoming request via the Bearer token.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return stdClass|null
|
||||
* @throws BindingResolutionException
|
||||
* @throws Throwable
|
||||
*/
|
||||
protected function validateRequestViaBearerToken(Request $request): ?stdClass
|
||||
{
|
||||
try {
|
||||
$decoded = $this->jwtParser->decode($request);
|
||||
|
||||
$request->attributes->set('oauth_access_token_id', $decoded->jti);
|
||||
$request->attributes->set('oauth_client_id', $decoded->aud);
|
||||
$request->attributes->set('oauth_client_trusted', $decoded->client->trusted);
|
||||
$request->attributes->set('oauth_user_id', $decoded->sub);
|
||||
$request->attributes->set('oauth_scopes', $decoded->scopes);
|
||||
|
||||
return $decoded;
|
||||
} catch (AuthenticationException $e) {
|
||||
$request->headers->set('Authorization', '', true);
|
||||
|
||||
Container::getInstance()->make(
|
||||
ExceptionHandler::class
|
||||
)->report($e);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate the incoming request via the token cookie.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
protected function authenticateViaCookie(Request $request)
|
||||
{
|
||||
if (!$token = $this->getTokenViaCookie($request)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// If this user exists, we will return this user and attach a "transient" token to
|
||||
// the user model. The transient token assumes it has all scopes since the user
|
||||
// is physically logged into the application via the application's interface.
|
||||
/** @var Authenticatable|HasBitinflowTokens $user */
|
||||
if ($user = $this->provider->retrieveById($token['sub'])) {
|
||||
return $user->withBitinflowAccessToken((object)['scopes' => ['*']]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the token cookie via the incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getTokenViaCookie(Request $request): ?array
|
||||
{
|
||||
// If we need to retrieve the token from the cookie, it'll be encrypted so we must
|
||||
// first decrypt the cookie and then attempt to find the token value within the
|
||||
// database. If we can't decrypt the value we'll bail out with a null return.
|
||||
try {
|
||||
$token = $this->decodeJwtTokenCookie($request);
|
||||
} catch (Exception $e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// We will compare the CSRF token in the decoded API token against the CSRF header
|
||||
// sent with the request. If they don't match then this request isn't sent from
|
||||
// a valid source and we won't authenticate the request for further handling.
|
||||
if (!BitinflowAccounts::$ignoreCsrfToken && (!$this->validCsrf($token, $request) ||
|
||||
time() >= $token['expiry'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode and decrypt the JWT token cookie.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
protected function decodeJwtTokenCookie(Request $request): array
|
||||
{
|
||||
return (array)JWT::decode(
|
||||
CookieValuePrefix::remove($this->encrypter->decrypt($request->cookie(BitinflowAccounts::cookie()), BitinflowAccounts::$unserializesCookies)),
|
||||
$this->encrypter->getKey(),
|
||||
['HS256']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the CSRF / header are valid and match.
|
||||
*
|
||||
* @param array $token
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
protected function validCsrf(array $token, Request $request): bool
|
||||
{
|
||||
return isset($token['csrf']) && hash_equals(
|
||||
$token['csrf'], (string)$this->getTokenFromRequest($request)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the CSRF token from the request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return string
|
||||
*/
|
||||
protected function getTokenFromRequest(Request $request): string
|
||||
{
|
||||
$token = $request->header('X-CSRF-TOKEN');
|
||||
|
||||
if (!$token && $header = $request->header('X-XSRF-TOKEN')) {
|
||||
$token = CookieValuePrefix::remove($this->encrypter->decrypt($header, static::serialized()));
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the cookie contents should be serialized.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function serialized(): bool
|
||||
{
|
||||
return EncryptCookies::serialized('XSRF-TOKEN');
|
||||
}
|
||||
}
|
||||
86
src/Accounts/Auth/UserProvider.php
Normal file
86
src/Accounts/Auth/UserProvider.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Auth;
|
||||
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\UserProvider as Base;
|
||||
|
||||
class UserProvider implements Base
|
||||
{
|
||||
/**
|
||||
* The user provider instance.
|
||||
*
|
||||
* @var Base
|
||||
*/
|
||||
protected $provider;
|
||||
|
||||
/**
|
||||
* The user provider name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $providerName;
|
||||
|
||||
/**
|
||||
* Create a new Bitinflow Accounts user provider.
|
||||
*
|
||||
* @param Base $provider
|
||||
* @param string $providerName
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Base $provider, $providerName)
|
||||
{
|
||||
$this->provider = $provider;
|
||||
$this->providerName = $providerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function retrieveById($identifier)
|
||||
{
|
||||
return $this->provider->retrieveById($identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function retrieveByToken($identifier, $token)
|
||||
{
|
||||
return $this->provider->retrieveByToken($identifier, $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function updateRememberToken(Authenticatable $user, $token)
|
||||
{
|
||||
$this->provider->updateRememberToken($user, $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function retrieveByCredentials(array $credentials)
|
||||
{
|
||||
return $this->provider->retrieveByCredentials($credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateCredentials(Authenticatable $user, array $credentials)
|
||||
{
|
||||
return $this->provider->validateCredentials($user, $credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the user provider.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getProviderName()
|
||||
{
|
||||
return $this->providerName;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace GhostZero\BitinflowAccounts;
|
||||
namespace Bitinflow\Accounts;
|
||||
|
||||
use GhostZero\BitinflowAccounts\ApiOperations;
|
||||
use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresAuthenticationException;
|
||||
use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresClientIdException;
|
||||
use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresRedirectUriException;
|
||||
use GhostZero\BitinflowAccounts\Helpers\Paginator;
|
||||
use GhostZero\BitinflowAccounts\Traits;
|
||||
use Bitinflow\Accounts\ApiOperations;
|
||||
use Bitinflow\Accounts\Exceptions\RequestRequiresAuthenticationException;
|
||||
use Bitinflow\Accounts\Exceptions\RequestRequiresClientIdException;
|
||||
use Bitinflow\Accounts\Exceptions\RequestRequiresRedirectUriException;
|
||||
use Bitinflow\Accounts\Helpers\Paginator;
|
||||
use Bitinflow\Accounts\Traits;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
@@ -18,52 +19,74 @@ use GuzzleHttp\Exception\RequestException;
|
||||
class BitinflowAccounts
|
||||
{
|
||||
|
||||
use Traits\ChargesTrait;
|
||||
use Traits\DocumentsTrait;
|
||||
use Traits\OauthTrait;
|
||||
use Traits\PaymentIntentsTrait;
|
||||
use Traits\SshKeysTrait;
|
||||
use Traits\UsersTrait;
|
||||
|
||||
use Traits\HasBitinflowPaymentsWallet;
|
||||
|
||||
use ApiOperations\Delete;
|
||||
use ApiOperations\Get;
|
||||
use ApiOperations\Post;
|
||||
use ApiOperations\Put;
|
||||
|
||||
/**
|
||||
* The name for API token cookies.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $cookie = 'bitinflow_token';
|
||||
/**
|
||||
* Indicates if Bitinflow Accounts should ignore incoming CSRF tokens.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public static $ignoreCsrfToken = false;
|
||||
/**
|
||||
* Indicates if Bitinflow Accounts should unserializes cookies.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public static $unserializesCookies = false;
|
||||
private static $baseUrl = 'https://accounts.bitinflow.com/api/';
|
||||
|
||||
/**
|
||||
* Guzzle is used to make http requests.
|
||||
* @var \GuzzleHttp\Client
|
||||
*
|
||||
* @var Client
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* Paginator object.
|
||||
*
|
||||
* @var Paginator
|
||||
*/
|
||||
protected $paginator;
|
||||
|
||||
/**
|
||||
* bitinflow Accounts OAuth token.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $token = null;
|
||||
|
||||
/**
|
||||
* bitinflow Accounts client id.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $clientId = null;
|
||||
|
||||
/**
|
||||
* bitinflow Accounts client secret.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $clientSecret = null;
|
||||
|
||||
/**
|
||||
* bitinflow Accounts OAuth redirect url.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $redirectUri = null;
|
||||
@@ -73,16 +96,16 @@ class BitinflowAccounts
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
if ($clientId = config('bitinflow-accounts-api.client_id')) {
|
||||
if ($clientId = config('bitinflow-accounts.client_id')) {
|
||||
$this->setClientId($clientId);
|
||||
}
|
||||
if ($clientSecret = config('bitinflow-accounts-api.client_secret')) {
|
||||
if ($clientSecret = config('bitinflow-accounts.client_secret')) {
|
||||
$this->setClientSecret($clientSecret);
|
||||
}
|
||||
if ($redirectUri = config('bitinflow-accounts-api.redirect_url')) {
|
||||
if ($redirectUri = config('bitinflow-accounts.redirect_url')) {
|
||||
$this->setRedirectUri($redirectUri);
|
||||
}
|
||||
if ($redirectUri = config('bitinflow-accounts-api.base_url')) {
|
||||
if ($redirectUri = config('bitinflow-accounts.base_url')) {
|
||||
self::setBaseUrl($redirectUri);
|
||||
}
|
||||
$this->client = new Client([
|
||||
@@ -101,29 +124,45 @@ class BitinflowAccounts
|
||||
}
|
||||
|
||||
/**
|
||||
* Get client id.
|
||||
* @return string
|
||||
* @throws RequestRequiresClientIdException
|
||||
* Get or set the name for API token cookies.
|
||||
*
|
||||
* @param string|null $cookie
|
||||
* @return string|static
|
||||
*/
|
||||
public function getClientId(): string
|
||||
public static function cookie($cookie = null)
|
||||
{
|
||||
if (!$this->clientId) {
|
||||
throw new RequestRequiresClientIdException;
|
||||
if (is_null($cookie)) {
|
||||
return static::$cookie;
|
||||
}
|
||||
|
||||
return $this->clientId;
|
||||
static::$cookie = $cookie;
|
||||
|
||||
return new static;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set client id.
|
||||
* Set the current user for the application with the given scopes.
|
||||
*
|
||||
* @param string $clientId bitinflow Accounts client id
|
||||
*
|
||||
* @return void
|
||||
* @param Authenticatable|Traits\HasBitinflowTokens $user
|
||||
* @param array $scopes
|
||||
* @param string $guard
|
||||
* @return Authenticatable
|
||||
*/
|
||||
public function setClientId(string $clientId): void
|
||||
public static function actingAs($user, $scopes = [], $guard = 'api')
|
||||
{
|
||||
$this->clientId = $clientId;
|
||||
$user->withBitinflowAccessToken((object)[
|
||||
'scopes' => $scopes
|
||||
]);
|
||||
|
||||
if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
|
||||
$user->wasRecentlyCreated = false;
|
||||
}
|
||||
|
||||
app('auth')->guard($guard)->setUser($user);
|
||||
|
||||
app('auth')->shouldUse($guard);
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,6 +181,7 @@ class BitinflowAccounts
|
||||
|
||||
/**
|
||||
* Get client secret.
|
||||
*
|
||||
* @return string
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
@@ -182,6 +222,7 @@ class BitinflowAccounts
|
||||
|
||||
/**
|
||||
* Get redirect url.
|
||||
*
|
||||
* @return string
|
||||
* @throws RequestRequiresRedirectUriException
|
||||
*/
|
||||
@@ -222,6 +263,7 @@ class BitinflowAccounts
|
||||
|
||||
/**
|
||||
* Get OAuth token.
|
||||
*
|
||||
* @return string bitinflow Accounts token
|
||||
* @return string|null
|
||||
* @throws RequestRequiresAuthenticationException
|
||||
@@ -262,8 +304,8 @@ class BitinflowAccounts
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param Paginator|null $paginator
|
||||
*
|
||||
* @return Result
|
||||
@@ -275,73 +317,13 @@ class BitinflowAccounts
|
||||
return $this->query('GET', $path, $parameters, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param Paginator|null $paginator
|
||||
*
|
||||
* @return Result
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function post(string $path = '', array $parameters = [], Paginator $paginator = null): Result
|
||||
{
|
||||
return $this->query('POST', $path, $parameters, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param Paginator|null $paginator
|
||||
*
|
||||
* @return Result
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function delete(string $path = '', array $parameters = [], Paginator $paginator = null): Result
|
||||
{
|
||||
return $this->query('DELETE', $path, $parameters, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param Paginator|null $paginator
|
||||
*
|
||||
* @return Result
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function put(string $path = '', array $parameters = [], Paginator $paginator = null): Result
|
||||
{
|
||||
return $this->query('PUT', $path, $parameters, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param string $path
|
||||
* @param array|null $body
|
||||
*
|
||||
* @return Result
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function json(string $method, string $path = '', array $body = null): Result
|
||||
{
|
||||
if ($body) {
|
||||
$body = json_encode(['data' => $body]);
|
||||
}
|
||||
|
||||
return $this->query($method, $path, [], null, $body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build query & execute.
|
||||
*
|
||||
* @param string $method HTTP method
|
||||
* @param string $path Query path
|
||||
* @param array $parameters Query parameters
|
||||
* @param Paginator $paginator Paginator object
|
||||
* @param string $method HTTP method
|
||||
* @param string $path Query path
|
||||
* @param array $parameters Query parameters
|
||||
* @param Paginator $paginator Paginator object
|
||||
* @param mixed|null $jsonBody JSON data
|
||||
*
|
||||
* @return Result Result object
|
||||
@@ -368,26 +350,6 @@ class BitinflowAccounts
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build query with support for multiple smae first-dimension keys.
|
||||
*
|
||||
* @param array $query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function buildQuery(array $query): string
|
||||
{
|
||||
$parts = [];
|
||||
foreach ($query as $name => $value) {
|
||||
$value = (array) $value;
|
||||
array_walk_recursive($value, function ($value) use (&$parts, $name) {
|
||||
$parts[] = urlencode($name) . '=' . urlencode($value);
|
||||
});
|
||||
}
|
||||
|
||||
return implode('&', $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build headers for request.
|
||||
*
|
||||
@@ -411,4 +373,111 @@ class BitinflowAccounts
|
||||
|
||||
return $headers;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get client id.
|
||||
*
|
||||
* @return string
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function getClientId(): string
|
||||
{
|
||||
if (!$this->clientId) {
|
||||
throw new RequestRequiresClientIdException;
|
||||
}
|
||||
|
||||
return $this->clientId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set client id.
|
||||
*
|
||||
* @param string $clientId bitinflow Accounts client id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setClientId(string $clientId): void
|
||||
{
|
||||
$this->clientId = $clientId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build query with support for multiple smae first-dimension keys.
|
||||
*
|
||||
* @param array $query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function buildQuery(array $query): string
|
||||
{
|
||||
$parts = [];
|
||||
foreach ($query as $name => $value) {
|
||||
$value = (array)$value;
|
||||
array_walk_recursive($value, function ($value) use (&$parts, $name) {
|
||||
$parts[] = urlencode($name) . '=' . urlencode($value);
|
||||
});
|
||||
}
|
||||
|
||||
return implode('&', $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param Paginator|null $paginator
|
||||
*
|
||||
* @return Result
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function post(string $path = '', array $parameters = [], Paginator $paginator = null): Result
|
||||
{
|
||||
return $this->query('POST', $path, $parameters, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param Paginator|null $paginator
|
||||
*
|
||||
* @return Result
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function delete(string $path = '', array $parameters = [], Paginator $paginator = null): Result
|
||||
{
|
||||
return $this->query('DELETE', $path, $parameters, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
* @param Paginator|null $paginator
|
||||
*
|
||||
* @return Result
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function put(string $path = '', array $parameters = [], Paginator $paginator = null): Result
|
||||
{
|
||||
return $this->query('PUT', $path, $parameters, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param string $path
|
||||
* @param array|null $body
|
||||
*
|
||||
* @return Result
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function json(string $method, string $path = '', array $body = null): Result
|
||||
{
|
||||
if ($body) {
|
||||
$body = json_encode(['data' => $body]);
|
||||
}
|
||||
|
||||
return $this->query($method, $path, [], null, $body);
|
||||
}
|
||||
}
|
||||
15
src/Accounts/Contracts/AppTokenRepository.php
Normal file
15
src/Accounts/Contracts/AppTokenRepository.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Contracts;
|
||||
|
||||
use Bitinflow\Accounts\Exceptions\RequestFreshAccessTokenException;
|
||||
|
||||
interface AppTokenRepository
|
||||
{
|
||||
/**
|
||||
* @throws RequestFreshAccessTokenException
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAccessToken(): string;
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Enums;
|
||||
namespace Bitinflow\Accounts\Enums;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
41
src/Accounts/Exceptions/MissingScopeException.php
Normal file
41
src/Accounts/Exceptions/MissingScopeException.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Exceptions;
|
||||
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class MissingScopeException extends AuthorizationException
|
||||
{
|
||||
/**
|
||||
* The scopes that the user did not have.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $scopes;
|
||||
|
||||
/**
|
||||
* Create a new missing scope exception.
|
||||
*
|
||||
* @param array|string $scopes
|
||||
* @param string $message
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($scopes = [], $message = 'Invalid scope(s) provided.')
|
||||
{
|
||||
parent::__construct($message);
|
||||
|
||||
$this->scopes = Arr::wrap($scopes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the scopes that the user did not have.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function scopes()
|
||||
{
|
||||
return $this->scopes;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Exceptions;
|
||||
namespace Bitinflow\Accounts\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
27
src/Accounts/Exceptions/RequestFreshAccessTokenException.php
Normal file
27
src/Accounts/Exceptions/RequestFreshAccessTokenException.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Exceptions;
|
||||
|
||||
use DomainException;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@bitinflow.com>
|
||||
*/
|
||||
class RequestFreshAccessTokenException extends DomainException
|
||||
{
|
||||
private ResponseInterface $response;
|
||||
|
||||
public static function fromResponse(ResponseInterface $response): self
|
||||
{
|
||||
$instance = new self(sprintf('Refresh token request from bitinflow accounts failed. Status Code is %s.', $response->getStatusCode()));
|
||||
$instance->response = $response;
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public function getResponse(): ResponseInterface
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Exceptions;
|
||||
namespace Bitinflow\Accounts\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Exceptions;
|
||||
namespace Bitinflow\Accounts\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Exceptions;
|
||||
namespace Bitinflow\Accounts\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Exceptions;
|
||||
namespace Bitinflow\Accounts\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Facades;
|
||||
namespace Bitinflow\Accounts\Facades;
|
||||
|
||||
use GhostZero\BitinflowAccounts\BitinflowAccounts as BitinflowAccountsService;
|
||||
use Bitinflow\Accounts\BitinflowAccounts as BitinflowAccountsService;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
41
src/Accounts/Helpers/JwtParser.php
Normal file
41
src/Accounts/Helpers/JwtParser.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Bitinflow\Accounts\Helpers;
|
||||
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Http\Request;
|
||||
use stdClass;
|
||||
use Throwable;
|
||||
|
||||
class JwtParser
|
||||
{
|
||||
public const ALLOWED_ALGORITHMS = ['RS256'];
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return stdClass
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
public function decode(Request $request): stdClass
|
||||
{
|
||||
JWT::$leeway = 60;
|
||||
|
||||
try {
|
||||
return JWT::decode(
|
||||
$request->bearerToken(),
|
||||
$this->getOauthPublicKey(),
|
||||
self::ALLOWED_ALGORITHMS
|
||||
);
|
||||
} catch (Throwable $exception) {
|
||||
throw (new AuthenticationException());
|
||||
}
|
||||
}
|
||||
|
||||
private function getOauthPublicKey()
|
||||
{
|
||||
return file_get_contents(dirname(__DIR__, 3) . '/oauth-public.key');
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Helpers;
|
||||
namespace Bitinflow\Accounts\Helpers;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use Bitinflow\Accounts\Result;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
@@ -13,17 +13,18 @@ use stdClass;
|
||||
class Paginator
|
||||
{
|
||||
|
||||
/**
|
||||
* bitinflow Accounts response pagination cursor.
|
||||
* @var null|stdClass
|
||||
*/
|
||||
private $pagination;
|
||||
|
||||
/**
|
||||
* Next desired action (first, after, before).
|
||||
*
|
||||
* @var null|string
|
||||
*/
|
||||
public $action = null;
|
||||
/**
|
||||
* bitinflow Accounts response pagination cursor.
|
||||
*
|
||||
* @var null|stdClass
|
||||
*/
|
||||
private $pagination;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -38,7 +39,7 @@ class Paginator
|
||||
/**
|
||||
* Create Paginator from Result object.
|
||||
*
|
||||
* @param Result $result Result object
|
||||
* @param Result $result Result object
|
||||
*
|
||||
* @return self Paginator object
|
||||
*/
|
||||
@@ -49,6 +50,7 @@ class Paginator
|
||||
|
||||
/**
|
||||
* Return the current active cursor.
|
||||
*
|
||||
* @return string bitinflow Accounts cursor
|
||||
*/
|
||||
public function cursor(): string
|
||||
@@ -58,6 +60,7 @@ class Paginator
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the next set of results.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function first(): self
|
||||
@@ -69,6 +72,7 @@ class Paginator
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the first set of results.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function next(): self
|
||||
@@ -80,6 +84,7 @@ class Paginator
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the last set of results.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function back(): self
|
||||
32
src/Accounts/Http/Middleware/CheckClientCredentials.php
Normal file
32
src/Accounts/Http/Middleware/CheckClientCredentials.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Http\Middleware;
|
||||
|
||||
use Bitinflow\Accounts\Exceptions\MissingScopeException;
|
||||
use stdClass;
|
||||
|
||||
class CheckClientCredentials extends CheckCredentials
|
||||
{
|
||||
/**
|
||||
* Validate token credentials.
|
||||
*
|
||||
* @param stdClass $token
|
||||
* @param array $scopes
|
||||
*
|
||||
* @return void
|
||||
* @throws MissingScopeException
|
||||
*
|
||||
*/
|
||||
protected function validateScopes(stdClass $token, array $scopes)
|
||||
{
|
||||
if (in_array('*', $token->scopes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($scopes as $scope) {
|
||||
if (!in_array($scope, $token->scopes)) {
|
||||
throw new MissingScopeException($scopes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Http\Middleware;
|
||||
|
||||
use Bitinflow\Accounts\Exceptions\MissingScopeException;
|
||||
use stdClass;
|
||||
|
||||
class CheckClientCredentialsForAnyScope extends CheckCredentials
|
||||
{
|
||||
/**
|
||||
* Validate token credentials.
|
||||
*
|
||||
* @param stdClass $token
|
||||
* @param array $scopes
|
||||
*
|
||||
* @return void
|
||||
* @throws MissingScopeException
|
||||
*
|
||||
*/
|
||||
protected function validateScopes(stdClass $token, array $scopes)
|
||||
{
|
||||
if (in_array('*', $token->scopes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($scopes as $scope) {
|
||||
if (in_array($scope, $token->scopes)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new MissingScopeException($scopes);
|
||||
}
|
||||
}
|
||||
46
src/Accounts/Http/Middleware/CheckCredentials.php
Normal file
46
src/Accounts/Http/Middleware/CheckCredentials.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Http\Middleware;
|
||||
|
||||
use Bitinflow\Accounts\Exceptions\MissingScopeException;
|
||||
use Bitinflow\Accounts\Helpers\JwtParser;
|
||||
use Closure;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Http\Request;
|
||||
use stdClass;
|
||||
|
||||
abstract class CheckCredentials
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Closure $next
|
||||
* @param mixed ...$scopes
|
||||
*
|
||||
* @return mixed
|
||||
* @throws AuthenticationException|MissingScopeException
|
||||
*
|
||||
*/
|
||||
public function handle($request, Closure $next, ...$scopes)
|
||||
{
|
||||
$decoded = $this->getJwtParser()->decode($request);
|
||||
|
||||
$request->attributes->set('oauth_access_token_id', $decoded->jti);
|
||||
$request->attributes->set('oauth_client_id', $decoded->aud);
|
||||
//$request->attributes->set('oauth_client_trusted', $decoded->client->trusted);
|
||||
$request->attributes->set('oauth_user_id', $decoded->sub);
|
||||
$request->attributes->set('oauth_scopes', $decoded->scopes);
|
||||
|
||||
$this->validateScopes($decoded, $scopes);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
private function getJwtParser(): JwtParser
|
||||
{
|
||||
return app(JwtParser::class);
|
||||
}
|
||||
|
||||
abstract protected function validateScopes(stdClass $token, array $scopes);
|
||||
}
|
||||
39
src/Accounts/Http/Middleware/CheckForAnyScope.php
Normal file
39
src/Accounts/Http/Middleware/CheckForAnyScope.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Bitinflow\Accounts\Http\Middleware;
|
||||
|
||||
|
||||
use Bitinflow\Accounts\Exceptions\MissingScopeException;
|
||||
use Closure;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class CheckForAnyScope
|
||||
{
|
||||
/**
|
||||
* Handle the incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Closure $next
|
||||
* @param mixed ...$scopes
|
||||
* @return Response
|
||||
*
|
||||
* @throws AuthenticationException|MissingScopeException
|
||||
*/
|
||||
public function handle($request, $next, ...$scopes)
|
||||
{
|
||||
if (!$request->user() || !$request->user()->bitinflowToken()) {
|
||||
throw new AuthenticationException;
|
||||
}
|
||||
|
||||
foreach ($scopes as $scope) {
|
||||
if ($request->user()->bitinflowTokenCan($scope)) {
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
throw new MissingScopeException($scopes);
|
||||
}
|
||||
}
|
||||
37
src/Accounts/Http/Middleware/CheckScopes.php
Normal file
37
src/Accounts/Http/Middleware/CheckScopes.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Http\Middleware;
|
||||
|
||||
use Bitinflow\Accounts\Exceptions\MissingScopeException;
|
||||
use Closure;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class CheckScopes
|
||||
{
|
||||
/**
|
||||
* Handle the incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Closure $next
|
||||
* @param mixed ...$scopes
|
||||
* @return Response
|
||||
*
|
||||
* @throws AuthenticationException|MissingScopeException
|
||||
*/
|
||||
public function handle($request, $next, ...$scopes)
|
||||
{
|
||||
if (!$request->user() || !$request->user()->bitinflowToken()) {
|
||||
throw new AuthenticationException;
|
||||
}
|
||||
|
||||
foreach ($scopes as $scope) {
|
||||
if (!$request->user()->bitinflowTokenCan($scope)) {
|
||||
throw new MissingScopeException($scope);
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
117
src/Accounts/Http/Middleware/CreateFreshApiToken.php
Normal file
117
src/Accounts/Http/Middleware/CreateFreshApiToken.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Http\Middleware;
|
||||
|
||||
use Bitinflow\Accounts\ApiTokenCookieFactory;
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Closure;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class CreateFreshApiToken
|
||||
{
|
||||
/**
|
||||
* The API token cookie factory instance.
|
||||
*
|
||||
* @var ApiTokenCookieFactory
|
||||
*/
|
||||
protected $cookieFactory;
|
||||
|
||||
/**
|
||||
* The authentication guard.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $guard;
|
||||
|
||||
/**
|
||||
* Create a new middleware instance.
|
||||
*
|
||||
* @param ApiTokenCookieFactory $cookieFactory
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(ApiTokenCookieFactory $cookieFactory)
|
||||
{
|
||||
$this->cookieFactory = $cookieFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Closure $next
|
||||
* @param string|null $guard
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next, $guard = null)
|
||||
{
|
||||
$this->guard = $guard;
|
||||
|
||||
$response = $next($request);
|
||||
|
||||
if ($this->shouldReceiveFreshToken($request, $response)) {
|
||||
$response->withCookie($this->cookieFactory->make(
|
||||
$request->user($this->guard)->getAuthIdentifier(), $request->session()->token()
|
||||
));
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given request should receive a fresh token.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @return bool
|
||||
*/
|
||||
protected function shouldReceiveFreshToken($request, $response): bool
|
||||
{
|
||||
return $this->requestShouldReceiveFreshToken($request) &&
|
||||
$this->responseShouldReceiveFreshToken($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the request should receive a fresh token.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
protected function requestShouldReceiveFreshToken($request)
|
||||
{
|
||||
return $request->isMethod('GET') && $request->user($this->guard);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the response should receive a fresh token.
|
||||
*
|
||||
* @param Response $response
|
||||
* @return bool
|
||||
*/
|
||||
protected function responseShouldReceiveFreshToken($response)
|
||||
{
|
||||
return ($response instanceof Response ||
|
||||
$response instanceof JsonResponse) &&
|
||||
!$this->alreadyContainsToken($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given response already contains an API token.
|
||||
*
|
||||
* This avoids us overwriting a just "refreshed" token.
|
||||
*
|
||||
* @param Response $response
|
||||
* @return bool
|
||||
*/
|
||||
protected function alreadyContainsToken($response): bool
|
||||
{
|
||||
foreach ($response->headers->getCookies() as $cookie) {
|
||||
if ($cookie->getName() === BitinflowAccounts::cookie()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
90
src/Accounts/Providers/BitinflowAccountsServiceProvider.php
Normal file
90
src/Accounts/Providers/BitinflowAccountsServiceProvider.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Accounts\Providers;
|
||||
|
||||
use Bitinflow\Accounts\Auth\TokenGuard;
|
||||
use Bitinflow\Accounts\Auth\UserProvider;
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Helpers\JwtParser;
|
||||
use Bitinflow\Accounts\Contracts;
|
||||
use Bitinflow\Accounts\Repository;
|
||||
use Illuminate\Auth\RequestGuard;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class BitinflowAccountsServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->publishes([
|
||||
dirname(__DIR__, 3) . '/config/bitinflow-accounts.php' => config_path('bitinflow-accounts.php'),
|
||||
], 'config');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->mergeConfigFrom(dirname(__DIR__, 3) . '/config/bitinflow-accounts.php', 'bitinflow-accounts');
|
||||
$this->app->singleton(Contracts\AppTokenRepository::class, Repository\AppTokenRepository::class);
|
||||
$this->app->singleton(BitinflowAccounts::class, function () {
|
||||
return new BitinflowAccounts;
|
||||
});
|
||||
|
||||
$this->registerGuard();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the token guard.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerGuard()
|
||||
{
|
||||
Auth::resolved(function ($auth) {
|
||||
$auth->extend('bitinflow-accounts', function ($app, $name, array $config) {
|
||||
return tap($this->makeGuard($config), function ($guard) {
|
||||
$this->app->refresh('request', $guard, 'setRequest');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an instance of the token guard.
|
||||
*
|
||||
* @param array $config
|
||||
* @return RequestGuard
|
||||
*/
|
||||
protected function makeGuard(array $config): RequestGuard
|
||||
{
|
||||
return new RequestGuard(function ($request) use ($config) {
|
||||
return (new TokenGuard(
|
||||
new UserProvider(Auth::createUserProvider($config['provider']), $config['provider']),
|
||||
$this->app->make('encrypter'),
|
||||
$this->app->make(JwtParser::class)
|
||||
))->user($request);
|
||||
}, $this->app['request']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provides()
|
||||
{
|
||||
return [BitinflowAccounts::class];
|
||||
}
|
||||
}
|
||||
126
src/Accounts/Providers/BitinflowAccountsSsoUserProvider.php
Normal file
126
src/Accounts/Providers/BitinflowAccountsSsoUserProvider.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Accounts\Providers;
|
||||
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class BitinflowAccountsSsoUserProvider implements UserProvider
|
||||
{
|
||||
private $bitinflowAccounts;
|
||||
private $accessTokenField = null;
|
||||
private $fields;
|
||||
private $model;
|
||||
private $request;
|
||||
|
||||
public function __construct(
|
||||
BitinflowAccounts $bitinflowAccounts,
|
||||
Request $request,
|
||||
string $model,
|
||||
array $fields,
|
||||
?string $accessTokenField = null
|
||||
) {
|
||||
$this->request = $request;
|
||||
$this->model = $model;
|
||||
$this->fields = $fields;
|
||||
$this->accessTokenField = $accessTokenField;
|
||||
$this->bitinflowAccounts = $bitinflowAccounts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $identifier
|
||||
* @return Builder|Model|object|null
|
||||
*/
|
||||
public function retrieveById($identifier)
|
||||
{
|
||||
$model = $this->createModel();
|
||||
$token = $this->request->bearerToken();
|
||||
|
||||
$user = $this->newModelQuery($model)
|
||||
->where($model->getAuthIdentifierName(), $identifier)
|
||||
->first();
|
||||
|
||||
// Return user when found
|
||||
if ($user) {
|
||||
// Update access token when updated
|
||||
if ($this->accessTokenField) {
|
||||
$user[$this->accessTokenField] = $token;
|
||||
|
||||
if ($user->isDirty()) {
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
// Create new user
|
||||
$this->bitinflowAccounts->setToken($token);
|
||||
$result = $this->bitinflowAccounts->getAuthedUser();
|
||||
|
||||
if (!$result->success()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$attributes = Arr::only((array)$result->data(), $this->fields);
|
||||
$attributes[$model->getAuthIdentifierName()] = $result->data->id;
|
||||
|
||||
if ($this->accessTokenField) {
|
||||
$attributes[$this->accessTokenField] = $token;
|
||||
}
|
||||
|
||||
return $this->newModelQuery($model)->create($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the model.
|
||||
*
|
||||
* @return Model
|
||||
*/
|
||||
public function createModel(): Model
|
||||
{
|
||||
$class = '\\' . ltrim($this->model, '\\');
|
||||
|
||||
return new $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new query builder for the model instance.
|
||||
*
|
||||
* @param Model|null $model
|
||||
* @return Builder
|
||||
*/
|
||||
protected function newModelQuery(Model $model = null): Builder
|
||||
{
|
||||
return is_null($model)
|
||||
? $this->createModel()->newQuery()
|
||||
: $model->newQuery();
|
||||
}
|
||||
|
||||
public function retrieveByToken($identifier, $token)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function updateRememberToken(Authenticatable $user, $token)
|
||||
{
|
||||
// void
|
||||
}
|
||||
|
||||
public function retrieveByCredentials(array $credentials)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function validateCredentials(Authenticatable $user, array $credentials)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
61
src/Accounts/Repository/AppTokenRepository.php
Normal file
61
src/Accounts/Repository/AppTokenRepository.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Repository;
|
||||
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Contracts\AppTokenRepository as Repository;
|
||||
use Bitinflow\Accounts\Exceptions\RequestFreshAccessTokenException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class AppTokenRepository implements Repository
|
||||
{
|
||||
public const ACCESS_TOKEN_CACHE_KEY = 'bitinflow-accounts:access_token';
|
||||
|
||||
private BitinflowAccounts $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = app(BitinflowAccounts::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getAccessToken(): string
|
||||
{
|
||||
$accessToken = Cache::get(self::ACCESS_TOKEN_CACHE_KEY);
|
||||
|
||||
if ($accessToken) {
|
||||
return $accessToken;
|
||||
}
|
||||
|
||||
return $this->requestFreshAccessToken('*');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $scope
|
||||
*
|
||||
* @throws RequestFreshAccessTokenException
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function requestFreshAccessToken(string $scope)
|
||||
{
|
||||
$result = $this->getClient()->retrievingToken('client_credentials', [
|
||||
'scope' => $scope,
|
||||
]);
|
||||
|
||||
if ( ! $result->success()) {
|
||||
throw RequestFreshAccessTokenException::fromResponse($result->response());
|
||||
}
|
||||
|
||||
Cache::put(self::ACCESS_TOKEN_CACHE_KEY, $accessToken = $result->data()->access_token, now()->addWeek());
|
||||
|
||||
return $accessToken;
|
||||
}
|
||||
|
||||
private function getClient(): BitinflowAccounts
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts;
|
||||
namespace Bitinflow\Accounts;
|
||||
|
||||
use Bitinflow\Accounts\Helpers\Paginator;
|
||||
use Exception;
|
||||
use GhostZero\BitinflowAccounts\Helpers\Paginator;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use stdClass;
|
||||
|
||||
@@ -17,55 +17,64 @@ class Result
|
||||
{
|
||||
|
||||
/**
|
||||
* Query successfull.
|
||||
* Query successful.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
public $success = false;
|
||||
|
||||
/**
|
||||
* Guzzle exception, if present.
|
||||
*
|
||||
* @var null|mixed
|
||||
*/
|
||||
public $exception = null;
|
||||
|
||||
/**
|
||||
* Query result data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $data = [];
|
||||
|
||||
/**
|
||||
* Total amount of result data.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
public $total = 0;
|
||||
|
||||
/**
|
||||
* Status Code.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
public $status = 0;
|
||||
|
||||
/**
|
||||
* bitinflow Accounts response pagination cursor.
|
||||
* @var null|\stdClass
|
||||
*
|
||||
* @var null|stdClass
|
||||
*/
|
||||
public $pagination;
|
||||
|
||||
/**
|
||||
* Internal paginator.
|
||||
*
|
||||
* @var null|Paginator
|
||||
*/
|
||||
public $paginator;
|
||||
|
||||
/**
|
||||
* Original Guzzle HTTP Response.
|
||||
*
|
||||
* @var ResponseInterface|null
|
||||
*/
|
||||
public $response;
|
||||
|
||||
/**
|
||||
* Original bitinflow Accounts instance.
|
||||
*
|
||||
* @var BitinflowAccounts
|
||||
*/
|
||||
public $bitinflow;
|
||||
@@ -73,9 +82,9 @@ class Result
|
||||
/**
|
||||
* Constructor,
|
||||
*
|
||||
* @param ResponseInterface|null $response HTTP response
|
||||
* @param Exception|mixed $exception Exception, if present
|
||||
* @param null|Paginator $paginator Paginator, if present
|
||||
* @param ResponseInterface|null $response HTTP response
|
||||
* @param Exception|mixed $exception Exception, if present
|
||||
* @param null|Paginator $paginator Paginator, if present
|
||||
*/
|
||||
public function __construct(?ResponseInterface $response, Exception $exception = null, Paginator $paginator = null)
|
||||
{
|
||||
@@ -95,14 +104,14 @@ class Result
|
||||
/**
|
||||
* Sets a class attribute by given JSON Response Body.
|
||||
*
|
||||
* @param stdClass $jsonResponse Response Body
|
||||
* @param string $responseProperty Response property name
|
||||
* @param string|null $attribute Class property name
|
||||
* @param stdClass $jsonResponse Response Body
|
||||
* @param string $responseProperty Response property name
|
||||
* @param string|null $attribute Class property name
|
||||
*/
|
||||
private function setProperty(stdClass $jsonResponse, string $responseProperty, string $attribute = null): void
|
||||
{
|
||||
$classAttribute = $attribute ?? $responseProperty;
|
||||
if ($jsonResponse !== null && property_exists($jsonResponse, $responseProperty)) {
|
||||
if (property_exists($jsonResponse, $responseProperty)) {
|
||||
$this->{$classAttribute} = $jsonResponse->{$responseProperty};
|
||||
} elseif ($responseProperty === 'data') {
|
||||
$this->{$classAttribute} = $jsonResponse;
|
||||
@@ -110,7 +119,8 @@ class Result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns wether the query was successfull.
|
||||
* Returns whether the query was successfully.
|
||||
*
|
||||
* @return bool Success state
|
||||
*/
|
||||
public function success(): bool
|
||||
@@ -120,6 +130,7 @@ class Result
|
||||
|
||||
/**
|
||||
* Get the response data, also available as public attribute.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function data()
|
||||
@@ -129,6 +140,7 @@ class Result
|
||||
|
||||
/**
|
||||
* Returns the last HTTP or API error.
|
||||
*
|
||||
* @return string Error message
|
||||
*/
|
||||
public function error(): string
|
||||
@@ -137,7 +149,7 @@ class Result
|
||||
if ($this->exception === null || !$this->exception->hasResponse()) {
|
||||
return 'bitinflow Accounts API Unavailable';
|
||||
}
|
||||
$exception = (string) $this->exception->getResponse()->getBody();
|
||||
$exception = (string)$this->exception->getResponse()->getBody();
|
||||
$exception = @json_decode($exception);
|
||||
if (property_exists($exception, 'message') && !empty($exception->message)) {
|
||||
return $exception->message;
|
||||
@@ -148,6 +160,7 @@ class Result
|
||||
|
||||
/**
|
||||
* Shifts the current result (Use for single user/video etc. query).
|
||||
*
|
||||
* @return mixed Shifted data
|
||||
*/
|
||||
public function shift()
|
||||
@@ -163,25 +176,16 @@ class Result
|
||||
|
||||
/**
|
||||
* Return the current count of items in dataset.
|
||||
*
|
||||
* @return int Count
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return count((array) $this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the first set of results.
|
||||
* @return null|Paginator
|
||||
*/
|
||||
public function first(): ?Paginator
|
||||
{
|
||||
return $this->paginator !== null ? $this->paginator->first() : null;
|
||||
return count((array)$this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the next set of results.
|
||||
* @return null|Paginator
|
||||
*/
|
||||
public function next(): ?Paginator
|
||||
{
|
||||
@@ -190,7 +194,6 @@ class Result
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the last set of results.
|
||||
* @return null|Paginator
|
||||
*/
|
||||
public function back(): ?Paginator
|
||||
{
|
||||
@@ -210,9 +213,9 @@ class Result
|
||||
return null;
|
||||
}
|
||||
$rateLimit = [
|
||||
'limit' => (int) $this->response->getHeaderLine('X-RateLimit-Limit'),
|
||||
'remaining' => (int) $this->response->getHeaderLine('X-RateLimit-Remaining'),
|
||||
'reset' => (int) $this->response->getHeaderLine('Retry-After'),
|
||||
'limit' => (int)$this->response->getHeaderLine('X-RateLimit-Limit'),
|
||||
'remaining' => (int)$this->response->getHeaderLine('X-RateLimit-Remaining'),
|
||||
'reset' => (int)$this->response->getHeaderLine('Retry-After'),
|
||||
];
|
||||
if ($key === null) {
|
||||
return $rateLimit;
|
||||
@@ -225,9 +228,7 @@ class Result
|
||||
* Insert users in data response.
|
||||
*
|
||||
* @param string $identifierAttribute Attribute to identify the users
|
||||
* @param string $insertTo Data index to insert user data
|
||||
*
|
||||
* @return self
|
||||
* @param string $insertTo Data index to insert user data
|
||||
*/
|
||||
public function insertUsers(string $identifierAttribute = 'user_id', string $insertTo = 'user'): self
|
||||
{
|
||||
@@ -248,4 +249,17 @@ class Result
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the first set of results.
|
||||
*/
|
||||
public function first(): ?Paginator
|
||||
{
|
||||
return $this->paginator !== null ? $this->paginator->first() : null;
|
||||
}
|
||||
|
||||
public function response(): ?ResponseInterface
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Socialite;
|
||||
namespace Bitinflow\Accounts\Socialite;
|
||||
|
||||
use SocialiteProviders\Manager\SocialiteWasCalled;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Socialite;
|
||||
namespace Bitinflow\Accounts\Socialite;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Enums\Scope;
|
||||
use Bitinflow\Accounts\Enums\Scope;
|
||||
use Illuminate\Support\Arr;
|
||||
use Laravel\Socialite\Two\ProviderInterface;
|
||||
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
|
||||
57
src/Accounts/Traits/BitinflowPaymentsWallet/Balance.php
Normal file
57
src/Accounts/Traits/BitinflowPaymentsWallet/Balance.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Balance
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get balance from user.
|
||||
*
|
||||
* @return float|null
|
||||
*/
|
||||
public function get(): ?float
|
||||
{
|
||||
return $this->user->getPaymentsUser()->data->balance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deposit given amount from bank to account.
|
||||
*
|
||||
* @param float $amount
|
||||
* @param string $description
|
||||
* @return bool
|
||||
*/
|
||||
public function deposit(float $amount, string $decription): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('wallet/deposit', [
|
||||
'amount' => $amount,
|
||||
'decription' => $decription,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge given amount from account.
|
||||
*
|
||||
* @param float $amount
|
||||
* @param string $decription
|
||||
* @return bool
|
||||
*/
|
||||
public function charge(float $amount, string $decription): bool
|
||||
{
|
||||
$order = $this->user->orders()->create([
|
||||
'name' => $decription,
|
||||
'description' => 'one-time payment',
|
||||
'amount' => 1,
|
||||
'price' => $amount,
|
||||
]);
|
||||
|
||||
return $this->user->orders()->checkout($order->id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class CheckoutSessions
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function create(array $payload)
|
||||
{
|
||||
return $this->user->asPaymentsUser('POST', 'checkout-sessions', $payload);
|
||||
}
|
||||
|
||||
public function get(string $id)
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', sprintf('checkout-sessions/%s', $id));
|
||||
}
|
||||
|
||||
public function checkout(string $id)
|
||||
{
|
||||
return $this->user->asPaymentsUser('PUT', sprintf('checkout-sessions/%s/checkout', $id));
|
||||
}
|
||||
|
||||
public function revoke(string $id)
|
||||
{
|
||||
return $this->user->asPaymentsUser('PUT', sprintf('checkout-sessions/%s/revoke', $id));
|
||||
}
|
||||
}
|
||||
69
src/Accounts/Traits/BitinflowPaymentsWallet/Orders.php
Normal file
69
src/Accounts/Traits/BitinflowPaymentsWallet/Orders.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Orders
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get orders from user.
|
||||
*
|
||||
* @return object|null
|
||||
*/
|
||||
public function all(): ?object
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', 'orders');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @return object|null
|
||||
*/
|
||||
public function get(string $id): ?object
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', sprintf('orders/%s', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new order.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return object|false
|
||||
*/
|
||||
public function create(array $attributes = []): object|false
|
||||
{
|
||||
return $this->user->asPaymentsUser('POST', 'orders', $attributes)->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkout given subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function checkout(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('orders/%s/checkout', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a running subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function revoke(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('orders/%s/revoke', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Subscriptions
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscriptions from user.
|
||||
*
|
||||
* @return object|null
|
||||
*/
|
||||
public function all(): ?object
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', 'subscriptions');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @return object|null
|
||||
*/
|
||||
public function get(string $id): ?object
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', sprintf('subscriptions/%s', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new subscription.
|
||||
*
|
||||
* @param array $attributes array which requires following attributes:
|
||||
* name, description, period, price
|
||||
* and following attributes are optional:
|
||||
* vat, payload, ends_at, webhook_url, webhook_secret
|
||||
* @return object|false the subscription object
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function create(array $attributes): object|false
|
||||
{
|
||||
return $this->user->asPaymentsUser('POST', 'subscriptions', $attributes)->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $name = 'default'): bool
|
||||
{
|
||||
$subscription = $this->get($name);
|
||||
|
||||
return $subscription && $subscription->status === 'settled' || $subscription && $subscription->resumeable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkout given subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function checkout(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('subscriptions/%s/checkout', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a running subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function revoke(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('subscriptions/%s/revoke', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume a running subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function resume(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('subscriptions/%s/resume', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
23
src/Accounts/Traits/BitinflowPaymentsWallet/Taxation.php
Normal file
23
src/Accounts/Traits/BitinflowPaymentsWallet/Taxation.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Taxation
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get vat from user.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function get(): ?int
|
||||
{
|
||||
return $this->user->getPaymentsUser()->data->taxation->vat;
|
||||
}
|
||||
}
|
||||
54
src/Accounts/Traits/BitinflowPaymentsWallet/Wallets.php
Normal file
54
src/Accounts/Traits/BitinflowPaymentsWallet/Wallets.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Wallets
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all wallets that belongs to the user.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function all(): ?array
|
||||
{
|
||||
return $this->user->getPaymentsUser()->data->wallets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has an active wallet.
|
||||
*
|
||||
* @return bool
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function has(): ?bool
|
||||
{
|
||||
return $this->user->getPaymentsUser()->data->has_wallet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default wallet to given wallet token.
|
||||
*
|
||||
* @param string $token default payment method token
|
||||
* @return bool
|
||||
*/
|
||||
public function setDefault(string $token): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('wallets/default', [
|
||||
'token' => $token
|
||||
]));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setupIntent()
|
||||
{
|
||||
return sprintf('%swallet/?continue_url=%s', config('bitinflow-accounts.payments.dashboard_url'), urlencode(url()->to($success_path)));
|
||||
}
|
||||
}
|
||||
99
src/Accounts/Traits/HasBitinflowPaymentsWallet.php
Normal file
99
src/Accounts/Traits/HasBitinflowPaymentsWallet.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use Bitinflow\Accounts\Traits\BitinflowPaymentsWallet\Balance;
|
||||
use Bitinflow\Accounts\Traits\BitinflowPaymentsWallet\CheckoutSessions;
|
||||
use Bitinflow\Accounts\Traits\BitinflowPaymentsWallet\Orders;
|
||||
use Bitinflow\Accounts\Traits\BitinflowPaymentsWallet\Subscriptions;
|
||||
use Bitinflow\Accounts\Traits\BitinflowPaymentsWallet\Taxation;
|
||||
use Bitinflow\Accounts\Traits\BitinflowPaymentsWallet\Wallets;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
|
||||
/**
|
||||
* @property Balance balance
|
||||
* @property CheckoutSessions checkout_sessions
|
||||
* @property Orders orders
|
||||
* @property Subscriptions subscriptions
|
||||
* @property Taxation taxation
|
||||
* @property Wallets wallets
|
||||
*/
|
||||
trait HasBitinflowPaymentsWallet
|
||||
{
|
||||
protected $paymentsUser = null;
|
||||
|
||||
/**
|
||||
* Create a new payment gateway request.
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $url
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function asPaymentsUser(string $method, string $url, array $attributes = []): mixed
|
||||
{
|
||||
$client = new Client([
|
||||
'base_uri' => config('bitinflow-accounts.payments.base_url'),
|
||||
]);
|
||||
|
||||
$response = $client->request($method, $url, [
|
||||
RequestOptions::JSON => $attributes,
|
||||
RequestOptions::HEADERS => [
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json',
|
||||
'Authorization' => sprintf('Bearer %s', $this->getAttribute(config('auth.providers.sso-users.access_token_field'))),
|
||||
],
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user from payments gateway.
|
||||
*
|
||||
* @return object|null
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getPaymentsUser(): ?object
|
||||
{
|
||||
if (is_null($this->paymentsUser)) {
|
||||
$this->paymentsUser = $this->asPaymentsUser('GET', 'user');
|
||||
}
|
||||
|
||||
return $this->paymentsUser;
|
||||
}
|
||||
|
||||
public function getBalanceAttribute(): Balance
|
||||
{
|
||||
return new Balance($this);
|
||||
}
|
||||
|
||||
public function getCheckoutSessionsAttribute(): CheckoutSessions
|
||||
{
|
||||
return new CheckoutSessions($this);
|
||||
}
|
||||
|
||||
public function getOrdersAttribute(): Orders
|
||||
{
|
||||
return new Orders($this);
|
||||
}
|
||||
|
||||
public function getSubscriptionsAttribute(): Subscriptions
|
||||
{
|
||||
return new Subscriptions($this);
|
||||
}
|
||||
|
||||
public function getTaxationAttribute(): Taxation
|
||||
{
|
||||
return new Taxation($this);
|
||||
}
|
||||
|
||||
public function getWalletsAttribute(): Wallets
|
||||
{
|
||||
return new Wallets($this);
|
||||
}
|
||||
}
|
||||
51
src/Accounts/Traits/HasBitinflowTokens.php
Normal file
51
src/Accounts/Traits/HasBitinflowTokens.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use stdClass;
|
||||
|
||||
trait HasBitinflowTokens
|
||||
{
|
||||
/**
|
||||
* The current access token for the authentication user.
|
||||
*
|
||||
* @var stdClass
|
||||
*/
|
||||
protected $accessToken;
|
||||
|
||||
/**
|
||||
* Get the current access token being used by the user.
|
||||
*
|
||||
* @return stdClass|null
|
||||
*/
|
||||
public function bitinflowToken(): ?stdClass
|
||||
{
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the current API token has a given scope.
|
||||
*
|
||||
* @param string $scope
|
||||
* @return bool
|
||||
*/
|
||||
public function bitinflowTokenCan(string $scope): bool
|
||||
{
|
||||
$scopes = $this->accessToken ? $this->accessToken->scopes : [];
|
||||
|
||||
return in_array('*', $scopes) || in_array($scope, $this->accessToken->scopes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current access token for the user.
|
||||
*
|
||||
* @param stdClass $accessToken
|
||||
* @return $this
|
||||
*/
|
||||
public function withBitinflowAccessToken(stdClass $accessToken): self
|
||||
{
|
||||
$this->accessToken = $accessToken;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Traits;
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use Bitinflow\Accounts\Result;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
|
||||
/**
|
||||
@@ -17,7 +17,7 @@ trait OauthTrait
|
||||
* Retrieving a oauth token using a given grant type.
|
||||
*
|
||||
* @param string $grantType
|
||||
* @param array $attributes
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
@@ -25,12 +25,12 @@ trait OauthTrait
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request('POST', '/oauth/token', [
|
||||
'form_params' => $attributes + [
|
||||
'form_params' => $attributes + [
|
||||
'grant_type' => $grantType,
|
||||
'client_id' => $this->getClientId(),
|
||||
'client_secret' => $this->getClientSecret(),
|
||||
],
|
||||
]);
|
||||
]);
|
||||
|
||||
$result = new Result($response, null);
|
||||
} catch (RequestException $exception) {
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Traits;
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Delete;
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Get;
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Post;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use Bitinflow\Accounts\ApiOperations\Delete;
|
||||
use Bitinflow\Accounts\ApiOperations\Get;
|
||||
use Bitinflow\Accounts\ApiOperations\Post;
|
||||
use Bitinflow\Accounts\Result;
|
||||
|
||||
trait SshKeysTrait
|
||||
{
|
||||
@@ -29,7 +29,7 @@ trait SshKeysTrait
|
||||
/**
|
||||
* Creates ssh key for the currently authed user
|
||||
*
|
||||
* @param string $publicKey
|
||||
* @param string $publicKey
|
||||
* @param string|null $name
|
||||
*
|
||||
* @return Result Result object
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Traits;
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Get;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use Bitinflow\Accounts\ApiOperations\Get;
|
||||
use Bitinflow\Accounts\Result;
|
||||
|
||||
trait UsersTrait
|
||||
{
|
||||
@@ -14,6 +14,7 @@ trait UsersTrait
|
||||
|
||||
/**
|
||||
* Get currently authed user with Bearer Token
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function getAuthedUser(): Result
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Enums;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
class DocumentType
|
||||
{
|
||||
// Read authorized user´s email address.
|
||||
public const TYPE_PDF_INVOICE = 'pdf.invoice';
|
||||
|
||||
// Manage a authorized user object.
|
||||
public const TYPE_PDF_ORDER = 'pdf.order';
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Providers;
|
||||
|
||||
use GhostZero\BitinflowAccounts\BitinflowAccounts;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class BitinflowAccountsServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->publishes([
|
||||
dirname(__DIR__) . '/../../../config/bitinflow-accounts-api.php' => config_path('bitinflow-accounts-api.php'),
|
||||
], 'config');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the application services.
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->mergeConfigFrom(
|
||||
dirname(__DIR__) . '/../../../config/bitinflow-accounts-api.php', 'bitinflow-accounts-api'
|
||||
);
|
||||
$this->app->singleton(BitinflowAccounts::class, function () {
|
||||
return new BitinflowAccounts;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
* @return array
|
||||
*/
|
||||
public function provides()
|
||||
{
|
||||
return [BitinflowAccounts::class];
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Traits;
|
||||
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Get;
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Post;
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Put;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
trait ChargesTrait
|
||||
{
|
||||
|
||||
use Get, Post, Put;
|
||||
|
||||
/**
|
||||
* Create a Charge object
|
||||
*
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function createCharge(array $parameters): Result
|
||||
{
|
||||
return $this->post('charges', $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Charge object
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function getCharge(string $id): Result
|
||||
{
|
||||
return $this->get("charges/$id");
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a Charge object
|
||||
*
|
||||
* @param string $id
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function updateCharge(string $id, array $parameters): Result
|
||||
{
|
||||
return $this->put("charges/$id", $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture a Charge object
|
||||
*
|
||||
* @param string $id
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function captureCharge(string $id, array $parameters = []): Result
|
||||
{
|
||||
return $this->post("charges/$id/capture", $parameters);
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Traits;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Get;
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Post;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
trait DocumentsTrait
|
||||
{
|
||||
|
||||
use Get, Post;
|
||||
|
||||
/**
|
||||
* Create a Documents object
|
||||
*
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public function createDocument(array $parameters): Result
|
||||
{
|
||||
return $this->post('documents', $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Documents download url
|
||||
*
|
||||
* @param string $identifier
|
||||
* @param CarbonInterface|null $expiresAt
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public function createDocumentDownloadUrl(string $identifier, ?CarbonInterface $expiresAt = null): Result
|
||||
{
|
||||
return $this->post("documents/$identifier/download-url", [
|
||||
'expires_at' => $expiresAt
|
||||
? $expiresAt->toDateTimeString()
|
||||
: now()->addHour()->toDateTimeString(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Traits;
|
||||
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Get;
|
||||
use GhostZero\BitinflowAccounts\ApiOperations\Post;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
trait PaymentIntentsTrait
|
||||
{
|
||||
|
||||
use Get, Post;
|
||||
|
||||
/**
|
||||
* Get a Payment Intent object
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function getPaymentIntent(string $id): Result
|
||||
{
|
||||
return $this->get("payment-intents/$id");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Payment Intent object
|
||||
*
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public function createPaymentIntent(array $parameters): Result
|
||||
{
|
||||
return $this->post('payment-intents', $parameters);
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Tests;
|
||||
namespace Bitinflow\Accounts\Tests;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Tests\TestCases\ApiTestCase;
|
||||
use Bitinflow\Accounts\Tests\TestCases\ApiTestCase;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Tests;
|
||||
namespace Bitinflow\Accounts\Tests;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use GhostZero\BitinflowAccounts\Tests\TestCases\ApiTestCase;
|
||||
use Bitinflow\Accounts\Result;
|
||||
use Bitinflow\Accounts\Tests\TestCases\ApiTestCase;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Tests;
|
||||
namespace Bitinflow\Accounts\Tests;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Enums\Scope;
|
||||
use GhostZero\BitinflowAccounts\Tests\TestCases\ApiTestCase;
|
||||
use Bitinflow\Accounts\Enums\Scope;
|
||||
use Bitinflow\Accounts\Tests\TestCases\ApiTestCase;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Tests;
|
||||
namespace Bitinflow\Accounts\Tests;
|
||||
|
||||
use GhostZero\BitinflowAccounts\BitinflowAccounts;
|
||||
use GhostZero\BitinflowAccounts\Facades\BitinflowAccounts as BitinflowAccountsFacade;
|
||||
use GhostZero\BitinflowAccounts\Tests\TestCases\TestCase;
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Facades\BitinflowAccounts as BitinflowAccountsFacade;
|
||||
use Bitinflow\Accounts\Tests\TestCases\TestCase;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Tests\TestCases;
|
||||
namespace Bitinflow\Accounts\Tests\TestCases;
|
||||
|
||||
use GhostZero\BitinflowAccounts\BitinflowAccounts;
|
||||
use GhostZero\BitinflowAccounts\Result;
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Result;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Tests\TestCases;
|
||||
namespace Bitinflow\Accounts\Tests\TestCases;
|
||||
|
||||
use GhostZero\BitinflowAccounts\BitinflowAccounts;
|
||||
use GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider;
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Providers\BitinflowAccountsServiceProvider;
|
||||
use Orchestra\Testbench\TestCase as BaseTestCase;
|
||||
|
||||
/**
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Tests;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Tests\TestCases\ApiTestCase;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
class ApiChargesTest extends ApiTestCase
|
||||
{
|
||||
|
||||
public function testCaptureWithoutCapture(): void
|
||||
{
|
||||
$this->getClient()->withToken($this->getToken());
|
||||
|
||||
$result = $this->getClient()->createCharge([
|
||||
'amount' => 2000,
|
||||
'currency' => 'usd',
|
||||
'source' => 'tok_visa',
|
||||
'description' => 'Charge for jenny.rosen@example.com',
|
||||
]);
|
||||
$this->registerResult($result);
|
||||
$this->assertTrue($result->success());
|
||||
$this->assertArrayHasKey('id', $result->data());
|
||||
$this->assertEquals(2000, $result->data()->amount);
|
||||
$this->assertTrue($result->data()->captured);
|
||||
}
|
||||
|
||||
public function testChargeWithCapture(): void
|
||||
{
|
||||
$this->getClient()->withToken($this->getToken());
|
||||
|
||||
$result = $this->getClient()->createCharge([
|
||||
'amount' => 2000,
|
||||
'currency' => 'usd',
|
||||
'source' => 'tok_visa',
|
||||
'description' => 'Charge for jenny.rosen@example.com',
|
||||
'capture' => false, // default is true for instant capture
|
||||
'metadata' => [
|
||||
'foo' => 'bar',
|
||||
],
|
||||
'receipt_email' => 'rene+unittest@bitinflow.com',
|
||||
]);
|
||||
$this->registerResult($result);
|
||||
$this->assertTrue($result->success());
|
||||
$this->assertArrayHasKey('id', $result->data());
|
||||
$this->assertEquals(2000, $result->data()->amount);
|
||||
$this->assertFalse($result->data()->captured);
|
||||
|
||||
$charge = $result->data();
|
||||
|
||||
$result = $this->getClient()->captureCharge($charge->id);
|
||||
$this->registerResult($result);
|
||||
$this->assertTrue($result->success());
|
||||
$this->assertArrayHasKey('id', $result->data());
|
||||
$this->assertEquals(2000, $result->data()->amount);
|
||||
$this->assertTrue($result->data()->captured);
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Tests;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Enums\DocumentType;
|
||||
use GhostZero\BitinflowAccounts\Tests\TestCases\ApiTestCase;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
class ApiDocumentsTest extends ApiTestCase
|
||||
{
|
||||
|
||||
public function testCreateDocument(): void
|
||||
{
|
||||
$this->getClient()->withToken($this->getToken());
|
||||
|
||||
$result = $this->getClient()->createDocument([
|
||||
'branding' => [
|
||||
'primary_color' => '#8284df',
|
||||
'watermark_url' => 'https://fbs.streamkit.gg/img/pdf/wm.png',
|
||||
'logo_url' => 'https://fbs.streamkit.gg/img/pdf/logo_dark_small.png',
|
||||
],
|
||||
'locale' => 'de',
|
||||
'type' => DocumentType::TYPE_PDF_INVOICE,
|
||||
'data' => $this->createDummyInvoiceData(),
|
||||
'receipt_email' => 'rene+unittest@bitinflow.com',
|
||||
]);
|
||||
|
||||
$this->registerResult($result);
|
||||
$this->assertTrue($result->success());
|
||||
$this->assertArrayHasKey('id', $result->data());
|
||||
$this->assertArrayHasKey('download_url', $result->data());
|
||||
$this->assertEquals(
|
||||
'rene+unittest@bitinflow.com',
|
||||
$result->data()->receipt_email
|
||||
);
|
||||
}
|
||||
|
||||
public function testGenerateDocumentStoragePath(): void
|
||||
{
|
||||
$this->getClient()->withToken($this->getToken());
|
||||
|
||||
$expiresAt = now()->addHours(2);
|
||||
|
||||
$result = $this->getClient()->createDocumentDownloadUrl('1', $expiresAt);
|
||||
|
||||
$this->registerResult($result);
|
||||
$this->assertTrue($result->success());
|
||||
$this->assertArrayHasKey('download_url', $result->data());
|
||||
$this->assertEquals(
|
||||
$expiresAt->toDateTimeString(),
|
||||
$result->data()->expires_at
|
||||
);
|
||||
}
|
||||
|
||||
private function createDummyInvoiceData(): array
|
||||
{
|
||||
return [
|
||||
'id' => 'FBS-IN-1337',
|
||||
'customer' => [
|
||||
'name' => 'GhostZero',
|
||||
'email' => 'rene@preuss.io',
|
||||
'address' => [
|
||||
'Example Street 123',
|
||||
'50733 Cologne',
|
||||
'GERMANY',
|
||||
],
|
||||
],
|
||||
'line_items' => [
|
||||
[
|
||||
'name' => 'T-shirt',
|
||||
'description' => 'Comfortable cotton t-shirt',
|
||||
'unit' => 'T-shirt', // optional unit name
|
||||
'amount' => 1500,
|
||||
'currency' => 'usd',
|
||||
'quantity' => 2,
|
||||
],
|
||||
],
|
||||
'legal_notice' => 'According to the German §19 UStG no sales tax is calculated. However, the product is a digital good delivered via Internet we generally offer no refunds. The delivery date corresponds to the invoice date.',
|
||||
'already_paid' => true,
|
||||
'created_at' => now()->format('d.m.Y'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Tests;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Tests\TestCases\ApiTestCase;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
class ApiPaymentIntentsTest extends ApiTestCase
|
||||
{
|
||||
private $paymentIntent;
|
||||
|
||||
public function testCreatePaymentIntent(): void
|
||||
{
|
||||
$this->getClient()->withToken($this->getToken());
|
||||
|
||||
$result = $this->getClient()->createPaymentIntent([
|
||||
'payment_method_types' => ['card'],
|
||||
'amount' => 1000,
|
||||
'currency' => 'usd',
|
||||
'application_fee_amount' => 123,
|
||||
]);
|
||||
$this->registerResult($result);
|
||||
$this->assertTrue($result->success());
|
||||
$this->assertArrayHasKey('id', $result->data());
|
||||
$this->assertArrayHasKey('redirect_url', $result->data());
|
||||
$this->assertEquals(1000, $result->data()->amount);
|
||||
|
||||
// use this payment intent for our next tests
|
||||
$this->paymentIntent = $result->data();
|
||||
}
|
||||
|
||||
public function testGetPaymentIntent(): void
|
||||
{
|
||||
$this->getClient()->withToken($this->getToken());
|
||||
|
||||
$result = $this->getClient()->getPaymentIntent($this->paymentIntent->id);
|
||||
$this->registerResult($result);
|
||||
$this->assertTrue($result->success());
|
||||
$this->assertArrayHasKey('id', $result->data());
|
||||
$this->assertEquals(1000, $result->data()->amount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user