bildvitta / iss-sdk
This package is used to communicate with the permission and authentication microservice.
Installs: 17 548
Dependents: 3
Suggesters: 0
Security: 0
Stars: 1
Watchers: 18
Forks: 0
Open Issues: 1
Requires
- php: ^7.4|^8.0|^8.1|^8.2|^8.3
- ably/ably-php: ^1.1
- illuminate/contracts: ^7.0|^8.0|^9.0|^10.0|^11.0
- spatie/laravel-permission: ^4.0|^5.0|^6.0
Requires (Dev)
- laravel/pint: ^1.13
- phpunit/phpunit: ^10.0
- dev-master
- v0.1.16
- v0.1.15
- v0.1.15-beta.01
- v0.1.14
- v0.1.14-beta.01
- v0.1.13
- v0.1.13-beta.04
- v0.1.13-beta.03
- v0.1.13-beta.02
- v0.1.13-beta.01
- v0.1.12
- v0.1.12-beta.6
- v0.1.12-beta.5
- v0.1.12-beta.4
- v0.1.12-beta.3
- v0.1.12-beta.2
- v0.1.12-beta.1
- v0.1.11
- v0.1.11-beta.2
- v0.1.11-beta.1
- v0.1.10
- v0.1.10-beta01
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1
- v0.1-beta.21
- v0.1-beta.20
- v0.1-beta.19
- v0.1-beta.18
- v0.1-beta.17
- v0.1-beta.16
- v0.1-beta.15
- v0.1-beta.14
- v0.1-beta.13
- v0.1-beta.12
- v0.1-beta.11
- v0.1-beta.10
- v0.1-beta.9
- v0.1-beta.8
- v0.1-beta.7
- v0.1-beta.6
- v0.1-beta.5
- v0.1-beta.4
- v0.1-beta.3
- v0.1-beta.2
- v0.1-beta.1
- v0.0.69
- v0.0.68
- v0.0.67
- v0.0.66
- v0.0.65
- v0.0.64
- v0.0.63
- v0.0.62
- v0.0.61
- v0.0.60
- v0.0.59
- v0.0.58
- v0.0.57
- v0.0.56
- v0.0.55
- v0.0.54
- v0.0.53
- v0.0.52
- v0.0.51
- v0.0.50
- v0.0.49
- v0.0.48
- v0.0.47
- v0.0.46
- v0.0.45
- v0.0.44
- v0.0.43
- v0.0.42
- v0.0.41
- v0.0.40
- v0.0.39
- v0.0.38
- v0.0.37
- v0.0.36
- v0.0.35
- v0.0.34
- v0.0.33
- v0.0.32
- v0.0.31
- v0.0.30
- v0.0.29
- v0.0.28
- v0.0.27
- v0.0.26
- v0.0.25
- v0.0.24
- v0.0.23
- v0.0.22
- v0.0.21
- v0.0.20
- v0.0.19
- v0.0.18
- v0.0.17
- v0.0.16
- v0.0.15
- v0.0.14
- v0.0.13
- v0.0.12
- v0.0.11
- v0.0.10
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-develop
- dev-feature/brands
- dev-bugfix/duplicate-columns-in-hub
- dev-feature/role-messaging
- dev-feature/fix-style
- dev-feature/update-to-laravel-10
- dev-feature/get-parent-user
- dev-feature/show-all-only-position
- dev-feature/check-position-user
- dev-feature/team-parent-positions
- dev-bugfix/list-all-team
- dev-next
- dev-feature/hub-next
- dev-feature/next-sort-positions
- dev-feature/next-fix-uuid
- dev-feature/next-exceptions
- dev-feature/next-get-by-order
- dev-feature/next-change-requests
- dev-feature/auth-middleware
- dev-feature/user-get-where-query
- dev-feature/companies-change
- dev-feature/cache-barra-me
This package is auto-updated.
Last update: 2024-12-10 15:31:55 UTC
README
Introduction
The ISS (International Space Station) aims to be a space station (client
) of connection between the microservices of
its ecosystem and the authentication and permissions microservice of the user that here is called in the script as
Hub.permissions modules / microservices (Hub)
Installation
You can install the package via composer:
composer require bildvitta/iss-sdk:dev-develop
For everything to work perfectly in addition to having the settings file published in your application, run the command below:
php artisan hub:install
Configuration
This is the contents of the published config file:
return [ 'base_uri' => env('MS_HUB_BASE_URI', 'https://api-dev-hub.nave.dev'), 'front_uri' => env('MS_HUB_FRONT_URI', 'https://develop.hub.nave.dev'), 'prefix' => env('MS_HUB_API_PREFIX', '/api'), 'model_user' => '\App\Entities\User', 'model_company' => '\BildVitta\Hub\Entities\HubCompany::class', 'programatic_access' => [ 'client_id' => env('HUB_PROGRAMMATIC_CLIENT'), 'client_secret' => env('HUB_PROGRAMMATIC_SECRET') ], 'oauth' => [ 'client_id' => env('HUB_CLIENT_ID', ''), 'client_secret' => env('HUB_CLIENT_SECRET', ''), 'redirect' => env('HUB_REDIRECT_URI', ''), 'scopes' => env('HUB_SCOPE', 'profile'), 'authorize_uri' => '/auth/authorize', 'token_uri' => '/oauth/token', 'userinfo_uri' => '/users/me' ] ];
With the configuration file hub.php
published in your configuration folder it is necessary to create environment
variables in your .env
file:
MS_HUB_BASE_URI="https://api-dev-hub.nave.dev" MS_HUB_PREFIX="/api"
Change permission and role model from spatie/laravel-permissions
You should change the default spatie/laravel-permissions models to ours, as we have some substantial changes to the use of Role and Permission.
// config/permission.php return [ 'models' = [ 'permission' => \BildVitta\Hub\Entities\HubPermission::class, 'role' => \BildVitta\Hub\Entities\HubRole::class, ] ];
If you already have a change to these models, just extend our classes to have the correct functionalities.
Add Trait on User Model
And remember to add the BildVitta\Hub\Traits\User\HasCompanyLinks
Trait in the Users model.
// \App\Models\User use BildVitta\Hub\Traits\User\HasCompanyLinks; class User extends Authenticatable { use HasCompanyLinks; ... }
Remembering that this trait already has Spatie\Permission\Traits\HasRoles
by default, so you can remove the Spatie\Permission\Traits\HasRoles
trait from your user model.
Usage
All requests made to the ISS Service will return an instance
of \Illuminate\Http\Client\Response
, which
implements the PHP ArrayAccess
interface, allowing you to access JSON response data directly in the response
This also means that a variety of methods that can be used to inspect the response, follow some below:
$response = Hub::setToken('jwt')->auth()->permissions(); $response->body(); // string; $response->json(); // array|mixed; $response->collect(); // Illuminate\Support\Collection; $response->status(); // int; $response->ok(); // bool; $response->successful(); // bool; $response->failed(); // bool; $response->serverError(); // bool; $response->clientError(); // bool; $response->header('content-type'); // string; $response->headers(); // array;
Initialize ISS Service.
As there are several ways to program, there are also several ways to start the ISS Service.
Below are some ways to start the Service.
$token = 'jwt'; $hub = app('hub', [$token]); // instance 2 $hub = app('hub')->setToken($token); // instance 1 $hub = new \BildVitta\Hub\Hub($token); // instance 3 $hub = (new \BildVitta\Hub\Hub())->setToken($token); // instance 4 $hub = BildVitta\Hub\Facades\Hub::setToken($token); // instance 1
Authenticating User
To authenticate the Hub user in your module, it is necessary to use the
middleware hub.auth = \ BildVitta \ Hub \ Middleware \ AuthenticateHubMiddleware
.
It will validate the token and create, if it does not exist, the user of the token in its user table.
Route::middleware('hub.auth')->get('/users/me', function () { return auth()->user()->toArray(); });
When we installed the package, we created the hub_uuid
column in your user table.
Tf it is not possible to authenticate, the middleware will return 401.
User Authenticated
To access the token's user data directly, there is the \BildVitta\Hub\Contracts\Resources\AuthResourceContract
interface
Check Token
It is verified whether the token passed by parameter or previously loaded in the ISS Service is valid.
Example of use:
try { Hub::auth()->check('jwt'); } catch (RequestException $requestException) { throw new Exception('invalid token'); }
Get Permissions
It is possible to obtain ALL the permissions of the token uploaded to the ISS Service.
Example of use:
try { $permissions = Hub::setToken('jwt')->auth()->permissions()['results']; // Implements `ArrayAccess` foreach ($permissions as $permission) { #TODO } } catch (RequestException $requestException) { #TODO }
Adding permission scope to entity listing.
Now we have added a scope that filters by the permission level of the logged in user. To use it is very simple, just add in the global scopes the PermissionScope class passing the permission that the user has to have, and then the magic happens ;D
Code example:
use BildVitta\Hub\Scopes\PermissionScope; $query = RealEstateDevelopment::query(); $query->withGlobalScope('permission', new PermissionScope('real_estate_developments.show')); $count = $query->count(); $query->pagination(); return (new RealEstateDevelopmentResource('index', $query->get()))->count($count);
Remembering that the scope name has to be permission, if not, it doesn't work <3
Notifications
Make sure the BroadcastServiceProvider is enabled in
config/app.php
Add the ABLY_KEY
key as an environment variable (ask your coordinator for this key)
ABLY_KEY=your-ably-key
Then, set the BROADCAST_CONNECTION environment variable to ably in your application's .env file:
BROADCAST_CONNECTION=ably
Check the routes/channels.php
file if the private channel authentication route is correct.
use Illuminate\Support\Facades\Broadcast; Broadcast::channel('notifications.{uuid}', function ($user, $uuid) { return (string) $user->uuid === (string) $uuid; });
Ensure that the $user->uuid is the same as that used in the hub, otherwise it may result in a 403 in this private channel authentication api.
To finish, go to the BroadcastServiceProvider file and change it to this code.
Broadcast::routes([ 'middleware' => ['hub.check'], 'prefix' => 'api', ]);
Testing
coming soon...
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.