own3d / id
PHP OWN3D ID API Client for Laravel 5+
Installs: 11 292
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 2
Open Issues: 4
Requires
- php: ^7.4|^8.0
- ext-json: *
- doctrine/dbal: ^2.10|^3.6|^4.0
- firebase/php-jwt: ^6.2
- guzzlehttp/guzzle: ^6.3|^7.0
- illuminate/console: ~5.4|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ~5.4|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- socialiteproviders/manager: ^3.4|^4.0.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.18
- orchestra/testbench: ~4.0|^6.0
- own3d/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^8.0|^9.0
- roave/security-advisories: dev-latest
- dev-main
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.9
- 2.2.8
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- dev-oauth-webhook
- dev-sso_provider
- dev-feature/api-changes
- dev-revert-7-sso_provider
- dev-next
- dev-internals
This package is auto-updated.
Last update: 2024-11-11 14:28:17 UTC
README
PHP OWN3D ID API Client for Laravel 5+
Table of contents
- Installation
- OAuth2 Documentation
- Remarks
- Socialite Event Listener
- Configuration
- Examples
- Documentation
- Development
Installation
Install the own3d id package with composer:
composer require own3d/id
OAuth2 Documentation
You can find the documentation for OAuth2 here. There you can find all the information about registering your application and the scopes you can request.
Remarks
In the StreamTV / OWN3D ID Library all SSO IDs are defined as strings. This comes from the origin that all IDs should become UUIDs. We will simply continue the id assignment on big-integers, since we never implemented this step. We recommend to store all ids as big-integers (20) in your database. It is not guaranteed that we will assign IDs incrementally.
E-Mail Verification
Every oauth client needs to check itself if they need a (verified) email address from the user.
The current email address can be fetched via /api/users/@me
, it will be returned in the email
attribute.
To see if the email is verified by the user, you can lookup the email_verified_at
attribute.
If the email
attribute is null
, this means the user has no email associated with his account.
You need to call /api/users/@me/update-email
by yourself to assign and trigger the email verification process.
Socialite Event Listener
- Add
SocialiteProviders\Manager\SocialiteWasCalled
event to yourlisten[]
array inapp/Providers/EventServiceProvider
. - Add your listeners (i.e. the ones from the providers) to the
SocialiteProviders\Manager\SocialiteWasCalled[]
that you just created. - The listener that you add for this provider is
'Own3d\\Id\\Socialite\\Own3dIdExtendSocialite@handle',
. - Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.
use SocialiteProviders\Manager\SocialiteWasCalled; use Own3d\Id\Socialite\Own3dIdExtendSocialite; /** * The event handler mappings for the application. * * @var array */ protected $listen = [ SocialiteWasCalled::class => [ // add your listeners (aka providers) here Own3dIdExtendSocialite::class, ], ];
Configuration
Copy configuration to config folder:
$ php artisan vendor:publish --provider="Own3d\Id\Providers\Own3dIdServiceProvider"
Add environmental variables to your .env
OWN3D_ID_KEY=
OWN3D_ID_SECRET=
OWN3D_ID_REDIRECT_URI=${APP_URL}/login/callback
You will need to add an entry to the services configuration file so that after config files are cached for usage in production environment (Laravel command artisan config:cache
) all config is still available.
Add to config/services.php
:
'own3d-id' => [ 'client_id' => env('OWN3D_ID_KEY'), 'client_secret' => env('OWN3D_ID_SECRET'), 'redirect' => env('OWN3D_ID_REDIRECT_URI') ],
Examples
Basic
$own3dId = new Own3d\Id\Own3dId(); $own3dId->setClientId('abc123'); ...
Setters
$own3dId = new Own3d\Id\Own3dId(); $own3dId->setClientId('abc123'); $own3dId->setClientSecret('abc456'); $own3dId->setToken('abcdef123456'); $own3dId = $own3dId->withClientId('abc123'); $own3dId = $own3dId->withClientSecret('abc123'); $own3dId = $own3dId->withToken('abcdef123456');
OAuth Tokens
$own3dId = new Own3d\Id\Own3dId(); $own3dId->setClientId('abc123'); $own3dId->setToken('abcdef123456'); $result = $own3dId->getAuthedUser(); $user = $userResult->shift();
$own3dId->setToken('uvwxyz456789'); $result = $own3dId->getAuthedUser();
$result = $own3dId->withToken('uvwxyz456789')->getAuthedUser();
Facade
use Own3d\Id\Facades\Own3dId; Own3dId::withClientId('abc123')->withToken('abcdef123456')->getAuthedUser();
Documentation
Oauth
public function retrievingToken(string $grantType, array $attributes)
Users
public function getAuthedUser() public function setAuthedUserEmailAddress(string $email) public function getUserById(string $id) public function getUsersByEmail(string $email) public function getUserConnections(array $parameters = []) public function getUserConnectionByPlatformId(string $platform, string $id)
Events
public function sendEvent(string $type, array $data, string $version)
Protecting Routes
Via Middleware
OWN3D ID includes an authentication guard that will validate access tokens on incoming requests. Once you have configured the api
guard to use the own3d-id
driver, you only need to specify the auth:api
middleware on any routes that should require a valid access token:
Route::get('/user', function () { // })->middleware('auth:api');
Multiple Authentication Guards
If your application authenticates different types of users that perhaps use entirely different Eloquent models, you will likely need to define a guard configuration for each user provider type in your application. This allows you to protect requests intended for specific user providers. For example, given the following guard configuration the config/auth.php
configuration file:
'api' => [ 'driver' => 'own3d-id', 'provider' => 'users', ], 'api-customers' => [ 'driver' => 'own3d-id', 'provider' => 'customers', ],
The following route will utilize the api-customers guard, which uses the customers user provider, to authenticate incoming requests:
Route::get('/customer', function () { // })->middleware('auth:api-customers');
Token Scopes
Scopes allow your application's users to limit the actions a third-party application can perform on their behalf. For example, not all API consumers will need the ability to fetch entitlements.
Defining Scopes
Scopes are registered globally by the OWN3D ID service. If a OWN3D first-party specific application needs a additional scope, then they need to define it in the OWN3D ID service.
Assigning Scopes To Tokens
When requesting an access token using the authorization code grant, consumers should specify their desired scopes as the scope
query string parameter. The scope
parameter should be a space-delimited list of scopes:
Route::get('/redirect', function () { $query = http_build_query([ 'client_id' => 'client-id', 'redirect_uri' => 'http://example.com/callback', 'response_type' => 'code', 'scope' => 'user:read connections', ]); return redirect('https://id.stream.tv/oauth/authorize?' . $query); });
Requesting all Tokens
When using the password grant or client credentials grant, you may wish to authorize the token for all of the scopes supported by your application. You can do this by requesting the *
scope. If you request the *
scope, the can
method on the token instance will always return true
. This scope may only be assigned to a token that is issued using the password
or client_credentials
grant:
TBD
Checking Scopes
Using the
scopes
andscope
middleware requires a authorization guard. For first-party apps, you may want to use a special authentication guard to create users dynamically. If you're interested in machine-to-machine authentication or want to skip the authorization guard, then have a look at client credentials grant tokens.
OWN3D ID includes two middleware that may be used to verify that an incoming request is authenticated with a token that has been granted a given scope. To get started, add the following middleware to the $routeMiddleware
property of your app/Http/Kernel.php
file:
'scopes' => \Own3d\Id\Http\Middleware\CheckScopes::class, 'scope' => \Own3d\Id\Http\Middleware\CheckForAnyScope::class,
Check For All Scopes
The scopes
middleware may be assigned to a route to verify that the incoming request's access token has all of the listed scopes:
Route::get('/test', function () { // Access token has both "user:read" and "connections" scopes... })->middleware(['auth:api', 'user:read,connections']);
Check For Any Scopes
The scope
middleware may be assigned to a route to verify that the incoming request's access token has at least one of the listed scopes:
Route::get('/test', function () { // Access token has either "user:read" or "connections" scope... })->middleware(['auth:api', 'scope:user:read,connections'])
Checking Scopes On A Token Instance
Once an access token authenticated request has entered your application, you may still check if the token has a given scope using the tokenCan
method on the authenticated App\Models\User
instance:
use Illuminate\Http\Request; Route::get('/orders', function (Request $request) { if ($request->user()->tokenCan('user:read')) { // } });
Client Credentials Grant Tokens
The client credentials grant is suitable for machine-to-machine authentication. For example, to performing maintenance tasks over an API.
Before your application can issue tokens via the client credentials grant, you will need to request a client credentials grant client. You may do this by writing to developers@stream.tv.
Next, to use this grant type, you need to add the CheckClientCredentials
middleware to the $routeMiddleware
property of your app/Http/Kernel.php
file:
use Own3d\Id\Http\Middleware\CheckClientCredentials; protected $routeMiddleware = [ 'client' => CheckClientCredentials::class, ];
Then, attach the middleware to a route:
Route::get('/test', function (Request $request) { ... })->middleware('client');
To restrict access to the route to specific scopes, you may provide a comma-delimited list of the required scopes when attaching the client
middleware to the route:
Route::get('/test', function (Request $request) { ... })->middleware('client:user:read,your-scope');
Using OWN3D ID as API Guard
If you want to accept OWN3D ID Access tokens within you API Server, you can easily add/modify your guards, to enable support.
If you also want to generate users automatically in your local database, then use the sso-users
provider within your api
guard.
config/auth.php
:
'guards' => [ ... 'api' => [ 'driver' => 'own3d-id', 'provider' => 'sso-users', ], ], 'providers' => [ ... 'sso-users' => [ 'driver' => 'sso-users', 'model' => App\Models\User::class, 'fields' => ['name', 'email', 'email_verified_at'], ], ],
After configure your guards, you need to register the own3d-id
and sso-users
drivers within your AuthServiceProvider
.
use Illuminate\Http\Request; use Own3d\Id\Auth\Own3dSsoUserProvider; use Own3d\Id\Own3dId; public function boot() { ... Own3dIdGuard::register(); Own3dSsoUserProvider::register(); }
Development
Run Tests
composer test
BASE_URL=xxxx CLIENT_ID=xxxx CLIENT_KEY=yyyy CLIENT_ACCESS_TOKEN=zzzz composer test
Generate Documentation
composer docs